private void visitJavaFileToPopulateCache(AccessibleObject methodOrConstructor) { InputStream is = null; try { is = javaFileFinder.openJavaFile(methodOrConstructor); if (is != null) { // visit .java file using our custom GenericVisitorAdapter CompilationUnit cu = JavaParser.parse(is); MethodParametersVisitor visitor = new MethodParametersVisitor(); cu.accept(visitor, cache); } } catch (Exception e) { throw new ParameterNamesNotFoundException( "Error while trying to read parameter names from the Java file which contains the declaration of " + methodOrConstructor.toString(), e); } finally { if (is != null) { try { is.close(); } catch (IOException e) { // should never happen } } } }
private CompilationUnit getCompilationUnit(SourceFile sourceFile) { CompilationUnit compilationUnit; if (Files.exists(sourceFile.getOutputPath())) { try { compilationUnit = JavaParser.parse(sourceFile.getOutputPath().toFile()); } catch (Throwable t) { throw new RuntimeException(t); } } else { compilationUnit = new CompilationUnit(); compilationUnit.setComment( new LineComment(" Generated by GraphWalker (http://www.graphwalker.org)")); if (!"".equals(sourceFile.getPackageName())) { compilationUnit.setPackage(createPackageDeclaration(sourceFile)); } compilationUnit.setImports( Arrays.asList( new ImportDeclaration( new NameExpr("org.graphwalker.java.annotation.Model"), false, false), new ImportDeclaration( new NameExpr("org.graphwalker.java.annotation.Vertex"), false, false), new ImportDeclaration( new NameExpr("org.graphwalker.java.annotation.Edge"), false, false))); ASTHelper.addTypeDeclaration(compilationUnit, getInterfaceName(sourceFile)); } return compilationUnit; }
public String generate(SourceFile sourceFile, RuntimeModel model) { CompilationUnit compilationUnit = getCompilationUnit(sourceFile); ChangeContext changeContext = new ChangeContext(model); visit(compilationUnit, changeContext); removeMethods(compilationUnit, changeContext); generateMethods(compilationUnit, changeContext); return compilationUnit.toString(); }
/** * Adds the given type declaration to the compilation unit. The list of types will be initialized * if it is <code>null</code>. * * @param cu compilation unit * @param type type declaration */ public static void addTypeDeclaration(CompilationUnit cu, TypeDeclaration type) { List<TypeDeclaration> types = cu.getTypes(); if (types == null) { types = new ArrayList<TypeDeclaration>(); cu.setTypes(types); } types.add(type); }
@Test public void tesCompilationUnitNotEqual() throws Exception { String source = Helper.readClass("DumperTestClass"); CompilationUnit cu1 = Helper.parserString(source); CompilationUnit cu2 = Helper.parserString(source); cu2.setPackage(new PackageDeclaration(new NameExpr("diff_package"))); assertNotEqualsAndHashCode(cu1, cu2); }
private void removeMethods(CompilationUnit compilationUnit, ChangeContext changeContext) { if (0 < changeContext.getMethodDeclarations().size()) { ClassOrInterfaceDeclaration body = (ClassOrInterfaceDeclaration) compilationUnit.getTypes().get(0); body.getMembers().removeAll(changeContext.getMethodDeclarations()); } }
/** Get every attributes this class has. But no protected attributes. */ private void getAttributes() { if (cUnit == null) { return; } // Initialize parameters that building a new attribute instance needs ClassAttribute attribute = null; String attributeName = ""; String accessModifier = ""; String attributeType = ""; for (TypeDeclaration typeDeclaration : cUnit.getTypes()) { List<BodyDeclaration> memebers = typeDeclaration.getMembers(); if (memebers == null) { return; } for (BodyDeclaration bodyDeclaration : memebers) { FieldDeclaration fieldDeclaration; try { // Conversion here may cause exception fieldDeclaration = (FieldDeclaration) bodyDeclaration; accessModifier = GetModifier(fieldDeclaration.getModifiers()); if (accessModifier == "+" || accessModifier == "-") { attributeName = fieldDeclaration.getVariables().get(0).getId().getName(); attributeType = fieldDeclaration.getType().toString(); attribute = new ClassAttribute(attributeName, accessModifier, attributeType); this.classAttributes.add(attribute); } } catch (Exception e) { } } } }
/** Get constructors in this class */ private void getConstructor() { if (cUnit == null) { return; } ConstructorDeclaration constructorDeclaration; String constructorModifier = ""; ArrayList<Parameter> constructorParameters = null; String constructorName = ""; for (TypeDeclaration typeDeclaration : cUnit.getTypes()) { List<BodyDeclaration> memebers = typeDeclaration.getMembers(); if (memebers == null) { return; } for (BodyDeclaration bodyDeclaration : memebers) { try { constructorDeclaration = (ConstructorDeclaration) bodyDeclaration; constructorModifier = GetModifier(constructorDeclaration.getModifiers()); if (constructorModifier == "+") { constructorParameters = new ArrayList<Parameter>(); if (constructorDeclaration.getParameters() != null && constructorDeclaration.getParameters().size() != 0) { for (Parameter parameter : constructorDeclaration.getParameters()) { constructorParameters.add(parameter); } } constructorName = constructorDeclaration.getName(); classMethods.add(new ClassMethod(constructorParameters, "void", constructorName, "+")); } } catch (Exception e) { // handle exception } } } }
public CompilationUnitBuilder startType(String className) { TypeDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, className); curType = type; curType.setMembers(new ArrayList<BodyDeclaration>()); cu.getTypes().add(type); return this; }
/** * Looking for dependencies in method's body * * @param allClasses */ public void findDependBody(ArrayList<RealClass> allClasses) { // Every possible class has to be considered // Therefore all classes passed in HashSet<String> allClassName = new HashSet<String>(); for (RealClass realClass : allClasses) { allClassName.add(realClass.getClassName()); } for (TypeDeclaration typeDeclaration : cUnit.getTypes()) { List<BodyDeclaration> bodyDeclarations = typeDeclaration.getMembers(); try { MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclarations.get(0); if (methodDeclaration.getName().contentEquals("main")) { // Block here means all lines inside {} BlockStmt blockStmt = methodDeclaration.getBody(); List<Statement> statements = blockStmt.getStmts(); for (Statement statement : statements) { String stmtLine = statement.toString(); // Only care about dependencies, therefore no need for scanning whole line // Just looking at strings before "=" if (stmtLine.contains("=")) { stmtLine = stmtLine.substring(0, stmtLine.indexOf('=')); String variable = stmtLine.substring(0, stmtLine.indexOf(' ')); if (allClassName.contains(variable)) { ClassRelation relation = new ClassRelation(this.getClassName(), variable, RelatinType.DEPENDENCIES); classRelations.add(relation); } } } } } catch (Exception e) { // TODO: handle exception } } }
public CompilationUnit getResult() { List<ImportDeclaration> importDecls = new ArrayList<ImportDeclaration>(); for (String type : imports) { ImportDeclaration importDecl = new ImportDeclaration(); importDecl.setName(new NameExpr(type)); importDecls.add(importDecl); } cu.setImports(importDecls); return cu; }
/** Get only public class method in this class */ private void getMethods() { if (cUnit == null) { return; } String methodReturnType; String methodName; String methodModifier; MethodDeclaration methodDeclaration; ArrayList<Parameter> parametersList = null; for (TypeDeclaration typeDeclaration : cUnit.getTypes()) { List<BodyDeclaration> memebers = typeDeclaration.getMembers(); if (memebers == null) { return; } for (BodyDeclaration bodyDeclaration : memebers) { try { methodDeclaration = (MethodDeclaration) bodyDeclaration; methodModifier = GetModifier(methodDeclaration.getModifiers()); if (methodModifier == "+") { methodReturnType = methodDeclaration.getType().toString(); methodName = methodDeclaration.getName().toString(); try { // Fetch each method's parameters if (methodDeclaration.getParameters() != null && methodDeclaration.getParameters().size() != 0) { parametersList = new ArrayList<Parameter>(); for (Parameter parameter : methodDeclaration.getParameters()) { parametersList.add(parameter); } } } catch (Exception e) { } classMethods.add( new ClassMethod(parametersList, methodReturnType, methodName, methodModifier)); parametersList = null; } } catch (Exception e) { } } } }
public void visit(CompilationUnit n, A arg) { if (n.getPackage() != null) { n.getPackage().accept(this, arg); } if (n.getImports() != null) { for (ImportDeclaration i : n.getImports()) { i.accept(this, arg); } } if (n.getTypes() != null) { for (TypeDeclaration typeDeclaration : n.getTypes()) { typeDeclaration.accept(this, arg); } } }
public void visit(CompilationUnit n, Object arg) { if (n.getPackage() != null) { n.getPackage().accept(this, arg); } if (n.getImports() != null) { for (ImportDeclaration i : n.getImports()) { i.accept(this, arg); } } if (n.getTypes() != null) { for (Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext(); ) { i.next().accept(this, arg); if (i.hasNext()) {} } } }
private void generateMethods(CompilationUnit compilationUnit, ChangeContext changeContext) { ClassOrInterfaceDeclaration body = (ClassOrInterfaceDeclaration) compilationUnit.getTypes().get(0); for (String methodName : changeContext.getMethodNames()) { if (isValidName(methodName)) { MethodDeclaration method = new MethodDeclaration(Modifier.INTERFACE, ASTHelper.VOID_TYPE, methodName); List<AnnotationExpr> annotations = new ArrayList<>(); if (changeContext.isVertex(methodName)) { List<MemberValuePair> memberValuePairs = new ArrayList<>(); annotations.add( new NormalAnnotationExpr(ASTHelper.createNameExpr("Vertex"), memberValuePairs)); } else { List<MemberValuePair> memberValuePairs = new ArrayList<>(); annotations.add( new NormalAnnotationExpr(ASTHelper.createNameExpr("Edge"), memberValuePairs)); } method.setAnnotations(annotations); ASTHelper.addMember(body, method); } } }
public CompilationUnitBuilder pakage(String pkgName) { NameExpr name = new NameExpr(pkgName); PackageDeclaration pakage = new PackageDeclaration(name); cu.setPackage(pakage); return this; }
public CompilationUnitBuilder() { cu = new CompilationUnit(); imports = new HashSet<String>(); cu.setTypes(new ArrayList<TypeDeclaration>()); }
private static ClassOrInterfaceDeclaration getMainType(CompilationUnit compilationUnit) { return (ClassOrInterfaceDeclaration) compilationUnit.getTypes().get(0); }