td.setName(ast.newSimpleName("Foo")); TypeParameter tp = ast.newTypeParameter(); tp.setName(ast.newSimpleName("X"));
List types = cu.types(); TypeDeclaration typeDec = (TypeDeclaration) types.get(0); //typeDec is the class System.out.println("className:" + typeDec.getName()); SimpleName sn = typeDec.getName(); sn.setIdentifier("aqaa"); typeDec.setName(sn);
private TypeDeclaration createViewHolderItemClass( FindViewByIdVisitor findViewByIdVisitor, SimpleName typeName, TypeNameDecider typeNameDecider) { final ASTBuilder b = this.ctx.getASTBuilder(); TypeDeclaration result = b.getAST().newTypeDeclaration(); modifiers(result).addAll(asList(b.private0(), b.static0())); result.setName(typeName); List<BodyDeclaration> viewItemsFieldDecls = bodyDeclarations(result); for (FindViewByIdVisitor.FindViewByIdItem item : findViewByIdVisitor.items) { viewItemsFieldDecls.add(item.toFieldDecl(b, typeNameDecider)); } return result; }
private static TypeDeclaration createType(final AST ast, final CompilationUnit cu, final Consumer<TypeDeclaration> typeCustomizer, final TypeInformation type) { final TypeDeclaration td = ast.newTypeDeclaration(); makePublic(td); if (typeCustomizer != null) { typeCustomizer.accept(td); } addJavadoc(ast, type, td); td.setName(ast.newSimpleName(type.getName())); return td; }
public TypeDeclaration createClassDeclaration(ICompilationUnit unit, String declaringType, CompilationUnitRewrite cuRewrite) throws CoreException { AST ast= cuRewrite.getAST(); TypeDeclaration typeDeclaration= ast.newTypeDeclaration(); typeDeclaration.setName(ast.newSimpleName(fClassName)); List body= typeDeclaration.bodyDeclarations(); for (Iterator iter= fVariables.iterator(); iter.hasNext();) { ParameterInfo pi= (ParameterInfo) iter.next(); if (isValidField(pi)) { FieldDeclaration declaration= createField(pi, unit, cuRewrite); body.add(declaration); } } MethodDeclaration constructor= createConstructor(unit, declaringType, cuRewrite); body.add(constructor); for (Iterator iter= fVariables.iterator(); iter.hasNext();) { ParameterInfo pi= (ParameterInfo) iter.next(); if (fCreateGetter && isValidField(pi)) { ASTNode getter= createGetter(pi, declaringType, unit, cuRewrite); body.add(getter); } if (fCreateSetter && isValidField(pi)) { if (!Modifier.isFinal(pi.getOldBinding().getModifiers())) { ASTNode setter= createSetter(pi, declaringType, unit, cuRewrite); body.add(setter); } } } return typeDeclaration; }
/** * Generate the Exception Class. * * @param clazz * the UML class * @param ast * the JDT Java AST * @param cu * the generated Java compilation unit * @return TypeDeclaration JDT */ @SuppressWarnings("unchecked") public TypeDeclaration generateClass(Classifier clazz, AST ast, CompilationUnit cu) { String className = getClassName(clazz); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(false); td.modifiers().add( ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); td.setName(ast.newSimpleName(className)); // Add inheritance generateClassInheritance(clazz, ast, td); // Add template params generateClassTemplateParams(clazz, ast, td); cu.types().add(td); return td; }
@Test public void testGenerateConstructorWithParams() { AST ast = AST.newAST(AST.JLS3); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("CompanyException")); when(clazz.getName()).thenReturn("CompanyException"); exceptionGenerator.generateConstructorWithParams(clazz, ast, td, new String[] { "String", "Throwable" }, new String[] { "message", "cause" }); assertEquals( "class CompanyException {\n" + " public CompanyException( String message, Throwable cause){\n" + " super(message,cause);\n }\n" + "}\n", td.toString()); } }
@Test public void testGenerateConstructors() { AST ast = AST.newAST(AST.JLS3); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("CompanyException")); when(clazz.getName()).thenReturn("CompanyException"); exceptionGenerator.generateConstructors(clazz, ast, td); assertEquals( "class CompanyException {\n public CompanyException(){\n }\n" + " public CompanyException( Throwable cause){\n" + " super(cause);\n }\n public CompanyException( String message){\n" + " super(message);\n }\n" + " public CompanyException( String message, Throwable cause){\n" + " super(message,cause);\n }\n" + "}\n", td.toString()); }
@SuppressWarnings("unchecked") @Test public void testGenerateClassWithTemplate() { TemplateSignature templateSignature = mock(TemplateSignature.class); EList<TemplateParameter> templateParameters = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get()); when(clazz.getOwnedTemplateSignature()).thenReturn(templateSignature); when(templateSignature.getParameters()).thenReturn(templateParameters); AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast .newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); TypeDeclaration typeDeclaration = interfaceGenerator.generateClass( clazz, ast, cu); assertEquals(typeDeclaration.toString(), td.toString()); }
ASTNode clone0(AST target) { TypeDeclaration result = new TypeDeclaration(target); result.setSourceRange(getStartPosition(), getLength()); result.setJavadoc( (Javadoc) ASTNode.copySubtree(target, getJavadoc())); if (this.ast.apiLevel == AST.JLS2_INTERNAL) { result.internalSetModifiers(getModifiers()); result.setSuperclass( (Name) ASTNode.copySubtree(target, getSuperclass())); result.superInterfaces().addAll( ASTNode.copySubtrees(target, superInterfaces())); } result.setInterface(isInterface()); result.setName((SimpleName) getName().clone(target)); if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); result.typeParameters().addAll( ASTNode.copySubtrees(target, typeParameters())); result.setSuperclassType( (Type) ASTNode.copySubtree(target, getSuperclassType())); result.superInterfaceTypes().addAll( ASTNode.copySubtrees(target, superInterfaceTypes())); } result.bodyDeclarations().addAll( ASTNode.copySubtrees(target, bodyDeclarations())); return result; }
@Override ASTNode clone0(AST target) { TypeDeclaration result = new TypeDeclaration(target); result.setSourceRange(getStartPosition(), getLength()); result.setJavadoc( (Javadoc) ASTNode.copySubtree(target, getJavadoc())); if (this.ast.apiLevel == AST.JLS2_INTERNAL) { result.internalSetModifiers(getModifiers()); result.setSuperclass( (Name) ASTNode.copySubtree(target, getSuperclass())); result.superInterfaces().addAll( ASTNode.copySubtrees(target, superInterfaces())); } result.setInterface(isInterface()); result.setName((SimpleName) getName().clone(target)); if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); result.typeParameters().addAll( ASTNode.copySubtrees(target, typeParameters())); result.setSuperclassType( (Type) ASTNode.copySubtree(target, getSuperclassType())); result.superInterfaceTypes().addAll( ASTNode.copySubtrees(target, superInterfaceTypes())); } result.bodyDeclarations().addAll( ASTNode.copySubtrees(target, bodyDeclarations())); return result; }
ASTNode clone0(AST target) { TypeDeclaration result = new TypeDeclaration(target); result.setSourceRange(getStartPosition(), getLength()); result.setJavadoc( (Javadoc) ASTNode.copySubtree(target, getJavadoc())); if (this.ast.apiLevel == AST.JLS2_INTERNAL) { result.internalSetModifiers(getModifiers()); result.setSuperclass( (Name) ASTNode.copySubtree(target, getSuperclass())); result.superInterfaces().addAll( ASTNode.copySubtrees(target, superInterfaces())); } result.setInterface(isInterface()); result.setName((SimpleName) getName().clone(target)); if (this.ast.apiLevel >= AST.JLS3_INTERNAL) { result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); result.typeParameters().addAll( ASTNode.copySubtrees(target, typeParameters())); result.setSuperclassType( (Type) ASTNode.copySubtree(target, getSuperclassType())); result.superInterfaceTypes().addAll( ASTNode.copySubtrees(target, superInterfaceTypes())); } result.bodyDeclarations().addAll( ASTNode.copySubtrees(target, bodyDeclarations())); return result; }
ASTNode clone0(AST target) { TypeDeclaration result = new TypeDeclaration(target); result.setSourceRange(getStartPosition(), getLength()); result.setJavadoc( (Javadoc) ASTNode.copySubtree(target, getJavadoc())); if (this.ast.apiLevel == AST.JLS2_INTERNAL) { result.internalSetModifiers(getModifiers()); result.setSuperclass( (Name) ASTNode.copySubtree(target, getSuperclass())); result.superInterfaces().addAll( ASTNode.copySubtrees(target, superInterfaces())); } result.setInterface(isInterface()); result.setName((SimpleName) getName().clone(target)); if (this.ast.apiLevel >= AST.JLS3) { result.modifiers().addAll(ASTNode.copySubtrees(target, modifiers())); result.typeParameters().addAll( ASTNode.copySubtrees(target, typeParameters())); result.setSuperclassType( (Type) ASTNode.copySubtree(target, getSuperclassType())); result.superInterfaceTypes().addAll( ASTNode.copySubtrees(target, superInterfaceTypes())); } result.bodyDeclarations().addAll( ASTNode.copySubtrees(target, bodyDeclarations())); return result; }
@SuppressWarnings("unchecked") @Test public void testGenerateAssociationEndUpperCardinalityMultiplesIsUnique() { AST ast = AST.newAST(AST.JLS3); ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast .newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); Property property = mock(Property.class); MethodDeclaration mdGetter = ast.newMethodDeclaration(); mdGetter.setName(ast.newSimpleName("getCompanies")); String umlQualifiedTypeName = "test.de.Company"; String umlTypeName = "Company"; when(property.isOrdered()).thenReturn(false); when(property.isUnique()).thenReturn(true); interfaceGenerator.generateAssociationEndUpperCardinalityMultiples(ast, td, property, mdGetter, umlTypeName, umlQualifiedTypeName); assertEquals("java.util.Set<test.de.Company>", mdGetter .getReturnType2().toString()); }
@SuppressWarnings("unchecked") @Test public void testGenerateAssociationEndUpperCardinalityMultiplesIsUniqueAndIsOrdered() { AST ast = AST.newAST(AST.JLS3); ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast .newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); Property property = mock(Property.class); MethodDeclaration mdGetter = ast.newMethodDeclaration(); mdGetter.setName(ast.newSimpleName("getCompanies")); String umlQualifiedTypeName = "test.de.Company"; String umlTypeName = "Company"; when(property.isOrdered()).thenReturn(true); when(property.isUnique()).thenReturn(true); interfaceGenerator.generateAssociationEndUpperCardinalityMultiples(ast, td, property, mdGetter, umlTypeName, umlQualifiedTypeName); assertEquals("java.util.SortedSet<test.de.Company>", mdGetter .getReturnType2().toString()); }
@SuppressWarnings("unchecked") @Test public void testGenerateAssociationEndUpperCardinalityMultiplesIsOrdered() { AST ast = AST.newAST(AST.JLS3); ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast .newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); Property property = mock(Property.class); MethodDeclaration mdGetter = ast.newMethodDeclaration(); mdGetter.setName(ast.newSimpleName("getCompanies")); String umlQualifiedTypeName = "test.de.Company"; String umlTypeName = "Company"; when(property.isOrdered()).thenReturn(true); when(property.isUnique()).thenReturn(false); interfaceGenerator.generateAssociationEndUpperCardinalityMultiples(ast, td, property, mdGetter, umlTypeName, umlQualifiedTypeName); assertEquals("java.util.List<test.de.Company>", mdGetter .getReturnType2().toString()); }
@SuppressWarnings("unchecked") @Test public void testGenerateAssociationEndUpperCardinalityMultiplesDefault() { AST ast = AST.newAST(AST.JLS3); ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast .newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); Property property = mock(Property.class); MethodDeclaration mdGetter = ast.newMethodDeclaration(); mdGetter.setName(ast.newSimpleName("getCompanies")); String umlQualifiedTypeName = "test.de.Company"; String umlTypeName = "Company"; when(property.isOrdered()).thenReturn(false); when(property.isUnique()).thenReturn(false); interfaceGenerator.generateAssociationEndUpperCardinalityMultiples(ast, td, property, mdGetter, umlTypeName, umlQualifiedTypeName); assertEquals("java.util.Collection<test.de.Company>", mdGetter .getReturnType2().toString()); }
@Test public void testGenerateSerialVersionUID() { AST ast = AST.newAST(AST.JLS3); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("Company")); exceptionGenerator.generateSerialVersionUID(clazz, ast, td); assertEquals( "class Company {\n private static final long serialVersionUID=1L;\n}\n", td.toString()); }
@SuppressWarnings("unchecked") @Test public void testGenerateClass() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); TypeDeclaration td = ast.newTypeDeclaration(); td.setInterface(true); Modifier modifier = ast .newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); td.modifiers().add(modifier); td.setName(ast.newSimpleName("Company")); TypeDeclaration typeDeclaration = interfaceGenerator.generateClass( clazz, ast, cu); assertEquals(typeDeclaration.toString(), td.toString()); }
AST ast = AST.newAST(AST.JLS3); CompilationUnit unit = ast.newCompilationUnit(); PackageDeclaration packageDeclaration = ast.newPackageDeclaration(); packageDeclaration.setName(ast.newSimpleName("example")); unit.setPackage(packageDeclaration); ImportDeclaration importDeclaration = ast.newImportDeclaration(); QualifiedName name = ast.newQualifiedName( ast.newSimpleName("java"), ast.newSimpleName("util")); importDeclaration.setName(name); importDeclaration.setOnDemand(true); unit.imports().add(importDeclaration); TypeDeclaration type = ast.newTypeDeclaration(); type.setInterface(false); type.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); type.setName(ast.newSimpleName("HelloWorld")); // ....