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

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

Introduction

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

Prototype

public boolean isPackageMemberTypeDeclaration() 

Source Link

Document

Returns whether this type declaration is a package member (that is, a top-level type).

Usage

From source file:ca.ecliptical.pde.internal.ds.AnnotationProcessor.java

License:Open Source License

@Override
public boolean visit(TypeDeclaration type) {
    if (!Modifier.isPublic(type.getModifiers())) {
        // non-public types cannot be (or have nested) components
        if (errorLevel.isNone())
            return false;

        Annotation annotation = findComponentAnnotation(type);
        if (annotation != null)
            reportProblem(annotation, null, problems,
                    NLS.bind(Messages.AnnotationProcessor_invalidComponentImplementationClass,
                            type.getName().getIdentifier()),
                    type.getName().getIdentifier());

        return true;
    }/*from  w ww  .  ja  va2  s  . c om*/

    Annotation annotation = findComponentAnnotation(type);
    if (annotation != null) {
        if (type.isInterface() || Modifier.isAbstract(type.getModifiers())
                || (!type.isPackageMemberTypeDeclaration() && !isNestedPublicStatic(type))
                || !hasDefaultConstructor(type)) {
            // interfaces, abstract types, non-static/non-public nested types, or types with no default constructor cannot be components
            reportProblem(annotation, null, problems,
                    NLS.bind(Messages.AnnotationProcessor_invalidComponentImplementationClass,
                            type.getName().getIdentifier()),
                    type.getName().getIdentifier());
        } else {
            ITypeBinding typeBinding = type.resolveBinding();
            if (typeBinding == null) {
                if (debug.isDebugging())
                    debug.trace(String.format("Unable to resolve binding for type: %s", type)); //$NON-NLS-1$
            } else {
                IAnnotationBinding annotationBinding = annotation.resolveAnnotationBinding();
                if (annotationBinding == null) {
                    if (debug.isDebugging())
                        debug.trace(String.format("Unable to resolve binding for annotation: %s", annotation)); //$NON-NLS-1$
                } else {
                    IDSModel model = processComponent(type, typeBinding, annotation, annotationBinding,
                            problems);
                    models.add(model);
                }
            }
        }
    }

    return true;
}

From source file:ca.ecliptical.pde.internal.ds.AnnotationProcessor.java

License:Open Source License

private boolean isNestedPublicStatic(TypeDeclaration type) {
    if (Modifier.isStatic(type.getModifiers())) {
        ASTNode parent = type.getParent();
        if (parent != null && parent.getNodeType() == ASTNode.TYPE_DECLARATION) {
            TypeDeclaration parentType = (TypeDeclaration) parent;
            if (Modifier.isPublic(parentType.getModifiers()))
                return parentType.isPackageMemberTypeDeclaration() || isNestedPublicStatic(parentType);
        }/*from  w  w w .  j  a v  a  2s .  c om*/
    }

    return false;
}

From source file:com.motorola.studio.android.generatecode.BasicCodeVisitor.java

License:Apache License

@Override
public boolean visit(TypeDeclaration node) {
    boolean result = super.visit(node);
    if (node.isPackageMemberTypeDeclaration()) {
        //only keep if it is the top level class
        typeDeclaration = node;//ww  w. ja  v  a 2 s  .  c  o m
    }
    return result;
}

From source file:edu.uci.ics.sourcerer.extractor.ast.ReferenceExtractorVisitor.java

License:Open Source License

/**
 * This method writes:/*  w  w w.java  2 s. c  o m*/
 * <ul>
 *   <li>Non-anonymous class or interface entity to <code>IEntityWriter</code>.
 *   <ul>
 *     <li>Inside relation to <code>IRelationWriter</code>.</li>
 *     <li>Extends relation to <code>IRelationWriter</code>.</li>
 *     <li>Implements relation to <code>IRelationWriter</code>.</li>
 *     <li>Synthesized constructors to <code>IEntityWriter</code>.
 *     <ul>
 *       <li>Inside relation to <code>IRelationWriter</code>.</li>
 *       <li>Implicit superconstructor calls to <code>IRelationWriter</code>.</li>
 *     </ul></li>
 *   </ul></li>
 * </ul>
 * 
 * Class/interface fully qualified names (FQNs) adhere to the following format:
 * <ul>
 * <li>Top-level: package fqn + . + simple name</li>
 * <li>Member: parent fqn + $ + simple name</li>
 * <li>Local: parent fqn + $local- + incrementing counter + - + simple name</li>
 * </ul>
 */
