Example usage for org.eclipse.jdt.internal.compiler.ast ASTNode IsAnonymousType

List of usage examples for org.eclipse.jdt.internal.compiler.ast ASTNode IsAnonymousType

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast ASTNode IsAnonymousType.

Prototype

int IsAnonymousType

To view the source code for org.eclipse.jdt.internal.compiler.ast ASTNode IsAnonymousType.

Click Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java

License:Open Source License

private Initializer convert(InitializerElementInfo initializerInfo, CompilationResult compilationResult)
        throws JavaModelException {

    Block block = new Block(0);
    Initializer initializer = new Initializer(block, ClassFileConstants.AccDefault);

    int start = initializerInfo.getDeclarationSourceStart();
    int end = initializerInfo.getDeclarationSourceEnd();

    initializer.sourceStart = initializer.declarationSourceStart = start;
    initializer.sourceEnd = initializer.declarationSourceEnd = end;
    initializer.modifiers = initializerInfo.getModifiers();

    /* convert local and anonymous types */
    IJavaElement[] children = initializerInfo.getChildren();
    int typesLength = children.length;
    if (typesLength > 0) {
        Statement[] statements = new Statement[typesLength];
        for (int i = 0; i < typesLength; i++) {
            SourceType type = (SourceType) children[i];
            TypeDeclaration localType = convert(type, compilationResult);
            if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
                QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
                expression.type = localType.superclass;
                localType.superclass = null;
                localType.superInterfaces = null;
                localType.allocation = expression;
                statements[i] = expression;
            } else {
                statements[i] = localType;
            }/*from  w w w .j  ava  2  s.  com*/
        }
        block.statements = statements;
    }

    return initializer;
}

From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java

License:Open Source License

