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

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

Introduction

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

Prototype

int IsSuperType

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

Click Source Link

Usage

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  w  w w .  j a  va 2 s.c o 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.BinaryTypeConverter.java

License:Open Source License

private TypeDeclaration convert(IType type, IType alreadyComputedMember,
        TypeDeclaration alreadyComputedMemberDeclaration) throws JavaModelException {
    /* create type declaration - can be member type */
    TypeDeclaration typeDeclaration = new TypeDeclaration(this.compilationResult);

    if (type.getDeclaringType() != null) {
        typeDeclaration.bits |= ASTNode.IsMemberType;
    }/*  w  w  w. j a va 2s  .c  o  m*/
    typeDeclaration.name = type.getElementName().toCharArray();
    typeDeclaration.modifiers = type.getFlags();

    /* set superclass and superinterfaces */
    if (type.getSuperclassName() != null) {
        TypeReference typeReference = createTypeReference(type.getSuperclassTypeSignature());
        if (typeReference != null) {
            typeDeclaration.superclass = typeReference;
            typeDeclaration.superclass.bits |= ASTNode.IsSuperType;
        }
    }

    String[] interfaceTypes = type.getSuperInterfaceTypeSignatures();
    int interfaceCount = interfaceTypes == null ? 0 : interfaceTypes.length;
    typeDeclaration.superInterfaces = new TypeReference[interfaceCount];
    int count = 0;
    for (int i = 0; i < interfaceCount; i++) {
        TypeReference typeReference = createTypeReference(interfaceTypes[i]);
        if (typeReference != null) {
            typeDeclaration.superInterfaces[count] = typeReference;
            typeDeclaration.superInterfaces[count++].bits |= ASTNode.IsSuperType;
        }
    }
    if (count != interfaceCount) {
        System.arraycopy(typeDeclaration.fields, 0,
                typeDeclaration.superInterfaces = new TypeReference[interfaceCount], 0, interfaceCount);
    }

    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {

        /* convert type parameters */
        ITypeParameter[] typeParameters = type.getTypeParameters();
        if (typeParameters != null && typeParameters.length > 0) {
            int parameterCount = typeParameters.length;
            org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParams = new org.eclipse.jdt.internal.compiler.ast.TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                ITypeParameter typeParameter = typeParameters[i];
                typeParams[i] = createTypeParameter(typeParameter.getElementName().toCharArray(),
                        stringArrayToCharArray(typeParameter.getBounds()), 0, 0);
            }

            typeDeclaration.typeParameters = typeParams;
        }
    }

    /* convert member types */
    IType[] memberTypes = type.getTypes();
    int memberTypeCount = memberTypes == null ? 0 : memberTypes.length;
    typeDeclaration.memberTypes = new TypeDeclaration[memberTypeCount];
    for (int i = 0; i < memberTypeCount; i++) {
        if (alreadyComputedMember != null && alreadyComputedMember.getFullyQualifiedName()
                .equals(memberTypes[i].getFullyQualifiedName())) {
            typeDeclaration.memberTypes[i] = alreadyComputedMemberDeclaration;
        } else {
            typeDeclaration.memberTypes[i] = convert(memberTypes[i], null, null);
        }
        typeDeclaration.memberTypes[i].enclosingType = typeDeclaration;
    }

    /* convert fields */
    IField[] fields = type.getFields();
    int fieldCount = fields == null ? 0 : fields.length;
    typeDeclaration.fields = new FieldDeclaration[fieldCount];
    count = 0;
    for (int i = 0; i < fieldCount; i++) {
        FieldDeclaration fieldDeclaration = convert(fields[i], type);
        if (fieldDeclaration != null) {
            typeDeclaration.fields[count++] = fieldDeclaration;
        }
    }
    if (count != fieldCount) {
        System.arraycopy(typeDeclaration.fields, 0, typeDeclaration.fields = new FieldDeclaration[count], 0,
                count);
    }

    /* convert methods - need to add default constructor if necessary */
    IMethod[] methods = type.getMethods();
    int methodCount = methods == null ? 0 : methods.length;

    /* source type has a constructor ?           */
    /* by default, we assume that one is needed. */
    int neededCount = 1;
    for (int i = 0; i < methodCount; i++) {
        if (methods[i].isConstructor()) {
            neededCount = 0;
            // Does not need the extra constructor since one constructor already exists.
            break;
        }
    }
    boolean isInterface = type.isInterface();
    neededCount = isInterface ? 0 : neededCount;
    typeDeclaration.methods = new AbstractMethodDeclaration[methodCount + neededCount];
    if (neededCount != 0) { // add default constructor in first position
        typeDeclaration.methods[0] = typeDeclaration.createDefaultConstructor(false, false);
    }
    boolean hasAbstractMethods = false;
    count = 0;
    for (int i = 0; i < methodCount; i++) {
        AbstractMethodDeclaration method = convert(methods[i], type);
        if (method != null) {
            boolean isAbstract;
            if ((isAbstract = method.isAbstract()) || isInterface) { // fix-up flag
                method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
            }
            if (isAbstract) {
                hasAbstractMethods = true;
            }
            typeDeclaration.methods[neededCount + (count++)] = method;
        }
    }
    if (count != methodCount) {
        System.arraycopy(typeDeclaration.methods, 0,
                typeDeclaration.methods = new AbstractMethodDeclaration[count + neededCount], 0,
                count + neededCount);
    }
    if (hasAbstractMethods) {
        typeDeclaration.bits |= ASTNode.HasAbstractMethods;
    }
    return typeDeclaration;
}

