| package me.tomassetti.symbolsolver; |
| |
| import com.github.javaparser.JavaParser; |
| import com.github.javaparser.ParseException; |
| import com.github.javaparser.ast.CompilationUnit; |
| import com.github.javaparser.ast.ImportDeclaration; |
| import com.github.javaparser.ast.Node; |
| import com.github.javaparser.ast.PackageDeclaration; |
| import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; |
| import com.github.javaparser.ast.body.MethodDeclaration; |
| import com.github.javaparser.ast.body.VariableDeclarator; |
| import com.github.javaparser.ast.expr.Expression; |
| import com.github.javaparser.ast.expr.MethodCallExpr; |
| import com.github.javaparser.ast.stmt.Statement; |
| import me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade; |
| import me.tomassetti.symbolsolver.model.declarations.TypeDeclaration; |
| import me.tomassetti.symbolsolver.model.resolution.SymbolReference; |
| import me.tomassetti.symbolsolver.model.resolution.TypeSolver; |
| import me.tomassetti.symbolsolver.model.typesystem.ReferenceTypeUsage; |
| import me.tomassetti.symbolsolver.model.typesystem.TypeUsage; |
| |
| import java.io.File; |
| import java.io.IOException; |
| import java.io.PrintStream; |
| |
| /** |
| * It print information extracted from a source file. It is mainly intended as an example usage of JavaSymbolSolver. |
| */ |
| public class SourceFileInfoExtractor { |
| |
| private TypeSolver typeSolver; |
| |
| private int ok = 0; |
| private int ko = 0; |
| private int unsupported = 0; |
| private boolean printFileName = true; |
| private PrintStream out = System.out; |
| private PrintStream err = System.err; |
| |
| public void setPrintFileName(boolean printFileName) { |
| this.printFileName = printFileName; |
| } |
| |
| public void clear() { |
| ok = 0; |
| ko = 0; |
| unsupported = 0; |
| } |
| |
| public void setOut(PrintStream out) { |
| this.out = out; |
| } |
| |
| public void setErr(PrintStream err) { |
| this.err = err; |
| } |
| |
| public int getOk() { |
| return ok; |
| |
| } |
| |
| public int getUnsupported() { |
| return unsupported; |
| } |
| |
| public int getKo() { |
| return ko; |
| } |
| |
| private void solveTypeDecl(ClassOrInterfaceDeclaration node) { |
| TypeDeclaration typeDeclaration = JavaParserFacade.get(typeSolver).getTypeDeclaration(node); |
| if (typeDeclaration.isClass()) { |
| out.println("\n[ Class " + typeDeclaration.getQualifiedName() + " ]"); |
| for (ReferenceTypeUsage sc : typeDeclaration.asClass().getAllSuperClasses()) { |
| out.println(" superclass: " + sc.getQualifiedName()); |
| } |
| for (TypeDeclaration sc : typeDeclaration.asClass().getAllInterfaces()) { |
| out.println(" interface: " + sc.getQualifiedName()); |
| } |
| } |
| } |
| |
| private void solve(Node node) { |
| if (node instanceof ClassOrInterfaceDeclaration) { |
| solveTypeDecl((ClassOrInterfaceDeclaration) node); |
| } else if (node instanceof Expression) { |
| if ((node.getParentNode() instanceof ImportDeclaration) || (node.getParentNode() instanceof Expression) |
| || (node.getParentNode() instanceof MethodDeclaration) |
| || (node.getParentNode() instanceof PackageDeclaration)) { |
| // skip |
| } else if ((node.getParentNode() instanceof Statement) || (node.getParentNode() instanceof VariableDeclarator)) { |
| try { |
| TypeUsage ref = JavaParserFacade.get(typeSolver).getType(node); |
| out.println(" Line " + node.getRange().begin.line + ") " + node + " ==> " + ref.describe()); |
| ok++; |
| } catch (UnsupportedOperationException upe) { |
| unsupported++; |
| err.println(upe.getMessage()); |
| throw upe; |
| } catch (RuntimeException re) { |
| ko++; |
| err.println(re.getMessage()); |
| throw re; |
| } |
| } |
| } |
| for (Node child : node.getChildrenNodes()) { |
| solve(child); |
| } |
| } |
| |
| private void solveMethodCalls(Node node) { |
| if (node instanceof MethodCallExpr) { |
| out.println(" Line " + node.getBegin().line + ") " + node + " ==> " + toString((MethodCallExpr)node)); |
| } |
| for (Node child : node.getChildrenNodes()) { |
| solveMethodCalls(child); |
| } |
| } |
| |
| private String toString(MethodCallExpr node) { |
| try { |
| return toString(JavaParserFacade.get(typeSolver).solve(node)); |
| } catch (Exception e) { |
| return "ERROR"; |
| } |
| } |
| |
| private String toString(SymbolReference<me.tomassetti.symbolsolver.model.declarations.MethodDeclaration> methodDeclarationSymbolReference) { |
| if (methodDeclarationSymbolReference.isSolved()) { |
| return methodDeclarationSymbolReference.getCorrespondingDeclaration().getQualifiedSignature(); |
| } else { |
| return "UNSOLVED"; |
| } |
| } |
| |
| public void solve(File file) throws IOException, ParseException { |
| if (file.isDirectory()) { |
| for (File f : file.listFiles()) { |
| solve(f); |
| } |
| } else { |
| if (file.getName().endsWith(".java")) { |
| if (printFileName) { |
| out.println("- parsing " + file.getAbsolutePath()); |
| } |
| CompilationUnit cu = JavaParser.parse(file); |
| solve(cu); |
| } |
| } |
| } |
| |
| public void solveMethodCalls(File file) throws IOException, ParseException { |
| if (file.isDirectory()) { |
| for (File f : file.listFiles()) { |
| solveMethodCalls(f); |
| } |
| } else { |
| if (file.getName().endsWith(".java")) { |
| if (printFileName) { |
| out.println("- parsing " + file.getAbsolutePath()); |
| } |
| CompilationUnit cu = JavaParser.parse(file); |
| solveMethodCalls(cu); |
| } |
| } |
| } |
| |
| public void setTypeSolver(TypeSolver typeSolver) { |
| this.typeSolver = typeSolver; |
| } |
| |
| } |