Example usage for org.eclipse.jdt.core.dom TypeDeclaration getFields

List of usage examples for org.eclipse.jdt.core.dom TypeDeclaration getFields

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom TypeDeclaration getFields.

Prototype

public FieldDeclaration[] getFields() 

Source Link

Document

Returns the ordered list of field declarations of this type declaration.

Usage

From source file:chibi.gumtreediff.gen.jdt.cd.CdJdtVisitor.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//  w  ww  . j  av a 2  s.  c  o m
public boolean visit(TypeDeclaration node) {
    if (node.getJavadoc() != null) {
        node.getJavadoc().accept(this);
    }
    // @Inria
    pushNode(node, node.getName().toString());
    visitListAsNode(EntityType.MODIFIERS, node.modifiers());
    visitListAsNode(EntityType.TYPE_ARGUMENTS, node.typeParameters());
    if (node.getSuperclassType() != null) {
        node.getSuperclassType().accept(this);
    }

    visitListAsNode(EntityType.SUPER_INTERFACE_TYPES, node.superInterfaceTypes());

    // @Inria
    // Change Distiller does not check the changes at Class Field declaration
    for (FieldDeclaration fd : node.getFields()) {
        fd.accept(this);
    }
    // @Inria
    // Visit Declaration and Body (inside MD visiting)
    for (MethodDeclaration md : node.getMethods()) {
        md.accept(this);
    }
    return false;
}

From source file:com.alex.example.fixlicense.actions.SampleAction.java

License:Open Source License

private void processInlineLicense(IDocument doc) throws Exception {
    CompilationUnit cu = getAST(doc);//from w  ww . j  a  v a 2  s. c o  m
    cu.recordModifications();
    AST ast = cu.getAST();

    if (cu.types().get(0) instanceof TypeDeclaration) {
        TypeDeclaration td = (TypeDeclaration) cu.types().get(0);
        FieldDeclaration[] fd = td.getFields();
        if (fd.length == 0) {
            td.bodyDeclarations().add(0, createLiceseInLineField(ast));
        } else {
            FieldDeclaration firstFd = fd[0];
            VariableDeclarationFragment vdf = (VariableDeclarationFragment) firstFd.fragments().get(0);
            if (vdf.getName().getIdentifier().equals("COPYRIGHT")) {
                td.bodyDeclarations().remove(0);
                td.bodyDeclarations().add(0, createLiceseInLineField(ast));
            } else {
                td.bodyDeclarations().add(0, createLiceseInLineField(ast));
            }
        }
    }

    //record changes
    TextEdit edits = cu.rewrite(doc, null);
    edits.apply(doc);
}

From source file:com.crispico.flower.codesync.tests.java.JavaTestsOnSourceCode.java

License:Open Source License

public BodyDeclaration getBody(TypeDeclaration javaClass, String bodyName) { // bodyDeclaration :FieldDeclaration,MethodDelcaration
    BodyDeclaration result = bodyMap.get(bodyName);
    if (result == null) {
        bodyMap.clear();/* w w  w  . j  a  va 2 s .c o  m*/
        for (FieldDeclaration f : javaClass.getFields())
            bodyMap.put(JavaSyncUtils.getSimpleNameString(f), f);
        for (MethodDeclaration m : javaClass.getMethods())
            bodyMap.put(JavaSyncUtils.getSimpleNameString(m), m);
        result = bodyMap.get(bodyName);
    }
    return result;
}

