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

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

Introduction

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

Prototype

public TypeDeclaration[] getTypes() 

Source Link

Document

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

Usage

From source file:changetypes.ASTVisitorAtomicChange.java

License:Open Source License

public boolean visit(TypeDeclaration node) {
    ITypeBinding itb = node.resolveBinding();
    this.itbStack.push(itb);
    try {//from ww w. j av  a  2 s .  com
        this.facts.add(Fact.makeTypeFact(getQualifiedName(itb), getSimpleName(itb), itb.getPackage().getName(),
                itb.isInterface() ? "interface" : "class"));
    } catch (Exception localException1) {
        System.err.println("Cannot resolve bindings for class " + node.getName().toString());
    }
    ITypeBinding localITypeBinding1;
    ITypeBinding i2;
    try {
        ITypeBinding itb2 = itb.getSuperclass();
        if (itb.getSuperclass() != null) {
            this.facts.add(Fact.makeSubtypeFact(getQualifiedName(itb2), getQualifiedName(itb)));
            this.facts.add(Fact.makeExtendsFact(getQualifiedName(itb2), getQualifiedName(itb)));
        }
        ITypeBinding[] arrayOfITypeBinding;
        int i;
        if (node.isInterface()) {
            i = (arrayOfITypeBinding = itb.getInterfaces()).length;
            for (localITypeBinding1 = 0; localITypeBinding1 < i; localITypeBinding1++) {
                ITypeBinding i2 = arrayOfITypeBinding[localITypeBinding1];
                this.facts.add(Fact.makeSubtypeFact(getQualifiedName(i2), getQualifiedName(itb)));
                this.facts.add(Fact.makeExtendsFact(getQualifiedName(i2), getQualifiedName(itb)));
            }
        } else {
            i = (arrayOfITypeBinding = itb.getInterfaces()).length;
            for (localITypeBinding1 = 0; localITypeBinding1 < i; localITypeBinding1++) {
                i2 = arrayOfITypeBinding[localITypeBinding1];
                this.facts.add(Fact.makeSubtypeFact(getQualifiedName(i2), getQualifiedName(itb)));
                this.facts.add(Fact.makeImplementsFact(getQualifiedName(i2), getQualifiedName(itb)));
            }
        }
    } catch (Exception localException2) {
        System.err.println("Cannot resolve super class bindings for class " + node.getName().toString());
    }
    Object localObject;
    try {
        localITypeBinding1 = (localObject = itb.getDeclaredFields()).length;
        for (i2 = 0; i2 < localITypeBinding1; i2++) {
            IVariableBinding ivb = localObject[i2];
            String visibility = getModifier(ivb);
            String fieldStr = ivb.toString();

            String[] tokens = fieldStr.split(" ");
            String[] arrayOfString1;
            int k = (arrayOfString1 = tokens).length;
            for (int j = 0; j < k; j++) {
                String token = arrayOfString1[j];
                if (this.allowedFieldMods_.contains(token)) {
                    this.facts.add(Fact.makeFieldModifierFact(getQualifiedName(ivb), token));
                }
            }
            this.facts.add(Fact.makeFieldFact(getQualifiedName(ivb), ivb.getName(), getQualifiedName(itb),
                    visibility));
            if (!ivb.getType().isParameterizedType()) {
                this.facts.add(Fact.makeFieldTypeFact(getQualifiedName(ivb), getQualifiedName(ivb.getType())));
            } else {
                this.facts.add(Fact.makeFieldTypeFact(getQualifiedName(ivb), makeParameterizedName(ivb)));
            }
        }
    } catch (Exception localException3) {
        System.err.println("Cannot resolve field bindings for class " + node.getName().toString());
    }
    try {
        ITypeBinding localITypeBinding2 = (localObject = node.getTypes()).length;
        for (i2 = 0; i2 < localITypeBinding2; i2++) {
            TypeDeclaration t = localObject[i2];
            ITypeBinding intb = t.resolveBinding();
            this.facts.add(Fact.makeTypeInTypeFact(getQualifiedName(intb), getQualifiedName(itb)));
        }
    } catch (Exception localException4) {
        System.err.println("Cannot resolve inner type bindings for class " + node.getName().toString());
    }
    return true;
}

From source file:com.android.icu4j.srcgen.RunWithAnnotator.java

License:Apache License