private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo,
        CompilationResult compilationResult) throws JavaModelException {
    AbstractMethodDeclaration method;//  w  w w. ja  v  a 2 s. c  o m

    /* only source positions available */
    int start = methodInfo.getNameSourceStart();
    int end = methodInfo.getNameSourceEnd();

    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, Even when this type is being constructed
       on behalf of a 1.4 project we must internalize type variables properly in order to be able to
       recognize usages of them in the method signature, to apply substitutions and thus to be able to
       detect overriding in the presence of generics. If we simply drop them, when the method signature
       refers to the type parameter, we won't know it should be bound to the type parameter and perform
       incorrect lookup and may mistakenly end up with missing types
     */
    TypeParameter[] typeParams = null;
    char[][] typeParameterNames = methodInfo.getTypeParameterNames();
    if (typeParameterNames != null) {
        int parameterCount = typeParameterNames.length;
        if (parameterCount > 0) { // method's type parameters must be null if no type parameter
            char[][][] typeParameterBounds = methodInfo.getTypeParameterBounds();
            typeParams = new TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                typeParams[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
            }
        }
    }

    int modifiers = methodInfo.getModifiers();
    if (methodInfo.isConstructor()) {
        ConstructorDeclaration decl = new ConstructorDeclaration(compilationResult);
        decl.bits &= ~ASTNode.IsDefaultConstructor;
        method = decl;
        decl.typeParameters = typeParams;
    } else {
        MethodDeclaration decl;
        if (methodInfo.isAnnotationMethod()) {
            AnnotationMethodDeclaration annotationMethodDeclaration = new AnnotationMethodDeclaration(
                    compilationResult);

            /* conversion of default value */
            SourceAnnotationMethodInfo annotationMethodInfo = (SourceAnnotationMethodInfo) methodInfo;
            boolean hasDefaultValue = annotationMethodInfo.defaultValueStart != -1
                    || annotationMethodInfo.defaultValueEnd != -1;
            if ((this.flags & FIELD_INITIALIZATION) != 0) {
                if (hasDefaultValue) {
                    char[] defaultValueSource = CharOperation.subarray(getSource(),
                            annotationMethodInfo.defaultValueStart, annotationMethodInfo.defaultValueEnd + 1);
                    if (defaultValueSource != null) {
                        Expression expression = parseMemberValue(defaultValueSource);
                        if (expression != null) {
                            annotationMethodDeclaration.defaultValue = expression;
                        }
                    } else {
                        // could not retrieve the default value
                        hasDefaultValue = false;
                    }
                }
            }
            if (hasDefaultValue)
                modifiers |= ClassFileConstants.AccAnnotationDefault;
            decl = annotationMethodDeclaration;
        } else {
            decl = new MethodDeclaration(compilationResult);
        }

        // convert return type
        decl.returnType = createTypeReference(methodInfo.getReturnTypeName(), start, end);

        // type parameters
        decl.typeParameters = typeParams;

        method = decl;
    }
    method.selector = methodHandle.getElementName().toCharArray();
    boolean isVarargs = (modifiers & ClassFileConstants.AccVarargs) != 0;
    method.modifiers = modifiers & ~ClassFileConstants.AccVarargs;
    method.sourceStart = start;
    method.sourceEnd = end;
    method.declarationSourceStart = methodInfo.getDeclarationSourceStart();
    method.declarationSourceEnd = methodInfo.getDeclarationSourceEnd();

    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        method.annotations = convertAnnotations(methodHandle);
    }

    /* convert arguments */
    String[] argumentTypeSignatures = methodHandle.getParameterTypes();
    char[][] argumentNames = methodInfo.getArgumentNames();
    int argumentCount = argumentTypeSignatures == null ? 0 : argumentTypeSignatures.length;
    if (argumentCount > 0) {
        ILocalVariable[] parameters = methodHandle.getParameters();
        long position = ((long) start << 32) + end;
        method.arguments = new Argument[argumentCount];
        for (int i = 0; i < argumentCount; i++) {
            TypeReference typeReference = createTypeReference(argumentTypeSignatures[i], start, end);
            if (isVarargs && i == argumentCount - 1) {
                typeReference.bits |= ASTNode.IsVarArgs;
            }
            method.arguments[i] = new Argument(argumentNames[i], position, typeReference,
                    ClassFileConstants.AccDefault);
            // do not care whether was final or not
            // convert 1.5 specific constructs only if compliance is 1.5 or above
            if (this.has1_5Compliance) {
                /* convert annotations */
                method.arguments[i].annotations = convertAnnotations(parameters[i]);
            }
        }
    }

    /* convert thrown exceptions */
    char[][] exceptionTypeNames = methodInfo.getExceptionTypeNames();
    int exceptionCount = exceptionTypeNames == null ? 0 : exceptionTypeNames.length;
    if (exceptionCount > 0) {
        method.thrownExceptions = new TypeReference[exceptionCount];
        for (int i = 0; i < exceptionCount; i++) {
            method.thrownExceptions[i] = createTypeReference(exceptionTypeNames[i], start, end);
        }
    }

    /* convert local and anonymous types */
    if ((this.flags & LOCAL_TYPE) != 0) {
        IJavaElement[] children = methodInfo.getChildren();
        int typesLength = children.length;
        if (typesLength != 0) {
            Statement[] statements = new Statement[typesLength];
            for (int i = 0; i < typesLength; i++) {
                SourceType type = (SourceType) children[i];
                TypeDeclaration localType = convert(type, compilationResult);
                if ((localType.bits & ASTNode.IsAnonymousType) != 0) {
                    QualifiedAllocationExpression expression = new QualifiedAllocationExpression(localType);
                    expression.type = localType.superclass;
                    localType.superclass = null;
                    localType.superInterfaces = null;
                    localType.allocation = expression;
                    statements[i] = expression;
                } else {
                    statements[i] = localType;
                }
            }
            method.statements = statements;
        }
    }

    return method;
}

From source file:com.codenvy.ide.ext.java.server.internal.compiler.parser.SourceTypeConverter.java

License:Open Source License

