Search in sources :

Example 1 with QualifiedAllocationExpression

use of org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression in project che by eclipse.

the class SourceTypeConverter method convert.

/*
	 * Convert an initializerinfo into a parsed initializer declaration
	 */
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;
            }
        }
        block.statements = statements;
    }
    return initializer;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer) Initializer(org.eclipse.jdt.internal.compiler.ast.Initializer) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) Block(org.eclipse.jdt.internal.compiler.ast.Block) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 2 with QualifiedAllocationExpression

use of org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression in project spoon by INRIA.

the class ParentExiter method visitCtNewClass.

@Override
public <T> void visitCtNewClass(CtNewClass<T> newClass) {
    if (child instanceof CtClass) {
        newClass.setAnonymousClass((CtClass<?>) child);
        final QualifiedAllocationExpression node = (QualifiedAllocationExpression) jdtTreeBuilder.getContextBuilder().stack.peek().node;
        final ReferenceBinding[] referenceBindings = node.resolvedType == null ? null : node.resolvedType.superInterfaces();
        if (referenceBindings != null && referenceBindings.length > 0) {
            ((CtClass<?>) child).addSuperInterface(newClass.getType().clone());
        } else if (newClass.getType() != null) {
            ((CtClass<?>) child).setSuperclass(newClass.getType().clone());
        }
        return;
    }
    super.visitCtNewClass(newClass);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)

Example 3 with QualifiedAllocationExpression

use of org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression in project che by eclipse.

the class SourceTypeConverter method convert.

/*
	 * Convert a method source element into a parsed method/constructor declaration
	 */
private AbstractMethodDeclaration convert(SourceMethod methodHandle, SourceMethodElementInfo methodInfo, CompilationResult compilationResult) throws JavaModelException {
    AbstractMethodDeclaration method;
    /* 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);
            // 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;
}
Also used : TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ConstructorDeclaration(org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) IJavaElement(org.eclipse.jdt.core.IJavaElement) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) AnnotationMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) SourceAnnotationMethodInfo(org.eclipse.jdt.internal.core.SourceAnnotationMethodInfo) QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 4 with QualifiedAllocationExpression

use of org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression in project che by eclipse.

the class SourceTypeConverter method convert.

private QualifiedAllocationExpression convert(IJavaElement localType, FieldDeclaration enumConstant, CompilationResult compilationResult) throws JavaModelException {
    TypeDeclaration anonymousLocalTypeDeclaration = convert((SourceType) localType, compilationResult);
    QualifiedAllocationExpression expression = new QualifiedAllocationExpression(anonymousLocalTypeDeclaration);
    expression.type = anonymousLocalTypeDeclaration.superclass;
    anonymousLocalTypeDeclaration.superclass = null;
    anonymousLocalTypeDeclaration.superInterfaces = null;
    anonymousLocalTypeDeclaration.allocation = expression;
    if (enumConstant != null) {
        anonymousLocalTypeDeclaration.modifiers &= ~ClassFileConstants.AccEnum;
        expression.enumConstant = enumConstant;
        expression.type = null;
    }
    return expression;
}
Also used : QualifiedAllocationExpression(org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Aggregations

QualifiedAllocationExpression (org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression)4 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)2 ISourceType (org.eclipse.jdt.internal.compiler.env.ISourceType)2 SourceType (org.eclipse.jdt.internal.core.SourceType)2 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 AnnotationMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration)1 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)1 ArrayInitializer (org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)1 Block (org.eclipse.jdt.internal.compiler.ast.Block)1 ConstructorDeclaration (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration)1 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)1 Initializer (org.eclipse.jdt.internal.compiler.ast.Initializer)1 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)1 TypeParameter (org.eclipse.jdt.internal.compiler.ast.TypeParameter)1 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)1 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)1 SourceAnnotationMethodInfo (org.eclipse.jdt.internal.core.SourceAnnotationMethodInfo)1