public void visit(ClassOrInterfaceDeclaration n, A arg) { if (n.getJavaDoc() != null) { n.getJavaDoc().accept(this, arg); } if (n.getAnnotations() != null) { for (AnnotationExpr a : n.getAnnotations()) { a.accept(this, arg); } } if (n.getTypeParameters() != null) { for (TypeParameter t : n.getTypeParameters()) { t.accept(this, arg); } } if (n.getExtends() != null) { for (ClassOrInterfaceType c : n.getExtends()) { c.accept(this, arg); } } if (n.getImplements() != null) { for (ClassOrInterfaceType c : n.getImplements()) { c.accept(this, arg); } } if (n.getMembers() != null) { for (BodyDeclaration member : n.getMembers()) { member.accept(this, arg); } } }
@Test public void testRpcSample() throws Exception { CompilationUnit compilationUnit = JavaParser.parse(getClass().getResourceAsStream("testRpcSample.java.resource")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ClassOrInterfaceDeclaration mainType = getMainType(compilationUnit); rpcPojosAjGenerator.generateEntity(compilationUnit, mainType, baos); byte[] expectedBytes = IOUtils.toByteArray(getClass().getResourceAsStream("rpcSample_Aspect.aj.expected")); AssertUtils.assertContent(expectedBytes, baos.toByteArray()); List<BodyDeclaration> members = mainType.getMembers(); for (BodyDeclaration bodyDeclaration : members) { // look for inner classes baos = new ByteArrayOutputStream(); if (bodyDeclaration instanceof ClassOrInterfaceDeclaration) { rpcPojosAjGenerator.generatePart( compilationUnit, (ClassOrInterfaceDeclaration) bodyDeclaration, baos, mainType.getName()); String expected = ((ClassOrInterfaceDeclaration) bodyDeclaration).getName() + "_Aspect.aj.expected"; expectedBytes = IOUtils.toByteArray(getClass().getResourceAsStream(expected)); AssertUtils.assertContent(expectedBytes, baos.toByteArray()); } } }
private void removeMethods(CompilationUnit compilationUnit, ChangeContext changeContext) { if (0 < changeContext.getMethodDeclarations().size()) { ClassOrInterfaceDeclaration body = (ClassOrInterfaceDeclaration) compilationUnit.getTypes().get(0); body.getMembers().removeAll(changeContext.getMethodDeclarations()); } }
public void visit(ClassOrInterfaceDeclaration n, Object arg) { if (n.getJavaDoc() != null) { n.getJavaDoc().accept(this, arg); } printMemberAnnotations(n.getAnnotations(), arg); printModifiers(n.getModifiers()); if (n.isInterface()) { } else { } printTypeParameters(n.getTypeParameters(), arg); Scope currentScope = (Scope) n.getData(); ClassSymbol currentClass = (ClassSymbol) currentScope.resolve(n.getName()); if (n.getExtends() != null) { for (Iterator<ClassOrInterfaceType> i = n.getExtends().iterator(); i.hasNext(); ) { ClassOrInterfaceType c = i.next(); c.accept(this, arg); ClassSymbol parentClass = (ClassSymbol) currentScope.resolve((c.getName())); currentClass.setInheritedScope(parentClass); if (i.hasNext()) {} } } if (n.getImplements() != null) { for (Iterator<ClassOrInterfaceType> i = n.getImplements().iterator(); i.hasNext(); ) { ClassOrInterfaceType c = i.next(); c.accept(this, arg); if (i.hasNext()) {} } } if (n.getMembers() != null) { printMembers(n.getMembers(), arg); } }
@Test public void testRemoveAnnotation() throws Exception { CompilerAssert<File, File> helper = incrementalCompiler("model", "meta", "controller"); helper.assertCompile(); File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser"); MetaModel mm = Tools.unserialize(MetaModel.class, ser); mm.getQueue().clear(); Tools.serialize(mm, ser); // JavaFile file = helper.assertJavaFile("model", "meta", "controller", "A.java"); ClassOrInterfaceDeclaration a = file.assertDeclaration(); MethodDeclaration decl = (MethodDeclaration) a.getMembers().get(0); decl.getAnnotations().clear(); file.assertSave(); // helper.addClassPath(helper.getClassOutput()).assertCompile(); mm = Tools.unserialize(MetaModel.class, ser); // JSON expected = json() .set( "applications", json() .list( "values", json() .list("controllers") .set("fqn", "model.meta.controller.ControllerApplication") .set("handle", "ElementHandle.Package[qn=model.meta.controller]") .list("templates"))); assertEquals(expected, mm.toJSON()); // List<MetaModelEvent> events = mm.getQueue().clear(); assertEquals(2, events.size()); assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType()); assertInstanceOf(MethodMetaModel.class, events.get(0).getObject()); assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(1).getType()); assertInstanceOf(ControllerMetaModel.class, events.get(1).getObject()); }
@Test public void testRemoveOverloadedMethod() throws Exception { CompilerAssert<File, File> helper = incrementalCompiler("model", "meta", "controller"); JavaFile file = helper.assertJavaFile("model", "meta", "controller", "A.java"); ClassOrInterfaceDeclaration a = file.assertDeclaration(); MethodDeclaration index = new MethodDeclaration(Modifier.PUBLIC, ASTHelper.VOID_TYPE, "index"); ASTHelper.addParameter( index, ASTHelper.createParameter(new ClassOrInterfaceType(String.class.getName()), "s")); index.setAnnotations( Collections.<AnnotationExpr>singletonList( new NormalAnnotationExpr( ASTHelper.createNameExpr(View.class.getName()), Collections.<MemberValuePair>emptyList()))); index.setBody(new BlockStmt()); a.getMembers().add(index); file.assertSave(); helper.assertCompile(); // File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser"); MetaModel mm = Tools.unserialize(MetaModel.class, ser); mm.getQueue().clear(); Tools.serialize(mm, ser); // assertTrue(a.getMembers().remove(index)); file.assertSave(); // helper.addClassPath(helper.getClassOutput()).assertCompile(); mm = Tools.unserialize(MetaModel.class, ser); // JSON expected = json() .set( "applications", json() .list( "values", json() .list( "controllers", json() .set( "handle", "ElementHandle.Class[fqn=model.meta.controller.A]") .list( "methods", json() .set( "handle", "ElementHandle.Method[fqn=model.meta.controller.A,name=index,parameterTypes[]]") .set("id", null) .set("name", "index") .list("parameters") .set("phase", "RENDER"))) .set("fqn", "model.meta.controller.ControllerApplication") .set("handle", "ElementHandle.Package[qn=model.meta.controller]") .list("templates"))); assertEquals(expected, mm.toJSON()); // List<MetaModelEvent> events = mm.getQueue().clear(); assertEquals(1, events.size()); assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType()); assertInstanceOf(MethodMetaModel.class, events.get(0).getObject()); }
@Test public void testAddMethod() throws Exception { CompilerAssert<File, File> helper = incrementalCompiler("model", "meta", "controller"); JavaFile file = helper.assertJavaFile("model", "meta", "controller", "A.java"); ClassOrInterfaceDeclaration a = file.assertDeclaration(); MethodDeclaration decl = (MethodDeclaration) a.getMembers().get(0); assertTrue(a.getMembers().remove(decl)); file.assertSave(); helper.assertCompile(); // File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser"); MetaModel mm = Tools.unserialize(MetaModel.class, ser); List<MetaModelEvent> events = mm.getQueue().clear(); assertEquals(1, events.size()); assertEquals(MetaModelEvent.AFTER_ADD, events.get(0).getType()); assertTrue(events.get(0).getObject() instanceof ApplicationMetaModel); Tools.serialize(mm, ser); // a.getMembers().add(decl); file.assertSave(); helper.addClassPath(helper.getClassOutput()).assertCompile(); // mm = Tools.unserialize(MetaModel.class, ser); // JSON expected = json() .set( "applications", json() .list( "values", json() .list( "controllers", json() .set( "handle", "ElementHandle.Class[fqn=model.meta.controller.A]") .list( "methods", json() .set( "handle", "ElementHandle.Method[fqn=model.meta.controller.A,name=index,parameterTypes[]]") .set("id", null) .set("name", "index") .list("parameters") .set("phase", "RENDER"))) .set("fqn", "model.meta.controller.ControllerApplication") .set("handle", "ElementHandle.Package[qn=model.meta.controller]") .list("templates"))); assertEquals(expected, mm.toJSON()); // events = mm.getQueue().clear(); assertEquals(3, events.size()); assertEquals(MetaModelEvent.AFTER_ADD, events.get(0).getType()); assertInstanceOf(ControllerMetaModel.class, events.get(0).getObject()); assertEquals(MetaModelEvent.AFTER_ADD, events.get(1).getType()); assertInstanceOf(MethodMetaModel.class, events.get(1).getObject()); assertEquals(MetaModelEvent.UPDATED, events.get(2).getType()); assertInstanceOf(ControllerMetaModel.class, events.get(2).getObject()); }
@Test public void testChangeAnnotation() throws Exception { CompilerAssert<File, File> helper = incrementalCompiler("model", "meta", "controller"); helper.assertCompile(); File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser"); MetaModel mm = Tools.unserialize(MetaModel.class, ser); mm.getQueue().clear(); Tools.serialize(mm, ser); // JavaFile file = helper.assertJavaFile("model", "meta", "controller", "A.java"); ClassOrInterfaceDeclaration a = file.assertDeclaration(); MethodDeclaration decl = (MethodDeclaration) a.getMembers().get(0); decl.getAnnotations().get(0).setName(ASTHelper.createNameExpr(Action.class.getName())); file.assertSave(); // helper.addClassPath(helper.getClassOutput()).assertCompile(); mm = Tools.unserialize(MetaModel.class, ser); // JSON expected = json() .set( "applications", json() .list( "values", json() .list( "controllers", json() .set( "handle", "ElementHandle.Class[fqn=model.meta.controller.A]") .list( "methods", json() .set( "handle", "ElementHandle.Method[fqn=model.meta.controller.A,name=index,parameterTypes[]]") .set("id", null) .set("name", "index") .list("parameters") .set("phase", "ACTION"))) .set("fqn", "model.meta.controller.ControllerApplication") .set("handle", "ElementHandle.Package[qn=model.meta.controller]") .list("templates"))); assertEquals(expected, mm.toJSON()); // List<MetaModelEvent> events = mm.getQueue().clear(); assertEquals(5, events.size()); assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType()); assertInstanceOf(MethodMetaModel.class, events.get(0).getObject()); assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(1).getType()); assertInstanceOf(ControllerMetaModel.class, events.get(1).getObject()); assertEquals(MetaModelEvent.AFTER_ADD, events.get(2).getType()); assertInstanceOf(ControllerMetaModel.class, events.get(2).getObject()); assertEquals(MetaModelEvent.AFTER_ADD, events.get(3).getType()); assertInstanceOf(MethodMetaModel.class, events.get(3).getObject()); assertEquals(MetaModelEvent.UPDATED, events.get(4).getType()); assertInstanceOf(ControllerMetaModel.class, events.get(4).getObject()); }