private TypeDeclaration convert(SourceType typeHandle, CompilationResult compilationResult)
        throws JavaModelException {
    SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) typeHandle.getElementInfo();
    if (typeInfo.isAnonymousMember())
        throw new AnonymousMemberFound();
    /* create type declaration - can be member type */
    TypeDeclaration type = new TypeDeclaration(compilationResult);
    if (typeInfo.getEnclosingType() == null) {
        if (typeHandle.isAnonymous()) {
            type.name = CharOperation.NO_CHAR;
            type.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType);
        } else {/*from www .  j a  va  2 s .  co m*/
            if (typeHandle.isLocal()) {
                type.bits |= ASTNode.IsLocalType;
            }
        }
    } else {
        type.bits |= ASTNode.IsMemberType;
    }
    if ((type.bits & ASTNode.IsAnonymousType) == 0) {
        type.name = typeInfo.getName();
    }
    type.name = typeInfo.getName();
    int start, end; // only positions available
    type.sourceStart = start = typeInfo.getNameSourceStart();
    type.sourceEnd = end = typeInfo.getNameSourceEnd();
    type.modifiers = typeInfo.getModifiers();
    type.declarationSourceStart = typeInfo.getDeclarationSourceStart();
    type.declarationSourceEnd = typeInfo.getDeclarationSourceEnd();
    type.bodyEnd = type.declarationSourceEnd;

    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        type.annotations = convertAnnotations(typeHandle);
    }
    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, even in a 1.4 project, we
       must internalize type variables and observe any parameterization of super class
       and/or super interfaces in order to be able to detect overriding in the presence
       of generics.
     */
    char[][] typeParameterNames = typeInfo.getTypeParameterNames();
    if (typeParameterNames.length > 0) {
        int parameterCount = typeParameterNames.length;
        char[][][] typeParameterBounds = typeInfo.getTypeParameterBounds();
        type.typeParameters = new TypeParameter[parameterCount];
        for (int i = 0; i < parameterCount; i++) {
            type.typeParameters[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start,
                    end);
        }
    }

    /* set superclass and superinterfaces */
    if (typeInfo.getSuperclassName() != null) {
        type.superclass = createTypeReference(typeInfo.getSuperclassName(), start, end,
                true /* include generics */);
        type.superclass.bits |= ASTNode.IsSuperType;
    }
    char[][] interfaceNames = typeInfo.getInterfaceNames();
    int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length;
    if (interfaceCount > 0) {
        type.superInterfaces = new TypeReference[interfaceCount];
        for (int i = 0; i < interfaceCount; i++) {
            type.superInterfaces[i] = createTypeReference(interfaceNames[i], start, end,
                    true /* include generics */);
            type.superInterfaces[i].bits |= ASTNode.IsSuperType;
        }
    }
    /* convert member types */
    if ((this.flags & MEMBER_TYPE) != 0) {
        SourceType[] sourceMemberTypes = typeInfo.getMemberTypeHandles();
        int sourceMemberTypeCount = sourceMemberTypes.length;
        type.memberTypes = new TypeDeclaration[sourceMemberTypeCount];
        for (int i = 0; i < sourceMemberTypeCount; i++) {
            type.memberTypes[i] = convert(sourceMemberTypes[i], compilationResult);
            type.memberTypes[i].enclosingType = type;
        }
    }

    /* convert intializers and fields*/
    InitializerElementInfo[] initializers = null;
    int initializerCount = 0;
    if ((this.flags & LOCAL_TYPE) != 0) {
        initializers = typeInfo.getInitializers();
        initializerCount = initializers.length;
    }
    SourceField[] sourceFields = null;
    int sourceFieldCount = 0;
    if ((this.flags & FIELD) != 0) {
        sourceFields = typeInfo.getFieldHandles();
        sourceFieldCount = sourceFields.length;
    }
    int length = initializerCount + sourceFieldCount;
    if (length > 0) {
        type.fields = new FieldDeclaration[length];
        for (int i = 0; i < initializerCount; i++) {
            type.fields[i] = convert(initializers[i], compilationResult);
        }
        int index = 0;
        for (int i = initializerCount; i < length; i++) {
            type.fields[i] = convert(sourceFields[index++], type, compilationResult);
        }
    }

    /* convert methods - need to add default constructor if necessary */
    boolean needConstructor = (this.flags & CONSTRUCTOR) != 0;
    boolean needMethod = (this.flags & METHOD) != 0;
    if (needConstructor || needMethod) {

        SourceMethod[] sourceMethods = typeInfo.getMethodHandles();
        int sourceMethodCount = sourceMethods.length;

        /* source type has a constructor ?           */
        /* by default, we assume that one is needed. */
        int extraConstructor = 0;
        int methodCount = 0;
        int kind = TypeDeclaration.kind(type.modifiers);
        boolean isAbstract = kind == TypeDeclaration.INTERFACE_DECL
                || kind == TypeDeclaration.ANNOTATION_TYPE_DECL;
        if (!isAbstract) {
            extraConstructor = needConstructor ? 1 : 0;
            for (int i = 0; i < sourceMethodCount; i++) {
                if (sourceMethods[i].isConstructor()) {
                    if (needConstructor) {
                        extraConstructor = 0; // Does not need the extra constructor since one constructor already exists.
                        methodCount++;
                    }
                } else if (needMethod) {
                    methodCount++;
                }
            }
        } else {
            methodCount = needMethod ? sourceMethodCount : 0;
        }
        type.methods = new AbstractMethodDeclaration[methodCount + extraConstructor];
        if (extraConstructor != 0) { // add default constructor in first position
            type.methods[0] = type.createDefaultConstructor(false, false);
        }
        int index = 0;
        boolean hasAbstractMethods = false;
        for (int i = 0; i < sourceMethodCount; i++) {
            SourceMethod sourceMethod = sourceMethods[i];
            SourceMethodElementInfo methodInfo = (SourceMethodElementInfo) sourceMethod.getElementInfo();
            boolean isConstructor = methodInfo.isConstructor();
            if ((methodInfo.getModifiers() & ClassFileConstants.AccAbstract) != 0) {
                hasAbstractMethods = true;
            }
            if ((isConstructor && needConstructor) || (!isConstructor && needMethod)) {
                AbstractMethodDeclaration method = convert(sourceMethod, methodInfo, compilationResult);
                if (isAbstract || method.isAbstract()) { // fix-up flag
                    method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
                }
                type.methods[extraConstructor + index++] = method;
            }
        }
        if (hasAbstractMethods)
            type.bits |= ASTNode.HasAbstractMethods;
    }

    return type;
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Visit the given type declaration and report the nodes that match exactly the
 * search pattern (i.e. the ones in the matching nodes set)
 *//*from www.j  av  a 2s . c  om*/
