/** * Generates something like * <code>P_INDEX_UPDATER.lazySet(this, newValue)</code> * * @param fieldUpdaterFieldName * @param newValueName * @return */ private static BlockStmt fieldUpdaterLazySet(String fieldUpdaterFieldName, String newValueName) { BlockStmt body = new BlockStmt(); body.addStatement(new ExpressionStmt( methodCallExpr(fieldUpdaterFieldName, "lazySet", new ThisExpr(), new NameExpr(newValueName)))); return body; }
@Override public ThisExpr doMerge(ThisExpr first, ThisExpr second) { ThisExpr te = new ThisExpr(); te.setClassExpr(mergeSingle(first.getClassExpr(),second.getClassExpr())); return te; }
public SymbolReference<ResolvedTypeDeclaration> solve(ThisExpr node){ // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()){ // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()){ return SymbolReference.solved(clazz.getCorrespondingDeclaration()); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.getAncestorOfType(CompilationUnit.class); if (cu.isPresent()){ Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()){ return SymbolReference.solved(getTypeDeclaration(classByName.get())); } } } return SymbolReference.solved(getTypeDeclaration(findContainingTypeDeclOrObjectCreationExpr(node))); }
public SymbolReference<ResolvedTypeDeclaration> solve(ThisExpr node) { // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()) { // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()) { return SymbolReference.solved(clazz.getCorrespondingDeclaration()); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.findAncestor(CompilationUnit.class); if (cu.isPresent()) { Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()) { return SymbolReference.solved(getTypeDeclaration(classByName.get())); } } } return SymbolReference.solved(getTypeDeclaration(findContainingTypeDeclOrObjectCreationExpr(node))); }
@Override public boolean doIsEquals(ThisExpr first, ThisExpr second) { if(!isEqualsUseMerger(first.getClassExpr(),second.getClassExpr())) return false; return true; } }
public SymbolReference<ResolvedTypeDeclaration> solve(ThisExpr node){ // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()){ // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()){ return SymbolReference.solved(clazz.getCorrespondingDeclaration()); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.getAncestorOfType(CompilationUnit.class); if (cu.isPresent()){ Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()){ return SymbolReference.solved(getTypeDeclaration(classByName.get())); } } } return SymbolReference.solved(getTypeDeclaration(findContainingTypeDeclOrObjectCreationExpr(node))); }
@Override public ResolvedType visit(ThisExpr node, Boolean solveLambdas) { // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()) { // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()) { return new ReferenceTypeImpl(clazz.getCorrespondingDeclaration(), typeSolver); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.findAncestor(CompilationUnit.class); if (cu.isPresent()) { Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()) { return new ReferenceTypeImpl(facade.getTypeDeclaration(classByName.get()), typeSolver); } } } return new ReferenceTypeImpl(facade.getTypeDeclaration(facade.findContainingTypeDeclOrObjectCreationExpr(node)), typeSolver); }
return NameRole.REFERENCE; if (whenParentIs(ThisExpr.class, name, (p, c) -> p.getClassExpr().isPresent() && p.getClassExpr().get() == c)) { return NameRole.REFERENCE;
@Override public ResolvedType visit(ThisExpr node, Boolean solveLambdas) { // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()){ // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()){ return new ReferenceTypeImpl(clazz.getCorrespondingDeclaration(),typeSolver); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.getAncestorOfType(CompilationUnit.class); if (cu.isPresent()){ Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()){ return new ReferenceTypeImpl(facade.getTypeDeclaration(classByName.get()), typeSolver); } } } return new ReferenceTypeImpl(facade.getTypeDeclaration(facade.findContainingTypeDeclOrObjectCreationExpr(node)), typeSolver); }
/** * Generates something like * <code>return P_INDEX_UPDATER.getAndSet(this, newValue)</code> * * @param fieldUpdaterFieldName * @param newValueName * @return */ private static BlockStmt fieldUpdaterGetAndSet(String fieldUpdaterFieldName, String newValueName) { BlockStmt body = new BlockStmt(); body.addStatement(new ReturnStmt( methodCallExpr(fieldUpdaterFieldName, "getAndSet", new ThisExpr(), new NameExpr(newValueName)))); return body; }
ne.getClassExpr().isPresent() && ne.getClassExpr().get() == c2) )) { return true; ne.getClassExpr().isPresent() && ne.getClassExpr().get() == c2)) { return true;
@Override public ResolvedType visit(ThisExpr node, Boolean solveLambdas) { // If 'this' is prefixed by a class eg. MyClass.this if (node.getClassExpr().isPresent()){ // Get the class name String className = node.getClassExpr().get().toString(); // Attempt to resolve using a typeSolver SymbolReference<ResolvedReferenceTypeDeclaration> clazz = typeSolver.tryToSolveType(className); if (clazz.isSolved()){ return new ReferenceTypeImpl(clazz.getCorrespondingDeclaration(),typeSolver); } // Attempt to resolve locally in Compilation unit Optional<CompilationUnit> cu = node.getAncestorOfType(CompilationUnit.class); if (cu.isPresent()){ Optional<ClassOrInterfaceDeclaration> classByName = cu.get().getClassByName(className); if (classByName.isPresent()){ return new ReferenceTypeImpl(facade.getTypeDeclaration(classByName.get()), typeSolver); } } } return new ReferenceTypeImpl(facade.getTypeDeclaration(facade.findContainingTypeDeclOrObjectCreationExpr(node)), typeSolver); }
/** * Generates something like * <code>P_INDEX_UPDATER.lazySet(this, newValue)</code> * * @param fieldUpdaterFieldName * @param newValueName * @return */ private static BlockStmt fieldUpdaterLazySet(String fieldUpdaterFieldName, String newValueName) { BlockStmt body = new BlockStmt(); body.addStatement(new ExpressionStmt( methodCallExpr(fieldUpdaterFieldName, "lazySet", new ThisExpr(), new NameExpr(newValueName)))); return body; }
/** * Generates something like * <code>return P_INDEX_UPDATER.compareAndSet(this, expectedValue, newValue)</code> * * @param fieldUpdaterFieldName * @param expectedValueName * @param newValueName * @return */ private static BlockStmt fieldUpdaterCompareAndSet(String fieldUpdaterFieldName, String expectedValueName, String newValueName) { BlockStmt body = new BlockStmt(); body.addStatement(new ReturnStmt(methodCallExpr(fieldUpdaterFieldName, "compareAndSet", new ThisExpr(), new NameExpr(expectedValueName), new NameExpr(newValueName)))); return body; }
/** * Generates something like * <code>return P_INDEX_UPDATER.compareAndSet(this, expectedValue, newValue)</code> * * @param fieldUpdaterFieldName * @param expectedValueName * @param newValueName * @return */ private static BlockStmt fieldUpdaterCompareAndSet(String fieldUpdaterFieldName, String expectedValueName, String newValueName) { BlockStmt body = new BlockStmt(); body.addStatement(new ReturnStmt(methodCallExpr(fieldUpdaterFieldName, "compareAndSet", new ThisExpr(), new NameExpr(expectedValueName), new NameExpr(newValueName)))); return body; }
private MethodDeclaration getterDeclaration(EntityField field) { MethodDeclaration decl = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.createReferenceType(field.getType().getSimpleName(), 0), "get" + CaseConverter.pascalCase(field.getName())); BlockStmt body = new BlockStmt(); body.setStmts( Collections.singletonList( new ReturnStmt( new FieldAccessExpr(new ThisExpr(), field.getName())))); decl.setBody(body); return decl; }
public static void main(String[] args) throws FileNotFoundException { // The directory where we store the examples String pathToExamplesDir = "." + separator + "src" + separator + "main" + separator + "resources"; // Parse the code of an entire source file, a.k.a. a Compilation Unit CompilationUnit compilationUnitNode = JavaParser.parse(new File(pathToExamplesDir + separator + "ASimpleClass.java")); printCompilationUnit("My original class", compilationUnitNode); // Modifying the name of the class compilationUnitNode.getClassByName("ASimpleClass").get() .setName("MyRenamedClass"); printCompilationUnit("Renamed class", compilationUnitNode); // Adding a method: we add a setter MethodDeclaration setter = compilationUnitNode .getClassByName("MyRenamedClass").get() .addMethod("setAField", Modifier.PUBLIC); setter.addParameter("boolean", "aField"); setter.getBody().get().getStatements().add(new ExpressionStmt( new AssignExpr( new FieldAccessExpr(new ThisExpr(),"aField"), new NameExpr("aField"), AssignExpr.Operator.ASSIGN ))); printCompilationUnit("With a setter", compilationUnitNode); } }
private MethodDeclaration setterDeclaration(EntityField field) { MethodDeclaration decl = new MethodDeclaration(ModifierSet.PUBLIC, new VoidType(), "set" + CaseConverter.pascalCase(field.getName()), Collections.singletonList(new Parameter( ASTHelper.createReferenceType(field.getType().getSimpleName(), 0), new VariableDeclaratorId(field.getName())))); BlockStmt body = new BlockStmt(); body.setStmts( Collections.singletonList( new ExpressionStmt( new AssignExpr( new FieldAccessExpr(new ThisExpr(), field.getName()), ASTHelper.createNameExpr(field.getName()), AssignExpr.Operator.assign )))); decl.setBody(body); return decl; }