private boolean annotateTypeDeclaration(CompilationUnit cu, ASTRewrite rewrite, TypeDeclaration typeDeclaration,
        boolean topLevelClass, boolean imported) {

    int modifiers = typeDeclaration.getModifiers();
    if ((topLevelClass || Modifier.isStatic(modifiers)) && Modifier.isPublic(modifiers)
            && !Modifier.isAbstract(modifiers)) {
        Type superClassType = typeDeclaration.getSuperclassType();
        if (superClassType != null) {
            String name = superClassType.toString();
            String runnerClass = BASE_CLASS_2_RUNNER_CLASS.get(name);
            if (runnerClass != null) {
                addRunWithAnnotation(cu, rewrite, typeDeclaration, runnerClass, imported);
                imported = true;//from w w  w  . j  av a2s.com
            }
        }
    }

    for (TypeDeclaration innerClass : typeDeclaration.getTypes()) {
        imported = annotateTypeDeclaration(cu, rewrite, innerClass, false, imported);
    }

    return imported;
}

From source file:com.architexa.diagrams.relo.jdt.ParseUtilities.java

License:Open Source License

static private TypeDeclaration findTypeDeclaration(CompilationUnit cu, IType type) throws JavaModelException {
    IType declaringType = type.getDeclaringType();
    if (declaringType == null) {
        Iterator<?> topLevelTypeDeclIt = cu.types().iterator();
        while (topLevelTypeDeclIt.hasNext()) {
            AbstractTypeDeclaration td = (AbstractTypeDeclaration) topLevelTypeDeclIt.next();
            if (td.getName().getIdentifier().equals(type.getElementName()))
                return (TypeDeclaration) td;
        }/*from  ww w  . j  a  v  a 2 s .c o  m*/
    } else {
        TypeDeclaration declaringTD = findTypeDeclaration(cu, declaringType);
        TypeDeclaration[] siblingTD = declaringTD.getTypes();
        for (int i = 0; i < siblingTD.length; i++) {
            if (siblingTD[i].getName().getIdentifier().equals(type.getElementName()))
                return siblingTD[i];
        }
    }

    logger.error("Could not find type for: " + type.getFullyQualifiedName());
    return null;
}

From source file:com.google.gwt.eclipse.core.validators.java.JavaASTUtilsTest.java

License:Open Source License

public void testGetSource() {
    String expectedSource = createString(testClassSource);
    String actualSource = JavaASTUtils.getSource(ast);
    assertEquals(expectedSource, actualSource);

    CompilationUnit root = (CompilationUnit) ast;
    TypeDeclaration typeDecl = (TypeDeclaration) root.types().get(0);
    TypeDeclaration innerTypeDecl = typeDecl.getTypes()[0];
    MethodDeclaration getNumberDecl = innerTypeDecl.getMethods()[0];

    String expectedMethodSource = createString(
            new String[] { "public static int getNumber() {", "      return 777;", "    }" });
    String actualMethodSource = JavaASTUtils.getSource(getNumberDecl);
    assertEquals(expectedMethodSource, actualMethodSource);
}

From source file:com.windowtester.eclipse.ui.convert.WTAPIAbstractVisitor.java

License:Open Source License

/**
 * Set the variable scope and then visit field then method nodes. Visit all the fields
 * before visiting the methods to cache the field type information accessible by the
 * methods./*from w  w w.  j a v a  2s  . co m*/
 */
public boolean visit(TypeDeclaration node) {
    Type superType = node.getSuperclassType();
    if (superType != null)
        superType.accept(this);
    for (Object each : node.superInterfaceTypes())
        ((Type) each).accept(this);
    currentScope = new VarScope(currentScope);
    FieldDeclaration[] fields = node.getFields();
    for (int i = 0; i < fields.length; i++)
        fields[i].accept(this);
    TypeDeclaration[] types = node.getTypes();
    for (int i = 0; i < types.length; i++)
        types[i].accept(this);
    MethodDeclaration[] methods = node.getMethods();
    for (int i = 0; i < methods.length; i++)
        methods[i].accept(this);
    currentScope = currentScope.getParent();
    return false;
}

From source file:de.fkoeberle.autocommit.message.java.factories.AddedTypeCMF.java

License:Open Source License