protected void reportMatching(TypeDeclaration type, IJavaElement parent, int accuracy, MatchingNodeSet nodeSet,
        int occurrenceCount) throws CoreException {
    // create type handle
    IJavaElement enclosingElement = parent;
    if (enclosingElement == null) {
        enclosingElement = createTypeHandle(new String(type.name));
    } else if (enclosingElement instanceof IType) {
        enclosingElement = ((IType) parent).getType(new String(type.name));
    } else if (enclosingElement instanceof IMember) {
        IMember member = (IMember) parent;
        if (member.isBinary()) {
            enclosingElement = ((IClassFile) this.currentPossibleMatch.openable).getType();
        } else {
            enclosingElement = member.getType(new String(type.name), occurrenceCount);
        }
    }
    if (enclosingElement == null)
        return;
    boolean enclosesElement = encloses(enclosingElement);

    // report the type declaration
    if (accuracy > -1 && enclosesElement) {
        int offset = type.sourceStart;
        SearchMatch match = this.patternLocator.newDeclarationMatch(type, enclosingElement, type.binding,
                accuracy, type.sourceEnd - offset + 1, this);
        report(match);
    }

    boolean matchedClassContainer = (this.matchContainer & PatternLocator.CLASS_CONTAINER) != 0;

    // report the type parameters
    if (type.typeParameters != null) {
        reportMatching(type.typeParameters, enclosingElement, parent, type.binding, nodeSet);
    }

    // report annotations
    if (type.annotations != null) {
        reportMatching(type.annotations, enclosingElement, null, type.binding, nodeSet, matchedClassContainer,
                enclosesElement);
    }

    // report references in javadoc
    if (type.javadoc != null) {
        ASTNode[] nodes = nodeSet.matchingNodes(type.declarationSourceStart, type.sourceStart);
        if (nodes != null) {
            if (!matchedClassContainer) {
                for (int i = 0, l = nodes.length; i < l; i++)
                    nodeSet.matchingNodes.removeKey(nodes[i]);
            } else {
                for (int i = 0, l = nodes.length; i < l; i++) {
                    ASTNode node = nodes[i];
                    Integer level = (Integer) nodeSet.matchingNodes.removeKey(node);
                    if (enclosesElement) {
                        this.patternLocator.matchReportReference(node, enclosingElement, null, null,
                                type.binding, level.intValue(), this);
                    }
                }
            }
        }
    }

    // super types
    if ((type.bits & ASTNode.IsAnonymousType) != 0) {
        TypeReference superType = type.allocation.type;
        if (superType != null) {
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(superType);
            if (level != null && matchedClassContainer)
                this.patternLocator.matchReportReference(superType, enclosingElement, null, null, type.binding,
                        level.intValue(), this);
        }
    } else {
        TypeReference superClass = type.superclass;
        if (superClass != null) {
            reportMatchingSuper(superClass, enclosingElement, type.binding, nodeSet, matchedClassContainer);
        }
        TypeReference[] superInterfaces = type.superInterfaces;
        if (superInterfaces != null) {
            for (int i = 0, l = superInterfaces.length; i < l; i++) {
                reportMatchingSuper(superInterfaces[i], enclosingElement, type.binding, nodeSet,
                        matchedClassContainer);
            }
        }
    }

    // filter out element not in hierarchy scope
    boolean typeInHierarchy = type.binding == null || typeInHierarchy(type.binding);
    matchedClassContainer = matchedClassContainer && typeInHierarchy;

    // Visit fields
    FieldDeclaration[] fields = type.fields;
    if (fields != null) {
        if (nodeSet.matchingNodes.elementSize == 0)
            return; // end as all matching nodes were reported
        FieldDeclaration[] otherFields = null;
        int first = -1;
        int length = fields.length;
        for (int i = 0; i < length; i++) {
            FieldDeclaration field = fields[i];
            boolean last = field.endPart2Position == 0 || field.declarationEnd == field.endPart2Position;
            // Store first index of multiple field declaration
            if (!last) {
                if (first == -1) {
                    first = i;
                }
            }
            if (first >= 0) {
                // Store all multiple fields but first one for other elements
                if (i > first) {
                    if (otherFields == null) {
                        otherFields = new FieldDeclaration[length - i];
                    }
                    otherFields[i - 1 - first] = field;
                }
                // On last field, report match with all other elements
                if (last) {
                    for (int j = first; j <= i; j++) {
                        Integer level = (Integer) nodeSet.matchingNodes.removeKey(fields[j]);
                        int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
                        reportMatching(fields[j], otherFields, type, enclosingElement, value, typeInHierarchy,
                                nodeSet);
                    }
                    first = -1;
                    otherFields = null;
                }
            } else {
                // Single field, report normally
                Integer level = (Integer) nodeSet.matchingNodes.removeKey(field);
                int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
                reportMatching(field, null, type, enclosingElement, value, typeInHierarchy, nodeSet);
            }
        }
    }

    // Visit methods
    AbstractMethodDeclaration[] methods = type.methods;
    if (methods != null) {
        if (nodeSet.matchingNodes.elementSize == 0)
            return; // end as all matching nodes were reported
        for (int i = 0, l = methods.length; i < l; i++) {
            AbstractMethodDeclaration method = methods[i];
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(method);
            int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
            reportMatching(method, type, enclosingElement, value, typeInHierarchy, nodeSet);
        }
    }

    // Visit types
    TypeDeclaration[] memberTypes = type.memberTypes;
    if (memberTypes != null) {
        for (int i = 0, l = memberTypes.length; i < l; i++) {
            if (nodeSet.matchingNodes.elementSize == 0)
                return; // end as all matching nodes were reported
            TypeDeclaration memberType = memberTypes[i];
            Integer level = (Integer) nodeSet.matchingNodes.removeKey(memberType);
            int value = (level != null && matchedClassContainer) ? level.intValue() : -1;
            reportMatching(memberType, enclosingElement, value, nodeSet, 1);
        }
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MemberDeclarationVisitor.java

License:Open Source License

public boolean visit(TypeDeclaration typeDeclaration, BlockScope unused) {
    try {/*from  www . ja va  2 s. c  o  m*/
        char[] simpleName;
        if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) {
            simpleName = CharOperation.NO_CHAR;
        } else {
            simpleName = typeDeclaration.name;
        }
        int occurrenceCount = this.occurrencesCounts.get(simpleName);
        if (occurrenceCount == HashtableOfIntValues.NO_VALUE) {
            occurrenceCount = 1;
        } else {
            occurrenceCount = occurrenceCount + 1;
        }
        this.occurrencesCounts.put(simpleName, occurrenceCount);
        if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) {
            this.locator.reportMatching(typeDeclaration, this.enclosingElement, -1, this.nodeSet,
                    occurrenceCount);
        } else {
            Integer level = (Integer) this.nodeSet.matchingNodes.removeKey(typeDeclaration);
            this.locator.reportMatching(typeDeclaration, this.enclosingElement,
                    level != null ? level.intValue() : -1, this.nodeSet, occurrenceCount);
        }
        return false; // don't visit members as this was done during reportMatching(...)
    } catch (CoreException e) {
        throw new MatchLocator.WrappedCoreException(e);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.util.ASTNodeFinder.java

License:Open Source License

public TypeDeclaration findType(IType typeHandle) {
    IJavaElement parent = typeHandle.getParent();
    final char[] typeName = typeHandle.getElementName().toCharArray();
    final int occurenceCount = ((SourceType) typeHandle).occurrenceCount;
    final boolean findAnonymous = typeName.length == 0;
    class Visitor extends ASTVisitor {
        TypeDeclaration result;/*from  w  w w  .  j  a va  2 s.  co m*/
        int count = 0;

        public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) {
            if (this.result != null)
                return false;
            if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) {
                if (findAnonymous && ++this.count == occurenceCount) {
                    this.result = typeDeclaration;
                }
            } else {
                if (!findAnonymous && CharOperation.equals(typeName, typeDeclaration.name)) {
                    this.result = typeDeclaration;
                }
            }
            return false; // visit only one level
        }
    }
    switch (parent.getElementType()) {
    case IJavaElement.COMPILATION_UNIT:
        TypeDeclaration[] types = this.unit.types;
        if (types != null) {
            for (int i = 0, length = types.length; i < length; i++) {
                TypeDeclaration type = types[i];
                if (CharOperation.equals(typeName, type.name)) {
                    return type;
                }
            }
        }
        break;
    case IJavaElement.TYPE:
        TypeDeclaration parentDecl = findType((IType) parent);
        if (parentDecl == null)
            return null;
        types = parentDecl.memberTypes;
        if (types != null) {
            for (int i = 0, length = types.length; i < length; i++) {
                TypeDeclaration type = types[i];
                if (CharOperation.equals(typeName, type.name)) {
                    return type;
                }
            }
        }
        break;
    case IJavaElement.FIELD:
        FieldDeclaration fieldDecl = findField((IField) parent);
        if (fieldDecl == null)
            return null;
        Visitor visitor = new Visitor();
        fieldDecl.traverse(visitor, null);
        return visitor.result;
    case IJavaElement.INITIALIZER:
        Initializer initializer = findInitializer((IInitializer) parent);
        if (initializer == null)
            return null;
        visitor = new Visitor();
        initializer.traverse(visitor, null);
        return visitor.result;
    case IJavaElement.METHOD:
        AbstractMethodDeclaration methodDecl = findMethod((IMethod) parent);
        if (methodDecl == null)
            return null;
        visitor = new Visitor();
        methodDecl.traverse(visitor, (ClassScope) null);
        return visitor.result;
    }
    return null;
}