From source file:com.crispico.flower.mp.metamodel.codesyncjava.algorithm.reverse.ReverseJavaClass.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from w  w w .  j  a  va2  s  .  com*/
protected ReverseStatus reverseInnerCollections(Class modelElement, CompilationUnit astElement)
        throws CodeSyncException {
    ReverseStatus result = super.reverseInnerCollections(modelElement, astElement);
    TypeDeclaration masterClass = JavaSyncUtils.getMasterClass(astElement);

    EList<Property> propertyList = modelElement.getOwnedAttributes();
    List<FieldDeclaration> fieldList = Arrays.asList(masterClass.getFields());
    result.applyOr(reverseJavaClassOwnedFields.reverse(propertyList, fieldList));

    EList<Operation> operationList = modelElement.getOwnedOperations();
    List<MethodDeclaration> methodList = Arrays.asList(masterClass.getMethods());
    result.applyOr(reverseJavaClassOwnedMethods.reverse(operationList, methodList));

    EList<InterfaceRealization> realizationList = modelElement.getInterfaceRealizations();
    List<Type> interfaceList = masterClass.superInterfaceTypes();
    result.applyOr(reverseJavaClassOwnedRealizations.reverse(realizationList, interfaceList));
    return result;
}

From source file:com.crispico.flower.mp.tests.suite.MMCSJ_S_ANNOT.java

License:Open Source License

/**
 * Add annotation for java Class/Interface on model; on synchronization it has a correspondent on source
 * Add annotation for attribute and operation also
 * /*from  w ww  .j  a va  2  s.  co  m*/
 * Add annotation for java Class/Interface on source; on synchronization it has a correspondent on model
 */
public void testCase01() {
    IFile modelFile = copyFiles(true);
    EclipseFlowerEditingDomain model = TestUtils.loadModel(modelFile);
    // add annotation for class
    JavaClass class1 = (JavaClass) TestUtils.getNamedElementsFromModel(model, "java_src/Class01").get(0);
    JavaAnnotation annot = CodeSyncJavaFactory.eINSTANCE.createJavaAnnotation();
    model.getCommandStack().execute(SetCommand.create(model, annot,
            EcorePackage.eINSTANCE.getEAnnotation_Source(), classAnnotationName));
    model.getCommandStack().execute(
            AddCommand.create(model, class1, EcorePackage.eINSTANCE.getEModelElement_EAnnotations(), annot));
    // add annotation for interface
    JavaInterface interface1 = (JavaInterface) TestUtils
            .getNamedElementsFromModel(model, "java_src/Interface01").get(0);
    annot = CodeSyncJavaFactory.eINSTANCE.createJavaAnnotation();
    model.getCommandStack().execute(SetCommand.create(model, annot,
            EcorePackage.eINSTANCE.getEAnnotation_Source(), interfaceAnnotationName));
    model.getCommandStack().execute(AddCommand.create(model, interface1,
            EcorePackage.eINSTANCE.getEModelElement_EAnnotations(), annot));
    model.save();
    TestUtils.syncronizeModel(model, "java_src");
    // check if the synchronization was correct for the class 
    CompilationUnit asCompUnit = JavaTestUtils.loadJavaFile("java_src/Class01.java");
    List<JavaAnnotationHolder> annotationList = JavaSyncUtils.getAnnotations(asCompUnit);
    assertTrue(annotationList.size() == 1 && ((SimpleName) annotationList.get(0).getAnnotation().getTypeName())
            .getIdentifier().equals(classAnnotationName));
    // check if the synchronization was correct for the interface
    asCompUnit = JavaTestUtils.loadJavaFile("java_src/Interface01.java");
    annotationList = JavaSyncUtils.getAnnotations(asCompUnit);
    assertTrue(annotationList.size() == 1 && ((SimpleName) annotationList.get(0).getAnnotation().getTypeName())
            .getIdentifier().equals(interfaceAnnotationName));
    // add annotation for attribute
    JavaProperty prop = (JavaProperty) class1.getAllAttributes().get(0);
    annot = CodeSyncJavaFactory.eINSTANCE.createJavaAnnotation();
    model.getCommandStack().execute(SetCommand.create(model, annot,
            EcorePackage.eINSTANCE.getEAnnotation_Source(), attributeAnnotationName));
    model.getCommandStack().execute(
            AddCommand.create(model, prop, EcorePackage.eINSTANCE.getEModelElement_EAnnotations(), annot));
    // add annotation for operation
    JavaOperation op = (JavaOperation) class1.getAllOperations().get(0);
    annot = CodeSyncJavaFactory.eINSTANCE.createJavaAnnotation();
    model.getCommandStack().execute(SetCommand.create(model, annot,
            EcorePackage.eINSTANCE.getEAnnotation_Source(), operationAnnotationName));
    model.getCommandStack().execute(
            AddCommand.create(model, op, EcorePackage.eINSTANCE.getEModelElement_EAnnotations(), annot));
    model.save();
    TestUtils.syncronizeModel(model, "java_src");
    // check if the synchronization was correct for the attribute
    asCompUnit = JavaTestUtils.loadJavaFile("java_src/Class01.java");
    TypeDeclaration masterClass = JavaSyncUtils.getMasterClass(asCompUnit);
    List<FieldDeclaration> fieldList = Arrays.asList(masterClass.getFields());
    annotationList = JavaSyncUtils.getAnnotations(fieldList.get(0));
    assertTrue(annotationList.size() == 1 && ((SimpleName) annotationList.get(0).getAnnotation().getTypeName())
            .getIdentifier().equals(attributeAnnotationName));
    // check if the synchronization was correct for the operation   
    List<MethodDeclaration> methodList = Arrays.asList(masterClass.getMethods());
    annotationList = JavaSyncUtils.getAnnotations(methodList.get(0));
    assertTrue(annotationList.size() == 1 && ((SimpleName) annotationList.get(0).getAnnotation().getTypeName())
            .getIdentifier().equals(operationAnnotationName));

    modelFile = copyFiles(false);
    model = TestUtils.loadModel(modelFile);
    TestUtils.syncronizeModel(model, "java_src");
    model.save();
    class1 = (JavaClass) TestUtils.getNamedElementsFromModel(model, "java_src/Class01").get(0);
    assertNotNull(class1.getEAnnotation(classAnnotationName));
    interface1 = (JavaInterface) TestUtils.getNamedElementsFromModel(model, "java_src/Interface01").get(0);
    assertNotNull(interface1.getEAnnotation(interfaceAnnotationName));
}