@SuppressWarnings("unchecked")
@Override
public boolean visit(TypeDeclaration node) {
    // Get the fqn
    String fqn = null;
    Entity type = null;
    ITypeBinding binding = node.resolveBinding();
    if (binding == null && !bindingFree) {
        throw new IllegalStateException("Binding resolution appears to have failed!");
    }
    if (node.isPackageMemberTypeDeclaration()) {
        fqn = fqnStack.getTypeFqn(node.getName().getIdentifier());
    } else if (node.isMemberTypeDeclaration()) {
        if (node.getName().getIdentifier().length() > 0) {
            fqn = fqnStack.getMemberFqn(node.getName().getIdentifier());
        } else {
            logger.severe("A type declaration should not declare an annonymous type!");
            fqn = fqnStack.getAnonymousClassFqn();
        }
    } else if (node.isLocalTypeDeclaration()) {
        if (binding == null) {
            fqn = getUnknownFqn(node.getName().getIdentifier());
        } else {
            fqn = fqnStack.getLocalFqn(node.getName().getIdentifier(), binding);
        }
    } else {
        logger.severe("Unsure what type the declaration is!");
        fqn = "(ERROR)" + node.getName().getIdentifier();
    }

    // Write the entity
    Metrics metrics = MetricsCalculator.computeLinesOfCode(getSource(node));
    if (node.isInterface()) {
        entityWriter.writeInterface(fqn, node.getModifiers(), metrics, getLocation(node));
        type = Entity.INTERFACE;
    } else {
        entityWriter.writeClass(fqn, node.getModifiers(), metrics, getLocation(node));
        type = Entity.CLASS;
    }

    // Write the inside relation
    relationWriter.writeInside(fqn, fqnStack.getFqn(), getUnknownLocation());

    // Write the extends relation
    Type superType = node.getSuperclassType();
    String superFqn = null;
    if (superType != null) {
        superFqn = getTypeFqn(superType);
        relationWriter.writeExtends(fqn, superFqn, getLocation(superType));
    }

    // Write the implements relation
    List<Type> superInterfaceTypes = node.superInterfaceTypes();
    for (Type superInterfaceType : superInterfaceTypes) {
        String superInterfaceFqn = getTypeFqn(superInterfaceType);
        relationWriter.writeImplements(fqn, superInterfaceFqn, getLocation(superInterfaceType));
    }

    fqnStack.push(fqn, type);

    if (binding != null) {
        if (binding.isAnonymous()) {
            logger.log(Level.SEVERE, "A type declaration should not declare an annonymous type!");
        } else {
            // Verify the fqn
            //        String fqn2 = getTypeFqn(binding);
            //        if (!fqn.equals(fqn2)) {
            //          logger.log(Level.SEVERE, "Mismatch between " + fqn + " and " + fqn2);
            //        }

            // Write out the synthesized constructors
            for (IMethodBinding method : binding.getDeclaredMethods()) {
                if (method.isDefaultConstructor()) {
                    // Write the entity
                    String constructorFqn = getMethodFqn(method, true);
                    entityWriter.writeConstructor(constructorFqn, method.getModifiers(),
                            MetricsCalculator.computeLinesOfCode(null), getUnknownLocation());

                    // Write the inside relation
                    relationWriter.writeInside(constructorFqn, fqn, getUnknownLocation());

                    // Write the calls relation (implicit superconstructor call)
                    if (superFqn == null) {
                        relationWriter.writeCalls(constructorFqn, "java.lang.Object.<init>()",
                                getUnknownLocation());
                    } else {
                        relationWriter.writeCalls(constructorFqn, superFqn + ".<init>()", getUnknownLocation());
                    }
                }
            }
        }
    }

    accept(node.getJavadoc());
    accept(node.typeParameters());
    accept(node.getSuperclassType());
    accept(node.superInterfaceTypes());
    accept(node.bodyDeclarations());

    return false;
}

From source file:edu.uci.ics.sourcerer.tools.java.extractor.eclipse.ReferenceExtractorVisitor.java

License:Open Source License