From source file:lombok.eclipse.agent.PatchDelegate.java

License:Open Source License

private static MethodDeclaration createDelegateMethod(char[] name, EclipseNode typeNode, BindingTuple pair,
        CompilationResult compilationResult, EclipseNode annNode, DelegateReceiver delegateReceiver) {
    /* public <T, U, ...> ReturnType methodName(ParamType1 name1, ParamType2 name2, ...) throws T1, T2, ... {
     *      (return) delegate.<T, U>methodName(name1, name2);
     *  }/*  w w  w.  j av a 2  s  .  co m*/
     */

    boolean isVarargs = (pair.base.modifiers & ClassFileConstants.AccVarargs) != 0;

    try {
        checkConflictOfTypeVarNames(pair, typeNode);
    } catch (CantMakeDelegates e) {
        annNode.addError(
                "There's a conflict in the names of type parameters. Fix it by renaming the following type parameters of your class: "
                        + e.conflicted);
        return null;
    }

    ASTNode source = annNode.get();

    int pS = source.sourceStart, pE = source.sourceEnd;

    MethodBinding binding = pair.parameterized;
    MethodDeclaration method = new MethodDeclaration(compilationResult);
    setGeneratedBy(method, source);
    method.sourceStart = pS;
    method.sourceEnd = pE;
    method.modifiers = ClassFileConstants.AccPublic;

    method.returnType = makeType(binding.returnType, source, false);
    boolean isDeprecated = binding.isDeprecated();

    method.selector = binding.selector;

    if (binding.thrownExceptions != null && binding.thrownExceptions.length > 0) {
        method.thrownExceptions = new TypeReference[binding.thrownExceptions.length];
        for (int i = 0; i < method.thrownExceptions.length; i++) {
            method.thrownExceptions[i] = makeType(binding.thrownExceptions[i], source, false);
        }
    }

    MessageSend call = new MessageSend();
    call.sourceStart = pS;
    call.sourceEnd = pE;
    call.nameSourcePosition = pos(source);
    setGeneratedBy(call, source);
    call.receiver = delegateReceiver.get(source, name);
    call.selector = binding.selector;

    if (binding.typeVariables != null && binding.typeVariables.length > 0) {
        method.typeParameters = new TypeParameter[binding.typeVariables.length];
        call.typeArguments = new TypeReference[binding.typeVariables.length];
        for (int i = 0; i < method.typeParameters.length; i++) {
            method.typeParameters[i] = new TypeParameter();
            method.typeParameters[i].sourceStart = pS;
            method.typeParameters[i].sourceEnd = pE;
            setGeneratedBy(method.typeParameters[i], source);
            method.typeParameters[i].name = binding.typeVariables[i].sourceName;
            call.typeArguments[i] = new SingleTypeReference(binding.typeVariables[i].sourceName, pos(source));
            setGeneratedBy(call.typeArguments[i], source);
            ReferenceBinding super1 = binding.typeVariables[i].superclass;
            ReferenceBinding[] super2 = binding.typeVariables[i].superInterfaces;
            if (super2 == null)
                super2 = new ReferenceBinding[0];
            if (super1 != null || super2.length > 0) {
                int offset = super1 == null ? 0 : 1;
                method.typeParameters[i].bounds = new TypeReference[super2.length + offset - 1];
                if (super1 != null)
                    method.typeParameters[i].type = makeType(super1, source, false);
                else
                    method.typeParameters[i].type = makeType(super2[0], source, false);
                int ctr = 0;
                for (int j = (super1 == null) ? 1 : 0; j < super2.length; j++) {
                    method.typeParameters[i].bounds[ctr] = makeType(super2[j], source, false);
                    method.typeParameters[i].bounds[ctr++].bits |= ASTNode.IsSuperType;
                }
            }
        }
    }

    if (isDeprecated) {
        method.annotations = new Annotation[] { generateDeprecatedAnnotation(source) };
    }

    method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;

    if (binding.parameters != null && binding.parameters.length > 0) {
        method.arguments = new Argument[binding.parameters.length];
        call.arguments = new Expression[method.arguments.length];
        for (int i = 0; i < method.arguments.length; i++) {
            AbstractMethodDeclaration sourceElem;
            try {
                sourceElem = pair.base.sourceMethod();
            } catch (Exception e) {
                sourceElem = null;
            }
            char[] argName;
            if (sourceElem == null)
                argName = ("arg" + i).toCharArray();
            else {
                argName = sourceElem.arguments[i].name;
            }
            method.arguments[i] = new Argument(argName, pos(source),
                    makeType(binding.parameters[i], source, false), ClassFileConstants.AccFinal);
            setGeneratedBy(method.arguments[i], source);
            call.arguments[i] = new SingleNameReference(argName, pos(source));
            setGeneratedBy(call.arguments[i], source);
        }
        if (isVarargs) {
            method.arguments[method.arguments.length - 1].type.bits |= ASTNode.IsVarArgs;
        }
    }

    Statement body;
    if (method.returnType instanceof SingleTypeReference
            && ((SingleTypeReference) method.returnType).token == TypeConstants.VOID) {
        body = call;
    } else {
        body = new ReturnStatement(call, source.sourceStart, source.sourceEnd);
        setGeneratedBy(body, source);
    }

    method.statements = new Statement[] { body };
    return method;
}