From source file:com.crispico.flower.mp.tests.suite.MMCSJ_S_ATTR_ST.java

License:Open Source License

/**
 * Actions: Copy the "testModelJava.flower1" and the folder TC04 from
 * "(MMCSA-S-ATTR-ST) Elemente statice" to the root test project Open the
 * file "Class01.java" from the copied folder "TC04/TC04_1" Modify "private
 * Class01 attr1;" to "private static Class01 attr1;" (add the "static"
 * modified) Open the copied "testModelJava.flower1" Open diagrama Diagram1
 * Synchronize "TC04" srcDir Check "Expected Results 1". Open the
 * "Properties" view, then click on the "attr1" from "Class01" in "TC04_1"
 * package From "Properties" view modify property "Is Static" from "true" to
 * "false" Check "Expected Results 2". Synchronize "TC04" srcDir Check
 * "Expected Results 3"./*from  w  ww .  ja v  a 2 s .c  om*/
 * 
 * 
 * Expected results 1 - "attr1" from "Class01" from "TC04_1" package will
 * have a "S" decorator in the upper right part of its icon - "attr1" from
 * "Diagram1" will be underlined and will have a "S" decorator in the upper
 * right part of its icon
 * 
 * Expected results 2 - "attr1" from "Class01" from "TC04_1" package will
 * have lost the "S" decorator in the upper right part of its icon - "attr1"
 * from "Diagram1" will have lost the underline and the decorator.
 * 
 * Expected results 3 - in the file "TC04/TC04_1/Class01.java" will be
 * "private Class01 attr1;" instead of "private static Class01 attr1;"
 */