/**
 * This method writes://from   ww w .  j  a  v a 2 s . c  o m
 * <ul>
 *   <li>Non-anonymous class or interface entity to <code>IEntityWriter</code>.
 *   <ul>
 *     <li>Inside relation to <code>IRelationWriter</code>.</li>
 *     <li>Extends relation to <code>IRelationWriter</code>.</li>
 *     <li>Implements relation to <code>IRelationWriter</code>.</li>
 *     <li>Synthesized constructors to <code>IEntityWriter</code>.
 *     <ul>
 *       <li>Inside relation to <code>IRelationWriter</code>.</li>
 *       <li>Implicit superconstructor calls to <code>IRelationWriter</code>.</li>
 *     </ul></li>
 *   </ul></li>
 * </ul>
 * 
 * Class/interface fully qualified names (FQNs) adhere to the following format:
 * <ul>
 * <li>Top-level: package fqn + . + simple name</li>
 * <li>Member: parent fqn + $ + simple name</li>
 * <li>Local: parent fqn + $ + incrementing counter + simple name</li>
 * </ul>
 */
@SuppressWarnings("unchecked")
@Override
public boolean visit(TypeDeclaration node) {
    // Get the fqn
    String parentFqn = null;
    String fqn = null;
    Entity type = null;
    ITypeBinding binding = node.resolveBinding();
    if (binding == null && !bindingFree) {
        throw new IllegalStateException("Binding resolution appears to have failed!");
    }
    if (node.isPackageMemberTypeDeclaration()) {
        EnclosingPackage enc = fqnStack.peek(EnclosingPackage.class);
        fqn = enc.getTypeFqn(node.getName().getIdentifier());
        parentFqn = enc.getFqn();
    } else if (node.isMemberTypeDeclaration()) {
        if (node.getName().getIdentifier().length() > 0) {
            EnclosingDeclaredType enc = fqnStack.peek(EnclosingDeclaredType.class);
            fqn = enc.getMemberFqn(node.getName().getIdentifier());
            parentFqn = enc.getFqn();
        } else {
            throw new IllegalStateException("A type declaration should not declare an annonymous type!");
        }
    } else if (node.isLocalTypeDeclaration()) {
        EnclosingBlock enc = fqnStack.peek(EnclosingBlock.class);
        if (binding == null) {
            fqn = createUnknownFqn(node.getName().getIdentifier());
        } else {
            fqn = enc.getLocalFqn(node.getName().getIdentifier(), binding);
        }
        parentFqn = enc.getFqn();
    } else {
        throw new IllegalStateException("Unknown declaration: " + node);
    }

    if (node.isInterface()) {
        type = Entity.INTERFACE;
    } else {
        type = Entity.CLASS;
    }

    // Push the stack
    fqnStack.push(fqn, type);

    // Visit the children
    accept(node.getJavadoc());
    accept(node.modifiers());
    accept(node.typeParameters());
    accept(node.getSuperclassType());
    accept(node.superInterfaceTypes());
    accept(node.bodyDeclarations());

    // Write the contains relation
    relationWriter.writeRelation(Relation.CONTAINS, parentFqn, fqn, createUnknownLocation());

    // Write the entity
    entityWriter.writeEntity(type, fqn, node.getModifiers(), createMetrics(node), createLocation(node));

    // Write the extends relation
    Type superType = node.getSuperclassType();
    String superFqn = null;
    if (superType == null) {
        if (!fqn.equals("java.lang.Object")) {
            superFqn = "java.lang.Object";
            relationWriter.writeRelation(Relation.EXTENDS, fqn, superFqn, createUnknownLocation());
        }
    } else {
        superFqn = getTypeFqn(superType);
        relationWriter.writeRelation(Relation.EXTENDS, fqn, superFqn, createLocation(superType));
    }

    // Write the implements relation
    List<Type> superInterfaceTypes = node.superInterfaceTypes();
    for (Type superInterfaceType : superInterfaceTypes) {
        String superInterfaceFqn = getTypeFqn(superInterfaceType);
        relationWriter.writeRelation(Relation.IMPLEMENTS, fqn, superInterfaceFqn,
                createLocation(superInterfaceType));
    }

    if (binding != null) {
        if (binding.isAnonymous()) {
            logger.log(Level.SEVERE, "A type declaration should not declare an annonymous type!");
        } else {
            // Write out the synthesized constructors
            for (IMethodBinding method : binding.getDeclaredMethods()) {
                if (method.isDefaultConstructor()) {
                    // Write the entity
                    String constructorFqn = getMethodName(method, true);
                    entityWriter.writeEntity(Entity.CONSTRUCTOR, constructorFqn, "()", null,
                            method.getModifiers(), null, createUnknownLocation());
                    constructorFqn += "()";
                    // Write the contains relation
                    relationWriter.writeRelation(Relation.CONTAINS, fqn, constructorFqn,
                            createUnknownLocation());

                    // Write the calls relation (implicit superconstructor call)
                    if (superFqn == null) {
                        relationWriter.writeRelation(Relation.CALLS, constructorFqn,
                                "java.lang.Object.<init>()", createUnknownLocation());
                    } else {
                        if (superType == null) {
                            relationWriter.writeRelation(Relation.CALLS, constructorFqn, superFqn + ".<init>()",
                                    createUnknownLocation());
                        } else {
                            relationWriter.writeRelation(Relation.CALLS, constructorFqn,
                                    getErasedTypeFqn(superType) + ".<init>()", createUnknownLocation());
                        }
                    }
                }
            }
        }
    }

    fqnStack.pop();

    return false;
}