private boolean isClassAStub(TypeDeclaration declaration) {
    if (declaration.getFields().length > 0) {
        return false;
    }//from w w  w  .  j av a2 s.c  om
    if (declaration.getTypes().length > 0) {
        return false;
    }
    MethodDeclaration[] methods = declaration.getMethods();
    if (declaration.getMethods().length == 0) {
        return true;
    }
    for (MethodDeclaration method : methods) {
        List<?> statements = method.getBody().statements();
        if (statements.size() > 1) {
            return false;
        }
        for (Object statementObject : statements) {
            if (!(statementObject instanceof ReturnStatement)) {
                return false;
            }
            ReturnStatement returnStatement = (ReturnStatement) statementObject;
            Expression expression = returnStatement.getExpression();
            if (expression != null) {
                boolean simple = (expression instanceof NullLiteral || expression instanceof NumberLiteral
                        || expression instanceof BooleanLiteral);
                if (!simple) {
                    return false;
                }
            }
        }
    }

    return true;
}

From source file:org.autorefactor.cfg.CFGBuilder.java

License:Open Source License

/**
 * Builds a CFG for the provided node.// w ww  . j a  v  a  2 s .c  o  m
 *
 * @param node the node for which to build a CFG.
 * @return the list of basic blocks representing CFGs for each method in this type declaration
 */
public List<CFGBasicBlock> buildCFG(TypeDeclaration node) {
    if (!node.isInterface()) {
        List<CFGBasicBlock> results = new LinkedList<CFGBasicBlock>();
        for (FieldDeclaration fieldDecl : node.getFields()) {
            buildCFG(fieldDecl);
        }
        for (MethodDeclaration methodDecl : node.getMethods()) {
            results.add(buildCFG(methodDecl));
        }
        for (TypeDeclaration typeDeclaration : node.getTypes()) {
            buildCFG(typeDeclaration);
        }
        // for (BodyDeclaration bodyDeclaration : (List<BodyDeclaration>)
        // node.bodyDeclarations()) {
        // buildCFG(bodyDeclaration);
        // }
        return results;
    }
    return Collections.emptyList();
}

From source file:org.autorefactor.refactoring.rules.CommentsRefactoring.java

License:Open Source License

private ASTNode getPreviousSibling(ASTNode node) {
    boolean isPrevious = true;
    if (node != null && node.getParent() instanceof TypeDeclaration) {
        final TypeDeclaration typeDecl = (TypeDeclaration) node.getParent();

        final TreeMap<Integer, ASTNode> nodes = new TreeMap<Integer, ASTNode>();
        addAll(nodes, typeDecl.getFields());
        addAll(nodes, typeDecl.getMethods());
        addAll(nodes, typeDecl.getTypes());

        Entry<Integer, ASTNode> entry;
        if (isPrevious) {
            entry = nodes.floorEntry(node.getStartPosition() - 1);
        } else {/*from w  w  w. jav a2s. co m*/
            entry = nodes.ceilingEntry(node.getStartPosition() + 1);
        }
        if (entry != null) {
            return entry.getValue();
        }
    }
    return null;
}

From source file:org.eclipse.emf.test.tools.merger.ASTTest.java

License:Open Source License