public void testCase01() {
    TestUtils.clearTempProject();
    // copy diagram file
    IFile diagramFile = TestUtils.copyFile(
            "(MMCSA-S-ATTR-ST) Synchronization of Static Attributes/testModelJava.flower1", "", false);
    // copy source folder TC04
    TestUtils.copyFolder("(MMCSA-S-ATTR-ST) Synchronization of Static Attributes/TC04", "");
    // synchronyze model
    TestUtils.syncronizeModel(diagramFile, "TC04");
    // open model file and see if attr1 from TC04/TC04_1/Class01 is static
    EclipseFlowerEditingDomain model = TestUtils.loadModel(diagramFile);
    JavaClass javaClass = (JavaClass) (TestUtils.getNamedElementsFromModel(model, "TC04/TC04_1/Class01")
            .get(0));
    Property propr = javaClass.getAttribute("attr1", null);
    assertEquals(true, propr.isStatic());
    // remove static attribute for attr1 in Class01 from the TC04_1 package
    // from the model
    model.getCommandStack()
            .execute(SetCommand.create(model, propr, UMLPackage.eINSTANCE.getFeature_IsStatic(), false));
    TestUtils.syncronizeModel(diagramFile, "TC04");
    // check that the attr1 from Class01 from source is no longer static
    CompilationUnit compUnit = JavaTestUtils.loadJavaFile("TC04/TC04_1/Class01.java");
    TypeDeclaration masterClass = JavaSyncUtils.getMasterClass(compUnit);
    FieldDeclaration decl = (FieldDeclaration) (masterClass.getFields()[0]);
    assertEquals(false, JavaSyncUtils.getFeatureValueFromJavaModifier(decl, JavaSyncUtils.MODIFIER_STATIC));

    // JavaSyncUtils.updateModifierFromModelToJavaClass(decl, false,
    // JavaSyncUtils.MODIFIER_STATIC);
}

From source file:com.google.devtools.j2cpp.gen.CppImplementationGenerator.java

License:Open Source License

@Override
public void generate(TypeDeclaration node) {
    List<IMethodBinding> testMethods = null;
    if (Types.isJUnitTest(Types.getTypeBinding(node))) {
        testMethods = findTestMethods(node);
    }/*from  w  ww .  j a va2s . c om*/
    syncLineNumbers(node.getName()); // avoid doc-comment

    fieldHiders = HiddenFieldDetector.getFieldNameConflicts(node);
    if (node.isInterface()) {
        printStaticInterface(node);
    } else {
        String typeName = NameTable.getFullName(node);
        printf("class %s {\n\n", typeName);
        printStaticVars(Lists.newArrayList(node.getFields()));
        printProperties(node.getFields());
        printMethods(node);
        printObjCTypeMethod(node);

        println("}\n");

        // Generate main method, if declared.
        MethodDeclaration main = null;
        for (MethodDeclaration m : node.getMethods()) {
            if (isMainMethod(m)) {
                main = m;
                break;
            }
        }
        newline();
        if (main != null || (testMethods != null && Options.generateTestMain())) {
            printMainMethod(main, typeName, testMethods);
        }
    }
}

From source file:com.google.devtools.j2cpp.gen.CppImplementationGenerator.java

License:Open Source License

private void printStaticInterface(TypeDeclaration node) {
    // Print implementation for static constants, if any.
    boolean needsPrinting = false;
    List<MethodDeclaration> methods = Lists.newArrayList(node.getMethods());
    for (MethodDeclaration m : methods) {
        if (isInterfaceConstantAccessor(Types.getMethodBinding(m))) {
            needsPrinting = true;/*from   w ww.  j av  a 2s  .c  o m*/
            break;
        }
    }

    if (needsPrinting) {
        printf("\n@implementation %s\n\n", NameTable.getFullName(node));
        printStaticVars(Lists.newArrayList(node.getFields()));
        for (MethodDeclaration m : methods) {
            IMethodBinding binding = Types.getMethodBinding(m);
            if (binding.isSynthetic() || isInterfaceConstantAccessor(binding)) {
                printMethod(m);
            }
        }
        println("@end");
    }
}