From source file:org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.java

License:Open Source License

/**
 * Build JDT TypeDeclarations for the groovy type declarations that were parsed from the source file.
 *//*w  w  w.j  a v a2s .  com*/
private void createTypeDeclarations(ModuleNode moduleNode) {
    List<ClassNode> moduleClassNodes = moduleNode.getClasses();
    List<TypeDeclaration> typeDeclarations = new ArrayList<TypeDeclaration>();
    Map<ClassNode, TypeDeclaration> fromClassNodeToDecl = new HashMap<ClassNode, TypeDeclaration>();

    char[] mainName = toMainName(compilationResult.getFileName());
    boolean isInner = false;
    List<ClassNode> classNodes = null;
    classNodes = moduleClassNodes;
    Map<ClassNode, List<TypeDeclaration>> innersToRecord = new HashMap<ClassNode, List<TypeDeclaration>>();
    for (ClassNode classNode : classNodes) {
        if (!classNode.isPrimaryClassNode()) {
            continue;
        }

        GroovyTypeDeclaration typeDeclaration = new GroovyTypeDeclaration(compilationResult, classNode);

        typeDeclaration.annotations = transformAnnotations(classNode.getAnnotations());
        // FIXASC duplicates code further down, tidy this up
        if (classNode instanceof InnerClassNode) {
            InnerClassNode innerClassNode = (InnerClassNode) classNode;
            ClassNode outerClass = innerClassNode.getOuterClass();
            String outername = outerClass.getNameWithoutPackage();
            String newInner = innerClassNode.getNameWithoutPackage().substring(outername.length() + 1);
            typeDeclaration.name = newInner.toCharArray();
            isInner = true;
        } else {
            typeDeclaration.name = classNode.getNameWithoutPackage().toCharArray();
            isInner = false;
        }

        // classNode.getInnerClasses();
        // classNode.
        boolean isInterface = classNode.isInterface();
        int mods = classNode.getModifiers();
        if ((mods & Opcodes.ACC_ENUM) != 0) {
            // remove final
            mods = mods & ~Opcodes.ACC_FINAL;
        }
        // FIXASC should this modifier be set?
        // mods |= Opcodes.ACC_PUBLIC;
        // FIXASC should not do this for inner classes, just for top level types
        // FIXASC does this make things visible that shouldn't be?
        mods = mods & ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED);
        if (!isInner) {
            if ((mods & Opcodes.ACC_STATIC) != 0) {
                mods = mods & ~(Opcodes.ACC_STATIC);
            }
        }
        typeDeclaration.modifiers = mods & ~(isInterface ? Opcodes.ACC_ABSTRACT : 0);

        if (!(classNode instanceof InnerClassNode)) {
            if (!CharOperation.equals(typeDeclaration.name, mainName)) {
                typeDeclaration.bits |= ASTNode.IsSecondaryType;
            }
        }

        fixupSourceLocationsForTypeDeclaration(typeDeclaration, classNode);

        if (classNode.getGenericsTypes() != null) {
            GenericsType[] genericInfo = classNode.getGenericsTypes();
            // Example case here: Foo<T extends Number & I>
            // the type parameter is T, the 'type' is Number and the bounds for the type parameter are just the extra bound
            // I.
            typeDeclaration.typeParameters = new TypeParameter[genericInfo.length];
            for (int tp = 0; tp < genericInfo.length; tp++) {
                TypeParameter typeParameter = new TypeParameter();
                typeParameter.name = genericInfo[tp].getName().toCharArray();
                ClassNode[] upperBounds = genericInfo[tp].getUpperBounds();
                if (upperBounds != null) {
                    // FIXASC (M3) Positional info for these references?
                    typeParameter.type = createTypeReferenceForClassNode(upperBounds[0]);
                    typeParameter.bounds = (upperBounds.length > 1 ? new TypeReference[upperBounds.length - 1]
                            : null);
                    for (int b = 1, max = upperBounds.length; b < max; b++) {
                        typeParameter.bounds[b - 1] = createTypeReferenceForClassNode(upperBounds[b]);
                        typeParameter.bounds[b - 1].bits |= ASTNode.IsSuperType;
                    }
                }
                typeDeclaration.typeParameters[tp] = typeParameter;
            }
        }

        boolean isEnum = (classNode.getModifiers() & Opcodes.ACC_ENUM) != 0;
        configureSuperClass(typeDeclaration, classNode.getSuperClass(), isEnum);
        configureSuperInterfaces(typeDeclaration, classNode);
        typeDeclaration.methods = createMethodAndConstructorDeclarations(classNode, isEnum, compilationResult);
        typeDeclaration.fields = createFieldDeclarations(classNode);
        typeDeclaration.properties = classNode.getProperties();
        if (classNode instanceof InnerClassNode) {
            InnerClassNode innerClassNode = (InnerClassNode) classNode;
            ClassNode outerClass = innerClassNode.getOuterClass();
            String outername = outerClass.getNameWithoutPackage();
            String newInner = innerClassNode.getNameWithoutPackage().substring(outername.length() + 1);
            typeDeclaration.name = newInner.toCharArray();

            // Record that we need to set the parent of this inner type later
            List<TypeDeclaration> inners = innersToRecord.get(outerClass);
            if (inners == null) {
                inners = new ArrayList<TypeDeclaration>();
                innersToRecord.put(outerClass, inners);
            }
            inners.add(typeDeclaration);
        } else {
            typeDeclarations.add(typeDeclaration);
        }
        fromClassNodeToDecl.put(classNode, typeDeclaration);
    }

    // For inner types, now attach them to their parents. This was not done earlier as sometimes the types are processed in
    // such an order that inners are dealt with before outers
    for (Map.Entry<ClassNode, List<TypeDeclaration>> innersToRecordEntry : innersToRecord.entrySet()) {
        TypeDeclaration outerTypeDeclaration = fromClassNodeToDecl.get(innersToRecordEntry.getKey());
        // Check if there is a problem locating the parent for the inner
        if (outerTypeDeclaration == null) {
            throw new GroovyEclipseBug(
                    "Failed to find the type declaration for " + innersToRecordEntry.getKey().getName());
        }
        List<TypeDeclaration> newInnersList = innersToRecordEntry.getValue();
        outerTypeDeclaration.memberTypes = newInnersList.toArray(new TypeDeclaration[newInnersList.size()]);
    }

    types = typeDeclarations.toArray(new TypeDeclaration[typeDeclarations.size()]);
}

