Postagens

Mostrando postagens de Junho, 2018

JavaParser como saber se o else tem Throw

public static void main(String[] args) {
try {
App app = new App();

Leitor leitor = new Leitor("App", "C:\\programa-java\\MetodoLiu\\src\\main\\java\\weiEtal\\App.java");
List<com.github.javaparser.ast.stmt.Statement> linhas = leitor.linhasMetodo("metodo1");
for (com.github.javaparser.ast.stmt.Statement statement : linhas) {
if(statement instanceof IfStmt) {
System.out.println(statement);
IfStmt ifStmt = (IfStmt) statement;
if(ifStmt.hasElseBlock()) {
System.out.println("Bloco else: " + ifStmt.getElseStmt().get());

System.out.println(ifStmt.getElseStmt().get().isThrowStmt());
System.out.println(ifStmt.getElseStmt().get() instanceof ThrowStmt);

List<?> nosElse = ifStmt.getElseStmt().get().getChildNodes();

//pegando lin por lin dentro do else.
for (Object object : nosElse) {
System.out.println(object instanceof ThrowStmt);
}
}
}
}
} catch (Exception e)…

Gestccon Sistema Concierge de Condominios Web

Imagem

Medir velocidade internet

Tabela de comparação entre funções JDT e JavaParser

JDT
Java Parser


IfStatement
IsStmt


Statement
Statement


Block
BlockStmt


Expression
Expression


Field
FieldDeclaration


Method
MethodDeclaration


Type
TypeDeclaration


JDT - AST - Exemplo básico de uso

public static void main(String[] args) {
try {
App app = new App();
ASTParser parser = ASTParser.newParser(AST.JLS3);
try {
parser.setSource(app.readFileToString("C:/programa-java/MetodoLiu/src/main/java/weiEtal/App.java").toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
for (Object declaration : cu.types()) {
if (declaration instanceof TypeDeclaration) {
MethodDeclaration[] metodos = ((TypeDeclaration) declaration).getMethods();
for (MethodDeclaration method : metodos) {
String nomeMetodo = method.getName().getFullyQualifiedName().toString();
if(nomeMetodo.equals("metodo1")) {
MethodDeclaration metodo = method;
List<Statement> statements = metodo.getBody().statements();
for (Statement stmnt : statements) {
if(stmnt instanceof IfStatement){
IfStatement ifStmt = (IfStatement) stmnt;
System.out.println(ifStmt.getElseStatement())…

usando live chat helper para responder mensagens

Imagem