List of usage examples for org.eclipse.jdt.core.dom AST newTypeDeclaration
public TypeDeclaration newTypeDeclaration()
From source file:br.com.objectos.way.core.code.jdt.ASTFake.java
License:Apache License
public static TypeDeclaration newTypeDeclaration() { AST ast = AST.newAST(AST.JLS8); return ast.newTypeDeclaration(); }
From source file:br.com.objectos.way.core.code.jdt.ASTTest.java
License:Apache License
@SuppressWarnings("unchecked") public void stackoverflow_answer() { AST ast = AST.newAST(AST.JLS8); CompilationUnit cu = ast.newCompilationUnit(); PackageDeclaration p1 = ast.newPackageDeclaration(); p1.setName(ast.newSimpleName("foo")); cu.setPackage(p1);//w ww. j av a2 s . c o m ImportDeclaration id = ast.newImportDeclaration(); id.setName(ast.newName(new String[] { "java", "util", "Set" })); cu.imports().add(id); TypeDeclaration td = ast.newTypeDeclaration(); td.setName(ast.newSimpleName("Foo")); TypeParameter tp = ast.newTypeParameter(); tp.setName(ast.newSimpleName("X")); td.typeParameters().add(tp); cu.types().add(td); MethodDeclaration md = ast.newMethodDeclaration(); md.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); md.setName(ast.newSimpleName("bar")); SingleVariableDeclaration var = ast.newSingleVariableDeclaration(); var.setType(ast.newSimpleType(ast.newSimpleName("String"))); var.setName(ast.newSimpleName("a")); md.parameters().add(var); td.bodyDeclarations().add(md); Block block = ast.newBlock(); md.setBody(block); MethodInvocation mi = ast.newMethodInvocation(); mi.setName(ast.newSimpleName("x")); ExpressionStatement e = ast.newExpressionStatement(mi); block.statements().add(e); System.out.println(cu); }
From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaTypeModelAdapter.java
License:Open Source License
public static Object createCorrespondingModelElement(AST ast, CodeSyncElement cse) { ASTNode child = null;//w ww . j a va 2 s.c o m if (JavaAttributeModelAdapter.ATTRIBUTE.equals(cse.getType())) { VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment(); FieldDeclaration field = ast.newFieldDeclaration(fragment); child = field; } if (JavaOperationModelAdapter.OPERATION.equals(cse.getType())) { child = ast.newMethodDeclaration(); } if (CLASS.equals(cse.getType())) { child = ast.newTypeDeclaration(); } if (INTERFACE.equals(cse.getType())) { TypeDeclaration type = ast.newTypeDeclaration(); type.setInterface(true); child = type; } if (ENUM.equals(cse.getType())) { child = ast.newEnumDeclaration(); } if (ANNOTATION.equals(cse.getType())) { child = ast.newAnnotationTypeDeclaration(); } return child; }
From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.forward.ForwardJavaSrcDir_Files.java
License:Open Source License
/** * @flowerModelElementId _IEIN7-rfEd6QaI9daHlzjA *///from w w w. j av a2 s .com @SuppressWarnings("unchecked") @Override protected CompilationUnit createAndAddNewASTElement(Type modelElement, IContainer parentAstElement) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource("".toCharArray()); CompilationUnit compUnit = (CompilationUnit) parser.createAST(null); AST ast = compUnit.getAST(); compUnit.recordModifications(); TypeDeclaration typeDeclaration = ast.newTypeDeclaration(); typeDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); compUnit.types().add(typeDeclaration); if (modelElement instanceof Class) typeDeclaration.setInterface(false); else if (modelElement instanceof Interface) typeDeclaration.setInterface(true); else throw new IllegalArgumentException("could not recognize instance of " + modelElement); return compUnit; }
From source file:com.google.devtools.j2cpp.translate.AnonymousClassConverter.java
License:Open Source License
/** * Convert the anonymous class into an inner class. Fields are added for * final variables that are referenced, and a constructor is added. * * Note: endVisit is used for a depth-first traversal, to make it easier * to scan their containing nodes for references. *//* w w w .j ava 2 s . c om*/ @Override @SuppressWarnings("unchecked") public void endVisit(AnonymousClassDeclaration node) { ASTNode parent = node.getParent(); ClassInstanceCreation newInvocation = null; EnumConstantDeclaration enumConstant = null; List<Expression> parentArguments; String newClassName; ITypeBinding innerType; boolean isStatic = staticParent(node); int modifiers = isStatic ? Modifier.STATIC : 0; if (parent instanceof ClassInstanceCreation) { newInvocation = (ClassInstanceCreation) parent; parentArguments = newInvocation.arguments(); innerType = Types.getTypeBinding(newInvocation); newClassName = innerType.getName(); innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType, modifiers); } else if (parent instanceof EnumConstantDeclaration) { enumConstant = (EnumConstantDeclaration) parent; parentArguments = enumConstant.arguments(); innerType = Types.getTypeBinding(node); newClassName = Types.getTypeBinding(node).getName(); innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType, modifiers); } else { throw new AssertionError("unknown anonymous class declaration parent: " + parent.getClass().getName()); } // Create a type declaration for this anonymous class. AST ast = node.getAST(); TypeDeclaration typeDecl = ast.newTypeDeclaration(); if (isStatic) { typeDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD)); } Types.addBinding(typeDecl, innerType); typeDecl.setName(ast.newSimpleName(newClassName)); Types.addBinding(typeDecl.getName(), innerType); typeDecl.setSourceRange(node.getStartPosition(), node.getLength()); Type superType = Types.makeType(Types.mapType(innerType.getSuperclass())); typeDecl.setSuperclassType(superType); for (ITypeBinding interfaceType : innerType.getInterfaces()) { typeDecl.superInterfaceTypes().add(Types.makeType(Types.mapType(interfaceType))); } for (Object bodyDecl : node.bodyDeclarations()) { BodyDeclaration decl = (BodyDeclaration) bodyDecl; typeDecl.bodyDeclarations().add(NodeCopier.copySubtree(ast, decl)); } typeDecl.accept(new InitializationNormalizer()); // Fix up references to external types, if necessary. Set<IVariableBinding> methodVars = getMethodVars(node); final List<ReferenceDescription> references = findReferences(node, methodVars); final List<Expression> invocationArgs = parentArguments; if (!references.isEmpty() || !invocationArgs.isEmpty()) { // is there anything to fix-up? List<IVariableBinding> innerVars = getInnerVars(references); if (!innerVars.isEmpty() || !invocationArgs.isEmpty()) { GeneratedMethodBinding defaultConstructor = addInnerVars(typeDecl, innerVars, references, parentArguments); Types.addBinding(parent, defaultConstructor); for (IVariableBinding var : innerVars) { if (!isConstant(var)) { parentArguments.add(makeFieldRef(var, ast)); } } assert defaultConstructor.getParameterTypes().length == parentArguments.size(); typeDecl.accept(new ASTVisitor() { @Override public void endVisit(SimpleName node) { IVariableBinding var = Types.getVariableBinding(node); if (var != null) { for (ReferenceDescription ref : references) { if (var.isEqualTo(ref.binding)) { if (ref.innerField != null) { setProperty(node, makeFieldRef(ref.innerField, node.getAST())); } else { // In-line constant. Object o = var.getConstantValue(); assert o != null; Expression literal = makeLiteral(o, var.getType().getQualifiedName(), node.getAST()); setProperty(node, literal); } return; } } } } }); } } // If invocation, replace anonymous class invocation with the new constructor. if (newInvocation != null) { newInvocation.setAnonymousClassDeclaration(null); newInvocation.setType(Types.makeType(innerType)); IMethodBinding oldBinding = Types.getMethodBinding(newInvocation); if (oldBinding == null) { oldBinding = newInvocation.resolveConstructorBinding(); } if (oldBinding != null) { GeneratedMethodBinding invocationBinding = new GeneratedMethodBinding(oldBinding); invocationBinding.setDeclaringClass(innerType); Types.addBinding(newInvocation, invocationBinding); } } else { enumConstant.setAnonymousClassDeclaration(null); } // Add type declaration to enclosing type. ITypeBinding outerType = innerType.getDeclaringClass(); if (outerType.isAnonymous()) { // Get outerType node. ASTNode n = parent.getParent(); while (!(n instanceof AnonymousClassDeclaration) && !(n instanceof TypeDeclaration)) { n = n.getParent(); } if (n instanceof AnonymousClassDeclaration) { AnonymousClassDeclaration outerDecl = (AnonymousClassDeclaration) n; outerDecl.bodyDeclarations().add(typeDecl); } } else { AbstractTypeDeclaration outerDecl = (AbstractTypeDeclaration) unit.findDeclaringNode(outerType); outerDecl.bodyDeclarations().add(typeDecl); } Symbols.scanAST(typeDecl); super.endVisit(node); }
From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java
License:Open Source License
/** * Creates a type declaration for a new class with the specified parent * and interfaces types./*from w w w . j a v a 2s . co m*/ */ @SuppressWarnings("unchecked") private TypeDeclaration createClass(AST ast, ITypeBinding scope, ITypeBinding superClass, List<ITypeBinding> interfaces) { TypeDeclaration decl = ast.newTypeDeclaration(); if (superClass != null) { decl.setSuperclassType(createType(ast, scope, superClass)); } for (ITypeBinding intrface : interfaces) { decl.superInterfaceTypes().add(createType(ast, scope, intrface)); } decl.modifiers().add(ast.newModifier(ModifierKeyword.ABSTRACT_KEYWORD)); decl.setName(ast.newSimpleName(generateClassName())); return decl; }
From source file:com.idega.eclipse.ejbwizards.BeanCreator.java
License:Open Source License
protected TypeDeclaration getTypeDeclaration(AST ast, String name, boolean isInterface, String superClass, String[] interfaces, Set imports) { TypeDeclaration classType = ast.newTypeDeclaration(); classType.setInterface(isInterface); classType.modifiers().addAll(ast.newModifiers(Modifier.PUBLIC)); classType.setName(ast.newSimpleName(name)); if (isInterface) { classType.superInterfaceTypes().add(ast.newSimpleType(ast.newSimpleName(superClass))); } else {// w w w . j a v a 2 s .c o m classType.setSuperclassType(ast.newSimpleType(ast.newSimpleName(superClass))); } if (interfaces != null) { for (int i = 0; i < interfaces.length; i++) { if (!Signature.getSignatureSimpleName(interfaces[i]).equals(name)) { classType.superInterfaceTypes().add( ast.newSimpleType(ast.newSimpleName(Signature.getSignatureSimpleName(interfaces[i])))); imports.add(getImportSignature(Signature.toString(interfaces[i]))); } } } return classType; }
From source file:de.crowdcode.kissmda.cartridges.simplejava.ExceptionGenerator.java
License:Apache License
/** * Generate the Exception Class./*ww w .j a v a 2 s . co m*/ * * @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; }
From source file:de.crowdcode.kissmda.cartridges.simplejava.ExceptionGeneratorTest.java
License:Apache License
@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()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.ExceptionGeneratorTest.java
License:Apache License
@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()); }