From source file:org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.java

License:Open Source License

/**
 * Create a JDT MethodDeclaration that represents a groovy MethodNode
 *//*w  w  w.  j  ava 2 s  .c  o m*/
private MethodDeclaration createMethodDeclaration(ClassNode classNode, MethodNode methodNode, boolean isEnum,
        CompilationResult compilationResult) {
    if (classNode.isAnnotationDefinition()) {
        AnnotationMethodDeclaration methodDeclaration = new AnnotationMethodDeclaration(compilationResult);
        int modifiers = methodNode.getModifiers();
        modifiers &= ~(ClassFileConstants.AccSynthetic | ClassFileConstants.AccTransient);
        methodDeclaration.annotations = transformAnnotations(methodNode.getAnnotations());
        methodDeclaration.modifiers = modifiers;
        if (methodNode.hasAnnotationDefault()) {
            methodDeclaration.modifiers |= ClassFileConstants.AccAnnotationDefault;
        }
        methodDeclaration.selector = methodNode.getName().toCharArray();
        fixupSourceLocationsForMethodDeclaration(methodDeclaration, methodNode);
        ClassNode returnType = methodNode.getReturnType();
        methodDeclaration.returnType = createTypeReferenceForClassNode(returnType);
        return methodDeclaration;
    } else {
        MethodDeclaration methodDeclaration = new MethodDeclaration(compilationResult);

        // TODO refactor - extract method
        GenericsType[] generics = methodNode.getGenericsTypes();
        // generic method
        if (generics != null && generics.length != 0) {
            methodDeclaration.typeParameters = new TypeParameter[generics.length];
            for (int tp = 0; tp < generics.length; tp++) {
                TypeParameter typeParameter = new TypeParameter();
                typeParameter.name = generics[tp].getName().toCharArray();
                ClassNode[] upperBounds = generics[tp].getUpperBounds();
                if (upperBounds != null) {
                    // FIXASC Positional info for these references?
                    typeParameter.type = createTypeReferenceForClassNode(upperBounds[0]);
                    typeParameter.bounds = (upperBounds.length > 1 ? new TypeReference[upperBounds.length - 1]
                            : null);
                    for (int b = 1, max = upperBounds.length; b < max; b++) {
                        typeParameter.bounds[b - 1] = createTypeReferenceForClassNode(upperBounds[b]);
                        typeParameter.bounds[b - 1].bits |= ASTNode.IsSuperType;
                    }
                }
                methodDeclaration.typeParameters[tp] = typeParameter;
            }
        }

        boolean isMain = false;
        // Note: modifiers for the MethodBinding constructed for this declaration will be created marked with
        // AccVarArgs if the bitset for the type reference in the final argument is marked IsVarArgs
        int modifiers = methodNode.getModifiers();
        modifiers &= ~(ClassFileConstants.AccSynthetic | ClassFileConstants.AccTransient);
        methodDeclaration.annotations = transformAnnotations(methodNode.getAnnotations());
        methodDeclaration.modifiers = modifiers;
        methodDeclaration.selector = methodNode.getName().toCharArray();
        // Need to capture the rule in Verifier.adjustTypesIfStaticMainMethod(MethodNode node)
        // if (node.getName().equals("main") && node.isStatic()) {
        // Parameter[] params = node.getParameters();
        // if (params.length == 1) {
        // Parameter param = params[0];
        // if (param.getType() == null || param.getType()==ClassHelper.OBJECT_TYPE) {
        // param.setType(ClassHelper.STRING_TYPE.makeArray());
        // ClassNode returnType = node.getReturnType();
        // if(returnType == ClassHelper.OBJECT_TYPE) {
        // node.setReturnType(ClassHelper.VOID_TYPE);
        // }
        // }
        // }
        Parameter[] params = methodNode.getParameters();
        ClassNode returnType = methodNode.getReturnType();

        // source of 'static main(args)' would become 'static Object main(Object args)' - so transform here
        if ((modifiers & ClassFileConstants.AccStatic) != 0 && params != null && params.length == 1
                && methodNode.getName().equals("main")) {
            Parameter p = params[0];
            if (p.getType() == null || p.getType().getName().equals(ClassHelper.OBJECT)) {
                String name = p.getName();
                params = new Parameter[1];
                params[0] = new Parameter(ClassHelper.STRING_TYPE.makeArray(), name);
                if (returnType.getName().equals(ClassHelper.OBJECT)) {
                    returnType = ClassHelper.VOID_TYPE;
                }
            }
        }

        methodDeclaration.arguments = createArguments(params, isMain);
        methodDeclaration.returnType = createTypeReferenceForClassNode(returnType);
        methodDeclaration.thrownExceptions = createTypeReferencesForClassNodes(methodNode.getExceptions());
        fixupSourceLocationsForMethodDeclaration(methodDeclaration, methodNode);
        return methodDeclaration;
    }
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumeClassHeaderExtends() {
    // ClassHeaderExtends ::= 'extends' ClassType
    //superclass//from   w  w  w . ja  v  a 2 s.co m
    TypeReference superClass = getTypeReference(0);
    // There is a class declaration on the top of stack
    TypeDeclaration typeDecl = (TypeDeclaration) this.astStack[this.astPtr];
    typeDecl.superclass = superClass;
    superClass.bits |= ASTNode.IsSuperType;
    typeDecl.bodyStart = typeDecl.superclass.sourceEnd + 1;
    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = typeDecl.bodyStart;
    }
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumeClassHeaderImplements() {
    // ClassHeaderImplements ::= 'implements' InterfaceTypeList
    int length = this.astLengthStack[this.astLengthPtr--];
    //super interfaces
    this.astPtr -= length;
    // There is a class declaration on the top of stack
    TypeDeclaration typeDecl = (TypeDeclaration) this.astStack[this.astPtr];
    System.arraycopy(this.astStack, this.astPtr + 1, typeDecl.superInterfaces = new TypeReference[length], 0,
            length);/*w ww. ja v  a2  s  .  c o  m*/
    for (int i = 0, max = typeDecl.superInterfaces.length; i < max; i++) {
        typeDecl.superInterfaces[i].bits |= ASTNode.IsSuperType;
    }
    typeDecl.bodyStart = typeDecl.superInterfaces[length - 1].sourceEnd + 1;
    this.listLength = 0; // reset after having read super-interfaces
    // recovery
    if (this.currentElement != null) { // is recovering
        this.lastCheckPoint = typeDecl.bodyStart;
    }
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumeInterfaceHeaderExtends() {
    // InterfaceHeaderExtends ::= 'extends' InterfaceTypeList
    int length = this.astLengthStack[this.astLengthPtr--];
    //super interfaces
    this.astPtr -= length;
    TypeDeclaration typeDecl = (TypeDeclaration) this.astStack[this.astPtr];
    System.arraycopy(this.astStack, this.astPtr + 1, typeDecl.superInterfaces = new TypeReference[length], 0,
            length);//from  ww  w  .j a  v  a  2  s .c o  m
    for (int i = 0, max = typeDecl.superInterfaces.length; i < max; i++) {
        typeDecl.superInterfaces[i].bits |= ASTNode.IsSuperType;
    }
    typeDecl.bodyStart = typeDecl.superInterfaces[length - 1].sourceEnd + 1;
    this.listLength = 0; // reset after having read super-interfaces
    // recovery
    if (this.currentElement != null) {
        this.lastCheckPoint = typeDecl.bodyStart;
    }
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumeTypeParameter1WithExtends() {
    //TypeParameter1 ::= TypeParameterHeader 'extends' ReferenceType1
    TypeReference superType = (TypeReference) this.genericsStack[this.genericsPtr--];
    this.genericsLengthPtr--;
    TypeParameter typeParameter = (TypeParameter) this.genericsStack[this.genericsPtr];
    typeParameter.declarationSourceEnd = superType.sourceEnd;
    typeParameter.type = superType;//  w  w  w. j  a v a 2s. c om
    superType.bits |= ASTNode.IsSuperType;
    this.genericsStack[this.genericsPtr] = typeParameter;
}

From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java

License:Open Source License

protected void consumeTypeParameter1WithExtendsAndBounds() {
    //TypeParameter1 ::= TypeParameterHeader 'extends' ReferenceType AdditionalBoundList1
    int additionalBoundsLength = this.genericsLengthStack[this.genericsLengthPtr--];
    TypeReference[] bounds = new TypeReference[additionalBoundsLength];
    this.genericsPtr -= additionalBoundsLength;
    System.arraycopy(this.genericsStack, this.genericsPtr + 1, bounds, 0, additionalBoundsLength);
    TypeReference superType = getTypeReference(this.intStack[this.intPtr--]);
    TypeParameter typeParameter = (TypeParameter) this.genericsStack[this.genericsPtr];
    typeParameter.declarationSourceEnd = bounds[additionalBoundsLength - 1].sourceEnd;
    typeParameter.type = superType;//from   www  . jav  a 2  s .  c  om
    superType.bits |= ASTNode.IsSuperType;
    typeParameter.bounds = bounds;
    for (int i = 0, max = bounds.length; i < max; i++) {
        bounds[i].bits |= ASTNode.IsSuperType;
    }
}