From source file:lombok.eclipse.handlers.ast.EclipseASTMaker.java

License:Open Source License

@Override
public ASTNode visitClassDecl(final lombok.ast.ClassDecl node, final Void p) {
    final TypeDeclaration typeDeclaration = new TypeDeclaration(
            ((CompilationUnitDeclaration) sourceNode.top().get()).compilationResult);
    setGeneratedByAndCopyPos(typeDeclaration, source, posHintOf(node));
    typeDeclaration.modifiers = modifiersFor(node.getModifiers());
    if (node.isInterface())
        typeDeclaration.modifiers |= AccInterface;
    typeDeclaration.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    if (node.isLocal())
        typeDeclaration.bits |= ASTNode.IsLocalType;
    if (node.isAnonymous()) {
        typeDeclaration.bits |= ASTNode.IsAnonymousType;
    }/*from w  w w . j ava2  s  .c  o m*/
    if (Is.empty(node.getName())) {
        typeDeclaration.name = CharOperation.NO_CHAR;
    } else {
        typeDeclaration.name = node.getName().toCharArray();
    }
    typeDeclaration.annotations = toArray(build(node.getAnnotations()), new Annotation[0]);
    typeDeclaration.typeParameters = toArray(build(node.getTypeParameters()), new TypeParameter[0]);
    typeDeclaration.fields = toArray(build(node.getFields()), new FieldDeclaration[0]);
    typeDeclaration.methods = toArray(build(node.getMethods()), new AbstractMethodDeclaration[0]);
    typeDeclaration.memberTypes = toArray(build(node.getMemberTypes()), new TypeDeclaration[0]);
    typeDeclaration.superInterfaces = toArray(build(node.getSuperInterfaces()), new TypeReference[0]);
    typeDeclaration.superclass = build(node.getSuperclass());
    for (final FieldDeclaration field : Each.elementIn(typeDeclaration.fields)) {
        if (isEnumConstant(field) || (field.modifiers & Modifier.STATIC) != 0) {
            typeDeclaration.addClinit();
            break;
        }
    }
    return typeDeclaration;
}