@Test
public void testRead() {
    String content = TestUtil.readFile(CLASS_FILE, false);

    ASTParser astParser = CodeGenUtil.EclipseUtil.newASTParser();
    astParser.setSource(content.toCharArray());
    CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);

    {/*  w  ww  .j  a  v  a  2s  .  c  om*/
        Javadoc javadoc = (Javadoc) compilationUnit.getCommentList().get(0);
        assertEquals(1, javadoc.tags().size());
        TagElement tagElement = (TagElement) javadoc.tags().get(0);
        assertEquals(7, tagElement.fragments().size());
        //     for (Iterator i = tagElement.fragments().iterator(); i.hasNext();)
        //    {
        //      TextElement element = (TextElement)i.next();
        //      System.out.println(element.getText());
        //    }
    }

    //** Package
    PackageDeclaration packageDeclaration = compilationUnit.getPackage();
    assertNotNull(packageDeclaration);
    assertTrue(packageDeclaration.getName().isQualifiedName());
    assertEquals("org.eclipse.emf.test.tools.merger", packageDeclaration.getName().getFullyQualifiedName());

    //** Imports
    List<?> importDeclarations = compilationUnit.imports();
    assertEquals(6, importDeclarations.size());
    assertEquals("java.util.Collections",
            ((ImportDeclaration) importDeclarations.get(0)).getName().getFullyQualifiedName());
    assertFalse(((ImportDeclaration) importDeclarations.get(0)).isOnDemand());
    assertEquals("java.util.List",
            ((ImportDeclaration) importDeclarations.get(1)).getName().getFullyQualifiedName());
    assertFalse(((ImportDeclaration) importDeclarations.get(1)).isOnDemand());
    assertEquals("java.util.Map",
            ((ImportDeclaration) importDeclarations.get(2)).getName().getFullyQualifiedName());
    assertFalse(((ImportDeclaration) importDeclarations.get(2)).isOnDemand());
    assertEquals("org.eclipse.emf.common",
            ((ImportDeclaration) importDeclarations.get(3)).getName().getFullyQualifiedName());
    assertTrue(((ImportDeclaration) importDeclarations.get(3)).isOnDemand());
    assertEquals("org.eclipse.emf.common.notify.Notification",
            ((ImportDeclaration) importDeclarations.get(4)).getName().getFullyQualifiedName());
    assertFalse(((ImportDeclaration) importDeclarations.get(4)).isOnDemand());
    assertEquals("org.eclipse.emf.ecore.impl.EObjectImpl",
            ((ImportDeclaration) importDeclarations.get(5)).getName().getFullyQualifiedName());
    assertFalse(((ImportDeclaration) importDeclarations.get(5)).isOnDemand());

    //** Types
    List<?> typeDeclarations = compilationUnit.types();
    assertEquals(2, typeDeclarations.size());

    //** Class Example1
    TypeDeclaration exampleClass = (TypeDeclaration) typeDeclarations.get(1);
    assertEquals("Example1", exampleClass.getName().getFullyQualifiedName());
    assertFalse(exampleClass.isInterface());
    //Javadoc
    {
        Javadoc typeJavadoc = exampleClass.getJavadoc();
        assertEquals(4, typeJavadoc.tags().size());
        @SuppressWarnings("unchecked")
        TagElement[] tagElements = (TagElement[]) typeJavadoc.tags()
                .toArray(new TagElement[typeJavadoc.tags().size()]);
        //Tag[0]: " This is an example to be parsed by the ASTTests.\n Not really important"
        assertNull(tagElements[0].getTagName());
        assertEquals(2, tagElements[0].fragments().size());
        assertEquals("This is an example of a fairly complete Java file.",
                ((TextElement) tagElements[0].fragments().get(0)).getText());
        assertEquals("Its content is not really important",
                ((TextElement) tagElements[0].fragments().get(1)).getText());
        //Tag[1]: "@author EMF team"
        assertEquals("@author", tagElements[1].getTagName());
        assertEquals(1, tagElements[1].fragments().size());
        assertEquals(" EMF team", ((TextElement) tagElements[1].fragments().get(0)).getText());
        //Tag[2]: "@generated"
        assertEquals("@generated", tagElements[2].getTagName());
        assertTrue(tagElements[2].fragments().isEmpty());
        //Tag[3]: "@generated NOT"
        assertEquals("@generated", tagElements[3].getTagName());
        assertEquals(1, tagElements[3].fragments().size());
        assertEquals(" NOT", ((TextElement) tagElements[3].fragments().get(0)).getText());
    }
    //Super Class
    assertTrue(exampleClass.getSuperclassType().isSimpleType());
    assertEquals("EObjectImpl",
            ((SimpleType) exampleClass.getSuperclassType()).getName().getFullyQualifiedName());
    //Interfaces
    assertTrue(exampleClass.superInterfaceTypes().isEmpty());
    //Modifiers
    assertEquals(Modifier.PUBLIC, exampleClass.getModifiers());

    //** Content of the Example1 class
    assertEquals(19, exampleClass.bodyDeclarations().size());
    assertEquals(2, exampleClass.getTypes().length);
    assertEquals(7, exampleClass.getFields().length);
    assertEquals(7, exampleClass.getMethods().length);

    // Tests the order of the contents
    List<?> bodyDeclarations = exampleClass.bodyDeclarations();
    assertTrue(bodyDeclarations.get(0).toString(), bodyDeclarations.get(0) instanceof TypeDeclaration);
    assertTrue(bodyDeclarations.get(1).toString(), bodyDeclarations.get(1) instanceof Initializer);
    assertTrue(bodyDeclarations.get(2).toString(), bodyDeclarations.get(2) instanceof FieldDeclaration);
    assertTrue(bodyDeclarations.get(3).toString(), bodyDeclarations.get(3) instanceof TypeDeclaration);
    assertTrue(bodyDeclarations.get(4).toString(), bodyDeclarations.get(4) instanceof FieldDeclaration);
    assertTrue(bodyDeclarations.get(5).toString(), bodyDeclarations.get(5) instanceof FieldDeclaration);
    assertTrue(bodyDeclarations.get(6).toString(), bodyDeclarations.get(6) instanceof FieldDeclaration);
    assertTrue(bodyDeclarations.get(7).toString(), bodyDeclarations.get(7) instanceof Initializer);
    assertTrue(bodyDeclarations.get(8).toString(), bodyDeclarations.get(8) instanceof MethodDeclaration);
    assertTrue(bodyDeclarations.get(8).toString(), bodyDeclarations.get(9) instanceof MethodDeclaration);
    assertTrue(bodyDeclarations.get(8).toString(), bodyDeclarations.get(10) instanceof MethodDeclaration);
    assertTrue(bodyDeclarations.get(9).toString(), bodyDeclarations.get(11) instanceof MethodDeclaration);
    assertTrue(bodyDeclarations.get(10).toString(), bodyDeclarations.get(12) instanceof FieldDeclaration);
    assertTrue(bodyDeclarations.get(11).toString(), bodyDeclarations.get(13) instanceof MethodDeclaration);
    assertTrue(bodyDeclarations.get(12).toString(), bodyDeclarations.get(14) instanceof MethodDeclaration);
    assertTrue(bodyDeclarations.get(13).toString(), bodyDeclarations.get(15) instanceof MethodDeclaration);
    assertTrue(bodyDeclarations.get(14).toString(), bodyDeclarations.get(16) instanceof Initializer);

    //** Initializers
    {
        Initializer initializer = (Initializer) bodyDeclarations.get(1);
        assertFalse(Modifier.isStatic(initializer.getModifiers()));
        assertNull(initializer.getJavadoc());
    }
    //
    {
        Initializer initializer = (Initializer) bodyDeclarations.get(7);
        assertTrue(Modifier.isStatic(initializer.getModifiers()));
        assertNotNull(initializer.getJavadoc());
        Javadoc javadoc = initializer.getJavadoc();
        assertEquals(1, javadoc.tags().size());
        assertEquals(1, ((TagElement) javadoc.tags().get(0)).fragments().size());
        assertEquals("An static initializer",
                ((TextElement) ((TagElement) javadoc.tags().get(0)).fragments().get(0)).getText());
    }
    //
    {
        Initializer initializer = (Initializer) bodyDeclarations.get(16);
        assertFalse(Modifier.isStatic(initializer.getModifiers()));
        assertNotNull(initializer.getJavadoc());
        Javadoc javadoc = initializer.getJavadoc();
        assertEquals(1, javadoc.tags().size());
        assertEquals(2, ((TagElement) javadoc.tags().get(0)).fragments().size());
        assertEquals("Another initializer with 2 lines",
                ((TextElement) ((TagElement) javadoc.tags().get(0)).fragments().get(0)).getText());
        assertEquals("of javadoc.",
                ((TextElement) ((TagElement) javadoc.tags().get(0)).fragments().get(1)).getText());
    }

    //** Inner Class
    TypeDeclaration innerClass = exampleClass.getTypes()[0];
    assertFalse(innerClass.isInterface());
    assertTrue(innerClass.bodyDeclarations().isEmpty());
    assertEquals(Modifier.PUBLIC | Modifier.ABSTRACT, innerClass.getModifiers());
    assertNull(innerClass.getSuperclassType());
    assertEquals(2, innerClass.superInterfaceTypes().size());
    assertTrue(((Type) innerClass.superInterfaceTypes().get(0)).isSimpleType());
    assertEquals("Notification",
            ((SimpleType) innerClass.superInterfaceTypes().get(0)).getName().getFullyQualifiedName());
    assertTrue(((Type) innerClass.superInterfaceTypes().get(1)).isSimpleType());
    assertEquals("org.eclipse.emf.common.notify.Notifier",
            ((SimpleType) innerClass.superInterfaceTypes().get(1)).getName().getFullyQualifiedName());
    assertNull(innerClass.getJavadoc());

    //** Inner Interface
    TypeDeclaration innerInterface = exampleClass.getTypes()[1];
    assertTrue(innerInterface.isInterface());
    assertTrue(innerInterface.bodyDeclarations().isEmpty());
    assertEquals(Modifier.PRIVATE | Modifier.STATIC, innerInterface.getModifiers());
    assertNull(innerInterface.getSuperclassType());
    assertEquals(1, innerInterface.superInterfaceTypes().size());
    assertTrue(((Type) innerInterface.superInterfaceTypes().get(0)).isSimpleType());
    assertEquals("Notification",
            ((SimpleType) innerInterface.superInterfaceTypes().get(0)).getName().getFullyQualifiedName());
    assertNull(innerClass.getJavadoc());

    //** Fields
    FieldDeclaration[] fieldDeclarations = exampleClass.getFields();
    //fieldDeclarations[0]: public static final String STR_CONST = "something"
    {
        Javadoc javadoc = fieldDeclarations[0].getJavadoc();
        assertEquals(1, javadoc.tags().size());
        assertEquals(1, ((TagElement) javadoc.tags().get(0)).fragments().size());
        assertEquals("public String constant.",
                ((TextElement) ((TagElement) javadoc.tags().get(0)).fragments().get(0)).getText());
        //
        assertEquals(Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL, fieldDeclarations[0].getModifiers());
        //
        assertTrue(fieldDeclarations[0].getType().isSimpleType());
        assertEquals("String", ((SimpleType) fieldDeclarations[0].getType()).getName().getFullyQualifiedName());
        //
        assertEquals(1, fieldDeclarations[0].fragments().size());
        @SuppressWarnings("unchecked")
        VariableDeclarationFragment[] variableDeclarationFragments = (VariableDeclarationFragment[]) fieldDeclarations[0]
                .fragments().toArray(new VariableDeclarationFragment[fieldDeclarations[0].fragments().size()]);
        assertEquals(0, variableDeclarationFragments[0].getExtraDimensions());
        assertEquals("STR_CONST", variableDeclarationFragments[0].getName().getFullyQualifiedName());
        //
        assertNotNull(variableDeclarationFragments[0].getInitializer());
        assertTrue(variableDeclarationFragments[0].getInitializer().getClass().getName(),
                variableDeclarationFragments[0].getInitializer() instanceof InfixExpression);
        InfixExpression infixExpression = (InfixExpression) variableDeclarationFragments[0].getInitializer();
        assertTrue(infixExpression.getLeftOperand() instanceof StringLiteral);
        assertEquals("something is ; different \"//; /*;*/",
                ((StringLiteral) infixExpression.getLeftOperand()).getLiteralValue());
        assertTrue(infixExpression.getRightOperand() instanceof StringLiteral);
        assertEquals(" !!;;", ((StringLiteral) infixExpression.getRightOperand()).getLiteralValue());
        assertEquals("+", infixExpression.getOperator().toString());
    }
    //fieldDeclarations[1]: protected static long longStatic = 1l
    {
        Javadoc javadoc = fieldDeclarations[1].getJavadoc();
        assertEquals(1, javadoc.tags().size());
        assertEquals(2, ((TagElement) javadoc.tags().get(0)).fragments().size());
        assertEquals("protected static long field.",
                ((TextElement) ((TagElement) javadoc.tags().get(0)).fragments().get(0)).getText());
        assertEquals("This is a multiline comment.",
                ((TextElement) ((TagElement) javadoc.tags().get(0)).fragments().get(1)).getText());
        //
        assertEquals(Modifier.PROTECTED | Modifier.STATIC, fieldDeclarations[1].getModifiers());
        //
        assertTrue(fieldDeclarations[1].getType().isPrimitiveType());
        assertEquals(PrimitiveType.LONG,
                ((PrimitiveType) fieldDeclarations[1].getType()).getPrimitiveTypeCode());
        //
        assertEquals(1, fieldDeclarations[1].fragments().size());
        @SuppressWarnings("unchecked")
        VariableDeclarationFragment[] variableDeclarationFragments = (VariableDeclarationFragment[]) fieldDeclarations[1]
                .fragments().toArray(new VariableDeclarationFragment[fieldDeclarations[1].fragments().size()]);
        assertEquals(0, variableDeclarationFragments[0].getExtraDimensions());
        assertEquals("longStatic", variableDeclarationFragments[0].getName().getFullyQualifiedName());
        //
        assertNotNull(variableDeclarationFragments[0].getInitializer());
        assertTrue(variableDeclarationFragments[0].getInitializer() instanceof NumberLiteral);
        assertEquals("1l", ((NumberLiteral) variableDeclarationFragments[0].getInitializer()).getToken());
    }
    //fieldDeclarations[2]: Boolean booleanInstance
    {
        assertNull(fieldDeclarations[2].getJavadoc());
        //
        assertEquals(0, fieldDeclarations[2].getModifiers());
        //
        assertTrue(fieldDeclarations[2].getType().isSimpleType());
        assertEquals("Boolean",
                ((SimpleType) fieldDeclarations[2].getType()).getName().getFullyQualifiedName());
        //
        assertEquals(1, fieldDeclarations[2].fragments().size());
        @SuppressWarnings("unchecked")
        VariableDeclarationFragment[] variableDeclarationFragments = (VariableDeclarationFragment[]) fieldDeclarations[2]
                .fragments().toArray(new VariableDeclarationFragment[fieldDeclarations[2].fragments().size()]);
        assertEquals(0, variableDeclarationFragments[0].getExtraDimensions());
        assertEquals("booleanInstance", variableDeclarationFragments[0].getName().getFullyQualifiedName());
        //
        assertNull(variableDeclarationFragments[0].getInitializer());
    }
    //fieldDeclarations[3]: private Map.Entry myEntry
    {
        assertNull(fieldDeclarations[3].getJavadoc());
        //
        assertEquals(Modifier.PRIVATE, fieldDeclarations[3].getModifiers());
        //
        assertTrue(fieldDeclarations[3].getType().isSimpleType());
        assertEquals("Map.Entry",
                ((SimpleType) fieldDeclarations[3].getType()).getName().getFullyQualifiedName());
        //
        assertEquals(1, fieldDeclarations[3].fragments().size());
        @SuppressWarnings("unchecked")
        VariableDeclarationFragment[] variableDeclarationFragments = (VariableDeclarationFragment[]) fieldDeclarations[3]
                .fragments().toArray(new VariableDeclarationFragment[fieldDeclarations[3].fragments().size()]);
        assertEquals(0, variableDeclarationFragments[0].getExtraDimensions());
        assertEquals("myEntry", variableDeclarationFragments[0].getName().getFullyQualifiedName());
        //
        assertNull(variableDeclarationFragments[0].getInitializer());
    }
    //fieldDeclarations[4]: private int[][] myMatrix = new int[4][5]
    {
        assertNull(fieldDeclarations[4].getJavadoc());
        //
        assertEquals(Modifier.PRIVATE, fieldDeclarations[4].getModifiers());
        //
        assertTrue(fieldDeclarations[4].getType().isArrayType());
        assertEquals(2, ((ArrayType) fieldDeclarations[4].getType()).getDimensions());
        assertTrue(((ArrayType) fieldDeclarations[4].getType()).getElementType().isPrimitiveType());
        assertEquals(PrimitiveType.INT,
                ((PrimitiveType) ((ArrayType) fieldDeclarations[4].getType()).getElementType())
                        .getPrimitiveTypeCode());
        //
        assertEquals(1, fieldDeclarations[4].fragments().size());
        @SuppressWarnings("unchecked")
        VariableDeclarationFragment[] variableDeclarationFragments = (VariableDeclarationFragment[]) fieldDeclarations[4]
                .fragments().toArray(new VariableDeclarationFragment[fieldDeclarations[4].fragments().size()]);
        assertEquals(0, variableDeclarationFragments[0].getExtraDimensions());
        assertEquals("myMatrix", variableDeclarationFragments[0].getName().getFullyQualifiedName());
        //
        assertNotNull(variableDeclarationFragments[0].getInitializer());
        assertTrue(variableDeclarationFragments[0].getInitializer() instanceof ArrayCreation);
        ArrayCreation arrayCreation = (ArrayCreation) variableDeclarationFragments[0].getInitializer();
        assertEquals(2, arrayCreation.dimensions().size());
        assertEquals("4", ((NumberLiteral) arrayCreation.dimensions().get(0)).getToken());
        assertEquals("5", ((NumberLiteral) arrayCreation.dimensions().get(1)).getToken());
    }

    //** Methods
    MethodDeclaration[] methodDeclarations = exampleClass.getMethods();
    //methodDeclarations[0]: public Example1()
    {
        Javadoc javadoc = methodDeclarations[0].getJavadoc();
        assertEquals(1, javadoc.tags().size());
        assertEquals(1, ((TagElement) javadoc.tags().get(0)).fragments().size());
        assertEquals("This is a contructor",
                ((TextElement) ((TagElement) javadoc.tags().get(0)).fragments().get(0)).getText());
        //
        assertTrue(methodDeclarations[0].isConstructor());
        //
        assertEquals(Modifier.PUBLIC, methodDeclarations[0].getModifiers());
        //
        assertNull(methodDeclarations[0].getReturnType2());
        //
        assertEquals("Example1", methodDeclarations[0].getName().getFullyQualifiedName());
        //
        assertTrue(methodDeclarations[0].parameters().isEmpty());
        //
        assertNotNull(methodDeclarations[0].getBody());
        assertEquals(1, methodDeclarations[0].getBody().statements().size());
        Statement statement = (Statement) methodDeclarations[0].getBody().statements().get(0);
        assertEquals(ASTNode.SUPER_CONSTRUCTOR_INVOCATION, statement.getNodeType());
        assertTrue(((SuperConstructorInvocation) statement).arguments().isEmpty());
    }
    //methodDeclarations[2]: public void setBooleanInstance(Boolean b)
    {
        Javadoc javadoc = methodDeclarations[2].getJavadoc();
        assertEquals(3, javadoc.tags().size());
        assertNull(((TagElement) javadoc.tags().get(0)).getTagName());
        assertEquals(1, ((TagElement) javadoc.tags().get(0)).fragments().size());
        assertEquals("Sets the boolean instance.",
                ((TextElement) ((TagElement) javadoc.tags().get(0)).fragments().get(0)).getText());
        assertEquals("@param", ((TagElement) javadoc.tags().get(1)).getTagName());
        assertEquals(1, ((TagElement) javadoc.tags().get(0)).fragments().size());
        assertEquals("b",
                ((SimpleName) ((TagElement) javadoc.tags().get(1)).fragments().get(0)).getFullyQualifiedName());
        assertEquals("@generated", ((TagElement) javadoc.tags().get(2)).getTagName());
        assertTrue(((TagElement) javadoc.tags().get(2)).fragments().isEmpty());
        //
        assertFalse(methodDeclarations[2].isConstructor());
        //
        assertEquals(Modifier.PUBLIC, methodDeclarations[2].getModifiers());
        //
        assertNotNull(methodDeclarations[2].getReturnType2());
        assertTrue(methodDeclarations[2].getReturnType2().isPrimitiveType());
        assertEquals(PrimitiveType.VOID,
                ((PrimitiveType) methodDeclarations[2].getReturnType2()).getPrimitiveTypeCode());
        //
        assertEquals("setBooleanInstance", methodDeclarations[2].getName().getFullyQualifiedName());
        //
        assertEquals(1, methodDeclarations[2].parameters().size());
        assertTrue(((SingleVariableDeclaration) methodDeclarations[2].parameters().get(0)).getType()
                .isSimpleType());
        assertEquals("Boolean",
                ((SimpleType) ((SingleVariableDeclaration) methodDeclarations[2].parameters().get(0)).getType())
                        .getName().getFullyQualifiedName());
        assertEquals("b", ((SingleVariableDeclaration) methodDeclarations[2].parameters().get(0)).getName()
                .getFullyQualifiedName());
        //
        assertNotNull(methodDeclarations[2].getBody());
        assertEquals(1, methodDeclarations[2].getBody().statements().size());
    }
}