From source file:org.eclipse.andmore.android.generatecode.BasicCodeVisitor.java

License:Apache License

@Override
public boolean visit(TypeDeclaration node) {
    boolean result = super.visit(node);
    if (node.isPackageMemberTypeDeclaration()) {
        // only keep if it is the top level class
        typeDeclaration = node;/*  w w w.j  a va 2 s.c  o m*/
    }
    return result;
}

From source file:org.eclipse.pde.ds.internal.annotations.AnnotationProcessor.java

License:Open Source License

@Override
public boolean visit(TypeDeclaration type) {
    if (!Modifier.isPublic(type.getModifiers())) {
        // non-public types cannot be (or have nested) components
        if (errorLevel.isIgnore())
            return false;

        Annotation annotation = findComponentAnnotation(type);
        if (annotation != null)
            reportProblem(annotation, null, problems,
                    NLS.bind(Messages.AnnotationProcessor_invalidCompImplClass_notPublic,
                            type.getName().getIdentifier()),
                    type.getName().getIdentifier());

        return true;
    }/*from   w w  w.j a  v  a  2  s  .c  o  m*/

    Annotation annotation = findComponentAnnotation(type);
    if (annotation != null) {
        boolean isInterface = false;
        boolean isAbstract = false;
        boolean isNested = false;
        boolean noDefaultConstructor = false;
        if ((isInterface = type.isInterface()) || (isAbstract = Modifier.isAbstract(type.getModifiers()))
                || (isNested = (!type.isPackageMemberTypeDeclaration() && !isNestedPublicStatic(type)))
                || (noDefaultConstructor = !hasDefaultConstructor(type))) {
            // interfaces, abstract types, non-static/non-public nested types, or types with no default constructor cannot be components
            if (errorLevel != ValidationErrorLevel.ignore) {
                if (isInterface)
                    reportProblem(annotation, null, problems,
                            NLS.bind(Messages.AnnotationProcessor_invalidCompImplClass_interface,
                                    type.getName().getIdentifier()),
                            type.getName().getIdentifier());
                else if (isAbstract)
                    reportProblem(annotation, null, problems,
                            NLS.bind(Messages.AnnotationProcessor_invalidCompImplClass_abstract,
                                    type.getName().getIdentifier()),
                            type.getName().getIdentifier());
                else if (isNested)
                    reportProblem(annotation, null, problems,
                            NLS.bind(Messages.AnnotationProcessor_invalidCompImplClass_notTopLevel,
                                    type.getName().getIdentifier()),
                            type.getName().getIdentifier());
                else if (noDefaultConstructor)
                    reportProblem(annotation, null, problems,
                            NLS.bind(Messages.AnnotationProcessor_invalidCompImplClass_noDefaultConstructor,
                                    type.getName().getIdentifier()),
                            type.getName().getIdentifier());
                else
                    reportProblem(annotation, null, problems,
                            NLS.bind(Messages.AnnotationProcessor_invalidComponentImplementationClass,
                                    type.getName().getIdentifier()),
                            type.getName().getIdentifier());
            }
        } else {
            ITypeBinding typeBinding = type.resolveBinding();
            if (typeBinding == null) {
                if (debug.isDebugging())
                    debug.trace(String.format("Unable to resolve binding for type: %s", type)); //$NON-NLS-1$
            } else {
                IAnnotationBinding annotationBinding = annotation.resolveAnnotationBinding();
                if (annotationBinding == null) {
                    if (debug.isDebugging())
                        debug.trace(String.format("Unable to resolve binding for annotation: %s", annotation)); //$NON-NLS-1$
                } else {
                    try {
                        processComponent(type, typeBinding, annotation, annotationBinding, problems);
                    } catch (CoreException e) {
                        Activator.log(e);
                    }
                }
            }
        }
    }

    return true;
}