From source file:lombok.eclipse.handlers.HandleRelations.java

License:Open Source License

private static FieldDeclaration createField(Object anno, EclipseNode fieldNode, ASTNode source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;

    FieldDeclaration result = null;/*from w  ww.  j ava2 s  . c  o  m*/
    FieldDeclaration fieldDecl = (FieldDeclaration) fieldNode.get();

    String relatedFieldName = null;
    boolean isOneToOne = false;
    boolean isUnique = false;

    String baseTypeName = new String(((TypeDeclaration) fieldNode.up().get()).name);

    char[] qualifiedRelationTypeName = null;
    char[] singleRelationTypeName = null;
    TypeReference fieldType = null;
    TypeReference baseType = createTypeReference(baseTypeName.split("\\."), p);
    setGeneratedBy(baseType, source);
    TypeReference referenceType = fieldDecl.type;

    if (anno instanceof OneToOne) {
        isOneToOne = true;
        relatedFieldName = ((OneToOne) anno).field();

        qualifiedRelationTypeName = OneToOneRelation.class.getName().toCharArray();
        singleRelationTypeName = OneToOneRelation.class.getSimpleName().toCharArray();
    } else {
        relatedFieldName = ((OneToMany) anno).field();
        isUnique = ((OneToMany) anno).unique();

        if (referenceType instanceof ParameterizedSingleTypeReference) {
            referenceType = ((ParameterizedSingleTypeReference) referenceType).typeArguments[0];
        } else if (referenceType instanceof ParameterizedQualifiedTypeReference) {
            ParameterizedQualifiedTypeReference type = (ParameterizedQualifiedTypeReference) referenceType;
            referenceType = type.typeArguments[type.typeArguments.length - 1][0];
        }

        qualifiedRelationTypeName = OneToManyRelation.class.getName().toCharArray();
        singleRelationTypeName = OneToManyRelation.class.getSimpleName().toCharArray();
    }

    addImportIfNotExists((CompilationUnitDeclaration) fieldNode.top().get(), qualifiedRelationTypeName, source);

    fieldType = new ParameterizedSingleTypeReference(singleRelationTypeName,
            new TypeReference[] { baseType, referenceType }, 0, p);
    setGeneratedBy(fieldType, source);
    fieldType.sourceStart = pS;
    fieldType.sourceEnd = fieldType.statementEnd = pE;

    CompilationResult compResult = ((CompilationUnitDeclaration) fieldNode.top().get()).compilationResult;

    final TypeDeclaration typeDeclaration = new TypeDeclaration(compResult);
    setGeneratedBy(typeDeclaration, source);
    typeDeclaration.name = CharOperation.NO_CHAR;
    typeDeclaration.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType);
    typeDeclaration.bodyStart = source.sourceStart;
    typeDeclaration.bodyEnd = source.sourceEnd;
    typeDeclaration.declarationSourceStart = source.sourceStart;
    typeDeclaration.declarationSourceEnd = source.sourceEnd;
    typeDeclaration.methods = new AbstractMethodDeclaration[] {
            createGetReferencedKeyMethod(source, relatedFieldName, isOneToOne, baseType, referenceType,
                    compResult),
            createSetReferencedObjectMethod(fieldDecl, source, relatedFieldName, isOneToOne, isUnique, baseType,
                    referenceType, (CompilationUnitDeclaration) fieldNode.top().get()),
            createSetRelatedIdMethod(source, relatedFieldName, isOneToOne, baseType, referenceType,
                    compResult) };

    typeDeclaration.addClinit();

    QualifiedAllocationExpression allocation = new QualifiedAllocationExpression(typeDeclaration);
    setGeneratedBy(allocation, source);
    allocation.sourceStart = pS;
    allocation.sourceEnd = allocation.statementEnd = pE;
    allocation.type = fieldType;

    result = new FieldDeclaration(toUpperCase(new String(fieldDecl.name)).toCharArray(), 0, -1);
    setGeneratedBy(result, source);
    result.declarationSourceEnd = -1;
    result.type = fieldType;
    result.initialization = allocation;
    result.modifiers = ClassFileConstants.AccPrivate | ClassFileConstants.AccFinal
            | ClassFileConstants.AccStatic;

    return result;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJSourceElementNotifier.java

