Example usage for org.eclipse.jdt.internal.compiler.classfmt ClassFileConstants AccAnnotationDefault

List of usage examples for org.eclipse.jdt.internal.compiler.classfmt ClassFileConstants AccAnnotationDefault

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.classfmt ClassFileConstants AccAnnotationDefault.

Prototype

int AccAnnotationDefault

To view the source code for org.eclipse.jdt.internal.compiler.classfmt ClassFileConstants AccAnnotationDefault.

Click Source Link

Document

Extra flags for types and members attributes (not from the JVMS, should have been defined in ExtraCompilerModifiers).

Usage

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;/* ww  w.j a va2  s  . c om*/

    /* 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:org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.java

License:Open Source License

/**
 * Create a JDT MethodDeclaration that represents a groovy MethodNode
 *//*from w  ww . ja va  2 s  . com*/
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 consumeMethodHeaderDefaultValue() {
    // MethodHeaderDefaultValue ::= DefaultValue
    MethodDeclaration md = (MethodDeclaration) this.astStack[this.astPtr];

    int length = this.expressionLengthStack[this.expressionLengthPtr--];
    if (length == 1) {
        this.intPtr--; // we get rid of the position of the default keyword
        this.intPtr--; // we get rid of the position of the default keyword
        if (md.isAnnotationMethod()) {
            ((AnnotationMethodDeclaration) md).defaultValue = this.expressionStack[this.expressionPtr];
            md.modifiers |= ClassFileConstants.AccAnnotationDefault;
        }/*from  w  w w.j  a va2s  .  c om*/
        this.expressionPtr--;
        this.recordStringLiterals = true;
    }

    if (this.currentElement != null) {
        if (md.isAnnotationMethod()) {
            this.currentElement
                    .updateSourceEndIfNecessary(((AnnotationMethodDeclaration) md).defaultValue.sourceEnd);
        }
    }
}

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

License:Open Source License

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

    /* 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) {
        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 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;
}