From source file:org.eclipse.jpt.common.core.internal.resource.java.source.SourceType.java

License:Open Source License

public void resolveTypes(TypeDeclaration typeDeclaration) {

    this.syncSuperclassQualifiedName(this.buildSuperclassQualifiedName(typeDeclaration.resolveBinding()));

    FieldDeclaration[] fieldDeclarations = typeDeclaration.getFields();
    CounterMap counters = new CounterMap(fieldDeclarations.length);
    HashSet<JavaResourceField> remainingFields = new HashSet<JavaResourceField>(this.fields);
    for (FieldDeclaration fieldDeclaration : fieldDeclarations) {
        for (VariableDeclarationFragment fragment : fragments(fieldDeclaration)) {
            String fieldName = fragment.getName().getFullyQualifiedName();
            int occurrence = counters.increment(fieldName);

            JavaResourceField field = getField(remainingFields, fieldName, occurrence);
            field.resolveTypes(fieldDeclaration, fragment);
            remainingFields.remove(field);
        }/*w w w .  j  a  v a 2s.co m*/
    }

    // a new type can trigger a method parameter type to be a resolved,
    // fully-qualified name, so we need to rebuild our list of methods:
    //     "setFoo(Foo)" is not the same as "setFoo(com.bar.Foo)"
    // and, vice-versa, a removed type can "unresolve" a parameter type
    //thus we are not calling resolveTypes on methods, that would be redundant to syncMethods 
    this.syncMethods(typeDeclaration);

    TypeDeclaration[] typeDeclarations = typeDeclaration.getTypes();
    int i = 0;
    for (JavaResourceType type : this.getTypes()) {
        type.resolveTypes(typeDeclarations[i++]);
    }

    EnumDeclaration[] enumDeclarations = enums(typeDeclaration);
    i = 0;
    for (JavaResourceEnum enum_ : this.getEnums()) {
        enum_.resolveTypes(enumDeclarations[i++]);
    }
}