License:Open Source License

protected char[][] getInterfaceNames(TypeDeclaration typeDeclaration) {
    char[][] interfaceNames = null;
    int superInterfacesLength = 0;
    TypeReference[] superInterfaces = typeDeclaration.superInterfaces;
    if (superInterfaces != null) {
        superInterfacesLength = superInterfaces.length;
        interfaceNames = new char[superInterfacesLength][];
    } else {//from  w  w w . j  av  a2s .  com
        if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) {
            // see PR 3442
            QualifiedAllocationExpression alloc = typeDeclaration.allocation;
            if (alloc != null && alloc.type != null) {
                superInterfaces = new TypeReference[] { alloc.type };
                superInterfacesLength = 1;
                interfaceNames = new char[1][];
            }
        }
    }
    if (superInterfaces != null) {
        for (int i = 0; i < superInterfacesLength; i++) {
            interfaceNames[i] = CharOperation.concatWith(superInterfaces[i].getParameterizedTypeName(), '.');
        }
    }
    return interfaceNames;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJSourceElementNotifier.java

License:Open Source License

private int sourceEnd(TypeDeclaration typeDeclaration) {
    if ((typeDeclaration.bits & ASTNode.IsAnonymousType) != 0) {
        QualifiedAllocationExpression allocation = typeDeclaration.allocation;
        if (allocation.enumConstant != null) // case of enum constant body
            return allocation.enumConstant.sourceEnd;
        return allocation.type.sourceEnd;
    } else {/*from ww  w  .  ja  v a 2 s.c  o m*/
        return typeDeclaration.sourceEnd;
    }
}