From source file:com.google.devtools.j2cpp.translate.DestructorGenerator.java

License:Open Source License

@Override
public boolean visit(TypeDeclaration node) {
    final List<IVariableBinding> releaseableFields = Lists.newArrayList();
    for (final FieldDeclaration field : node.getFields()) {
        if (!field.getType().isPrimitiveType() && !isStatic(field)) {
            ErrorReportingASTVisitor varFinder = new ErrorReportingASTVisitor() {
                @Override/*from  w w  w  .j av  a 2  s .c om*/
                public boolean visit(VariableDeclarationFragment node) {
                    IVariableBinding binding = Types.getVariableBinding(node);
                    if (!Modifier.isStatic(field.getModifiers()) && !Types.isConstantVariable(binding)) {
                        releaseableFields.add(binding);
                    }
                    return true;
                }
            };
            varFinder.run(field);
        }
    }
    if (!releaseableFields.isEmpty()) {
        Types.addReleaseableFields(releaseableFields);

        boolean foundDestructor = false;

        // If a destructor method already exists, append release statements.
        for (MethodDeclaration method : node.getMethods()) {
            if (FINALIZE_METHOD.equals(method.getName().getIdentifier())) {
                addReleaseStatements(method, releaseableFields);
                foundDestructor = true;
            }
        }

        // No destructor, so create a new one.
        if (!foundDestructor && !Options.useARC()) {
            MethodDeclaration finalizeMethod = buildFinalizeMethod(node.getAST(), Types.getTypeBinding(node),
                    releaseableFields);
            @SuppressWarnings("unchecked")
            List<BodyDeclaration> declarations = node.bodyDeclarations();
            declarations.add(finalizeMethod);
        }
    }

    // Rename method to correct destructor name.  This is down outside of
    // the loop above, because a class may have a finalize() method but no
    // releasable fields.
    for (MethodDeclaration method : node.getMethods()) {
        if (needsRenaming(method.getName())) {
            NameTable.rename(Types.getBinding(method), destructorName);
        }
    }
    return super.visit(node);
}

From source file:com.google.devtools.j2objc.translate.AnonymousClassConverterTest.java

License:Open Source License

public void testMethodVarInNestedAnonymousClass() throws IOException {
    String source = "class Test { " + "  void bar() { " + "    Runnable r1 = new Runnable() { "
            + "      public void run() { " + "        final Integer i = 1; "
            + "        Runnable r2 = new Runnable() { "
            + "          public void run() { int j = i + 1; } }; } }; } }";

    // Verify method var in r1.run() isn't mistakenly made a field in r1.
    CompilationUnit unit = translateType("Test", source);
    List<TypeDeclaration> types = unit.types();
    TypeDeclaration r1 = types.get(1);
    assertEquals("Test_$1", NameTable.getFullName(r1));
    for (FieldDeclaration field : r1.getFields()) {
        List<VariableDeclarationFragment> vars = field.fragments();
        for (VariableDeclaration var : vars) {
            if (var.getName().getIdentifier().equals("val$i")) {
                fail("found field that shouldn't be declared");
            }//from   w  w  w .  j a va2s.  c  om
        }
    }

    // Method var in r1.run() becomes a field in r2.
    TypeDeclaration r2 = types.get(2);
    assertEquals("Test_$1_$1", NameTable.getFullName(r2));
    boolean found = false;
    for (FieldDeclaration field : r2.getFields()) {
        List<VariableDeclarationFragment> vars = field.fragments();
        for (VariableDeclaration var : vars) {
            if (var.getName().getIdentifier().equals("val$i")) {
                found = true;
            }
        }
    }
    assertTrue("required field not found", found);

    // Verify constructor takes both outer field and var.
    ObjectiveCImplementationGenerator.generate("Test.java", unit, source);
    String translation = getTranslatedFile("Test.m");
    assertTranslation(translation, "r2 = [[[Test_$1_$1 alloc] initWithJavaLangInteger:i] autorelease]");
}