List of usage examples for org.eclipse.jdt.core.dom AST newCompilationUnit
public CompilationUnit newCompilationUnit()
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);//from w w w . jav a 2 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:br.com.objectos.way.core.code.jdt.CompilationUnitWriter.java
License:Apache License
public static CompilationUnitWriter atPackageOf(TypeInfo typeInfo) { AST ast = AST.newAST(AST.JLS8); CompilationUnit compilationUnit = ast.newCompilationUnit(); PackageInfo packageInfo = typeInfo.writePackage(compilationUnit); return new CompilationUnitWriter(compilationUnit, packageInfo); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGenerator.java
License:Apache License
/** * Generate the Class Interface. This is the main generation part for this * SimpleJavaTransformer./*from w w w . j a v a2s .com*/ * * @param Class * clazz the UML class * @return CompilationUnit the complete class with its content as a String */ public CompilationUnit generateEnum(Classifier clazz, String sourceDirectoryPackageName) { this.sourceDirectoryPackageName = sourceDirectoryPackageName; AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); generatePackage(clazz, ast, cu); EnumDeclaration ed = generateEnum(clazz, ast, cu); generateAttributes(clazz, ast, ed); generateConstructor(clazz, ast, ed); generateConstants(clazz, ast, ed); generateGetterMethod(clazz, ast, ed); logger.log(Level.INFO, "Compilation unit: \n\n" + cu.toString()); return cu; }
From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java
License:Apache License
@Test public void testGeneratePackage() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); enumGenerator.generatePackage(clazz, ast, cu); String onlyPackage = cu.toString(); String expectedResult = "package de.crowdcode.kissmda.testapp.components;\n"; boolean isInside = onlyPackage.contains(expectedResult); assertTrue(isInside);//from w ww .jav a 2 s .c o m }
From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java
License:Apache License
@Test public void testGenerateEnum() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); enumGenerator.generateEnum(clazz, ast, cu); assertEquals("public enum Company {}\n", cu.toString()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test// w w w . ja va 2 s . c o m public void testGenerateConstantsIntegerWithNoParamNames() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu); Enumeration enumeration = mock(Enumeration.class); EList<EnumerationLiteral> enumLiterals = mock(EList.class); Iterator<EnumerationLiteral> enumIter = mock(Iterator.class); EnumerationLiteral enumLiteral = mock(EnumerationLiteral.class); EList<Slot> slots = mock(EList.class); Iterator<Slot> slotIter = mock(Iterator.class); Slot slot = mock(Slot.class); Property property = mock(Property.class); Type type = mock(Type.class); EList<ValueSpecification> valueSpecifications = mock(EList.class); Iterator<ValueSpecification> valueSpecificationIter = mock(Iterator.class); ValueSpecification valueSpecification = mock(ValueSpecification.class); when(enumeration.getOwnedLiterals()).thenReturn(enumLiterals); when(enumLiterals.iterator()).thenReturn(enumIter); when(enumIter.hasNext()).thenReturn(true).thenReturn(false); when(enumIter.next()).thenReturn(enumLiteral); when(enumLiteral.getName()).thenReturn("Home"); when(enumLiteral.getSlots()).thenReturn(slots); when(slots.iterator()).thenReturn(slotIter); when(slotIter.hasNext()).thenReturn(true).thenReturn(false); when(slotIter.next()).thenReturn(slot); when(slot.getDefiningFeature()).thenReturn(property); when(property.getType()).thenReturn(type); when(type.getName()).thenReturn("Integer"); when(slot.getValues()).thenReturn(valueSpecifications); when(valueSpecifications.iterator()).thenReturn(valueSpecificationIter); when(valueSpecificationIter.hasNext()).thenReturn(true).thenReturn(false); when(valueSpecificationIter.next()).thenReturn(valueSpecification); when(valueSpecification.integerValue()).thenReturn(0); enumGenerator.generateConstants(enumeration, ast, ed); assertEquals("public enum Company {HOME(0)}\n", ed.toString()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test//from w ww .ja v a2s. c o m public void testGenerateConstantsStringWithNoParamNames() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu); Enumeration enumeration = mock(Enumeration.class); EList<EnumerationLiteral> enumLiterals = mock(EList.class); Iterator<EnumerationLiteral> enumIter = mock(Iterator.class); EnumerationLiteral enumLiteral = mock(EnumerationLiteral.class); EList<Slot> slots = mock(EList.class); Iterator<Slot> slotIter = mock(Iterator.class); Slot slot = mock(Slot.class); Property property = mock(Property.class); Type type = mock(Type.class); EList<ValueSpecification> valueSpecifications = mock(EList.class); Iterator<ValueSpecification> valueSpecificationIter = mock(Iterator.class); ValueSpecification valueSpecification = mock(ValueSpecification.class); when(enumeration.getOwnedLiterals()).thenReturn(enumLiterals); when(enumLiterals.iterator()).thenReturn(enumIter); when(enumIter.hasNext()).thenReturn(true).thenReturn(false); when(enumIter.next()).thenReturn(enumLiteral); when(enumLiteral.getName()).thenReturn("Home"); when(enumLiteral.getSlots()).thenReturn(slots); when(slots.iterator()).thenReturn(slotIter); when(slotIter.hasNext()).thenReturn(true).thenReturn(false); when(slotIter.next()).thenReturn(slot); when(slot.getDefiningFeature()).thenReturn(property); when(property.getType()).thenReturn(type); when(type.getName()).thenReturn("String"); when(slot.getValues()).thenReturn(valueSpecifications); when(valueSpecifications.iterator()).thenReturn(valueSpecificationIter); when(valueSpecificationIter.hasNext()).thenReturn(true).thenReturn(false); when(valueSpecificationIter.next()).thenReturn(valueSpecification); when(valueSpecification.stringValue()).thenReturn("Home"); enumGenerator.generateConstants(enumeration, ast, ed); assertEquals("public enum Company {HOME(\"Home\")}\n", ed.toString()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test// w w w . j a v a2s . c om public void testGenerateConstantsLongWithNoParamNames() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu); Enumeration enumeration = mock(Enumeration.class); EList<EnumerationLiteral> enumLiterals = mock(EList.class); Iterator<EnumerationLiteral> enumIter = mock(Iterator.class); EnumerationLiteral enumLiteral = mock(EnumerationLiteral.class); EList<Slot> slots = mock(EList.class); Iterator<Slot> slotIter = mock(Iterator.class); Slot slot = mock(Slot.class); Property property = mock(Property.class); Type type = mock(Type.class); EList<ValueSpecification> valueSpecifications = mock(EList.class); Iterator<ValueSpecification> valueSpecificationIter = mock(Iterator.class); ValueSpecification valueSpecification = mock(ValueSpecification.class); when(enumeration.getOwnedLiterals()).thenReturn(enumLiterals); when(enumLiterals.iterator()).thenReturn(enumIter); when(enumIter.hasNext()).thenReturn(true).thenReturn(false); when(enumIter.next()).thenReturn(enumLiteral); when(enumLiteral.getName()).thenReturn("Home"); when(enumLiteral.getSlots()).thenReturn(slots); when(slots.iterator()).thenReturn(slotIter); when(slotIter.hasNext()).thenReturn(true).thenReturn(false); when(slotIter.next()).thenReturn(slot); when(slot.getDefiningFeature()).thenReturn(property); when(property.getType()).thenReturn(type); when(type.getName()).thenReturn("Long"); when(slot.getValues()).thenReturn(valueSpecifications); when(valueSpecifications.iterator()).thenReturn(valueSpecificationIter); when(valueSpecificationIter.hasNext()).thenReturn(true).thenReturn(false); when(valueSpecificationIter.next()).thenReturn(valueSpecification); when(valueSpecification.integerValue()).thenReturn(1); enumGenerator.generateConstants(enumeration, ast, ed); assertEquals("public enum Company {HOME(1L)}\n", ed.toString()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test//from ww w .j av a2s . c o m public void testGenerateConstantsBooleanWithNoParamNames() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu); Enumeration enumeration = mock(Enumeration.class); EList<EnumerationLiteral> enumLiterals = mock(EList.class); Iterator<EnumerationLiteral> enumIter = mock(Iterator.class); EnumerationLiteral enumLiteral = mock(EnumerationLiteral.class); EList<Slot> slots = mock(EList.class); Iterator<Slot> slotIter = mock(Iterator.class); Slot slot = mock(Slot.class); Property property = mock(Property.class); Type type = mock(Type.class); EList<ValueSpecification> valueSpecifications = mock(EList.class); Iterator<ValueSpecification> valueSpecificationIter = mock(Iterator.class); ValueSpecification valueSpecification = mock(ValueSpecification.class); when(enumeration.getOwnedLiterals()).thenReturn(enumLiterals); when(enumLiterals.iterator()).thenReturn(enumIter); when(enumIter.hasNext()).thenReturn(true).thenReturn(false); when(enumIter.next()).thenReturn(enumLiteral); when(enumLiteral.getName()).thenReturn("Home"); when(enumLiteral.getSlots()).thenReturn(slots); when(slots.iterator()).thenReturn(slotIter); when(slotIter.hasNext()).thenReturn(true).thenReturn(false); when(slotIter.next()).thenReturn(slot); when(slot.getDefiningFeature()).thenReturn(property); when(property.getType()).thenReturn(type); when(type.getName()).thenReturn("boolean"); when(slot.getValues()).thenReturn(valueSpecifications); when(valueSpecifications.iterator()).thenReturn(valueSpecificationIter); when(valueSpecificationIter.hasNext()).thenReturn(true).thenReturn(false); when(valueSpecificationIter.next()).thenReturn(valueSpecification); when(valueSpecification.booleanValue()).thenReturn(true); enumGenerator.generateConstants(enumeration, ast, ed); assertEquals("public enum Company {HOME(true)}\n", ed.toString()); }
From source file:de.crowdcode.kissmda.cartridges.simplejava.EnumGeneratorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test/* w w w .j av a 2s .co m*/ public void testGenerateGetterMethod() { AST ast = AST.newAST(AST.JLS3); CompilationUnit cu = ast.newCompilationUnit(); EnumDeclaration ed = enumGenerator.generateEnum(clazz, ast, cu); EList<Property> properties = mock(EList.class); Iterator<Property> propertyIter = mock(Iterator.class); Property property = mock(Property.class); Type type = mock(Type.class); String name = "type"; EList<Comment> comments = mock(EList.class, Answers.RETURNS_DEEP_STUBS.get()); when(clazz.getAttributes()).thenReturn(properties); when(properties.iterator()).thenReturn(propertyIter); when(propertyIter.hasNext()).thenReturn(true).thenReturn(false); when(propertyIter.next()).thenReturn(property); when(property.getType()).thenReturn(type); when(property.getName()).thenReturn(name); when(property.getUpper()).thenReturn(1); when(property.getLower()).thenReturn(1); when(property.getOwnedComments()).thenReturn(comments); when(type.getName()).thenReturn("String"); when(type.getQualifiedName()).thenReturn("String"); enumGenerator.generateGetterMethod(clazz, ast, ed); assertEquals("public enum Company {; public String getType(){\n return type;\n}\n}\n", cu.toString()); }