Search in sources :

Example 1 with TypeParameter

use of org.eclipse.jdt.internal.compiler.ast.TypeParameter in project lombok by rzwitserloot.

the class PatchDelegate method checkConflictOfTypeVarNames.

public static void checkConflictOfTypeVarNames(BindingTuple binding, EclipseNode typeNode) throws CantMakeDelegates {
    TypeVariableBinding[] typeVars = binding.parameterized.typeVariables();
    if (typeVars == null || typeVars.length == 0)
        return;
    Set<String> usedInOurType = new HashSet<String>();
    EclipseNode enclosingType = typeNode;
    while (enclosingType != null) {
        if (enclosingType.getKind() == Kind.TYPE) {
            TypeParameter[] typeParameters = ((TypeDeclaration) enclosingType.get()).typeParameters;
            if (typeParameters != null) {
                for (TypeParameter param : typeParameters) {
                    if (param.name != null)
                        usedInOurType.add(new String(param.name));
                }
            }
        }
        enclosingType = enclosingType.up();
    }
    Set<String> usedInMethodSig = new HashSet<String>();
    for (TypeVariableBinding var : typeVars) {
        char[] sourceName = var.sourceName();
        if (sourceName != null)
            usedInMethodSig.add(new String(sourceName));
    }
    usedInMethodSig.retainAll(usedInOurType);
    if (usedInMethodSig.isEmpty())
        return;
    // We might be delegating a List<T>, and we are making method <T> toArray(). A conflict is possible.
    // But only if the toArray method also uses type vars from its class, otherwise we're only shadowing,
    // which is okay as we'll add a @SuppressWarnings.
    TypeVarFinder finder = new TypeVarFinder();
    finder.visitRaw(binding.base);
    Set<String> names = new HashSet<String>(finder.getTypeVariables());
    names.removeAll(usedInMethodSig);
    if (!names.isEmpty()) {
        // We have a confirmed conflict. We could dig deeper as this may still be a false alarm, but its already an exceedingly rare case.
        CantMakeDelegates cmd = new CantMakeDelegates();
        cmd.conflicted = usedInMethodSig;
        throw cmd;
    }
}
Also used : TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) EclipseNode(lombok.eclipse.EclipseNode) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) HashSet(java.util.HashSet)

Example 2 with TypeParameter

use of org.eclipse.jdt.internal.compiler.ast.TypeParameter in project lombok by rzwitserloot.

the class EclipseHandlerUtil method copyTypeParams.

/**
 * You can't share TypeParameter objects or bad things happen; for example, one 'T' resolves differently
 * from another 'T', even for the same T in a single class file. Unfortunately the TypeParameter type hierarchy
 * is complicated and there's no clone method on TypeParameter itself. This method can clone them.
 */
public static TypeParameter[] copyTypeParams(TypeParameter[] params, ASTNode source) {
    if (params == null)
        return null;
    TypeParameter[] out = new TypeParameter[params.length];
    int idx = 0;
    for (TypeParameter param : params) {
        TypeParameter o = new TypeParameter();
        setGeneratedBy(o, source);
        o.annotations = param.annotations;
        o.bits = param.bits;
        o.modifiers = param.modifiers;
        o.name = param.name;
        o.type = copyType(param.type, source);
        o.sourceStart = param.sourceStart;
        o.sourceEnd = param.sourceEnd;
        o.declarationEnd = param.declarationEnd;
        o.declarationSourceStart = param.declarationSourceStart;
        o.declarationSourceEnd = param.declarationSourceEnd;
        if (param.bounds != null) {
            TypeReference[] b = new TypeReference[param.bounds.length];
            int idx2 = 0;
            for (TypeReference ref : param.bounds) b[idx2++] = copyType(ref, source);
            o.bounds = b;
        }
        out[idx++] = o;
    }
    return out;
}
Also used : TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)

Example 3 with TypeParameter

use of org.eclipse.jdt.internal.compiler.ast.TypeParameter 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 TypeParameter

use of org.eclipse.jdt.internal.compiler.ast.TypeParameter in project lombok by rzwitserloot.

the class EclipseHandlerUtil method cloneSelfType.

public static TypeReference cloneSelfType(EclipseNode context, ASTNode source) {
    int pS = source == null ? 0 : source.sourceStart, pE = source == null ? 0 : source.sourceEnd;
    long p = (long) pS << 32 | pE;
    EclipseNode type = context;
    TypeReference result = null;
    while (type != null && type.getKind() != Kind.TYPE) type = type.up();
    if (type != null && type.get() instanceof TypeDeclaration) {
        TypeDeclaration typeDecl = (TypeDeclaration) type.get();
        if (typeDecl.typeParameters != null && typeDecl.typeParameters.length > 0) {
            TypeReference[] refs = new TypeReference[typeDecl.typeParameters.length];
            int idx = 0;
            for (TypeParameter param : typeDecl.typeParameters) {
                TypeReference typeRef = new SingleTypeReference(param.name, (long) param.sourceStart << 32 | param.sourceEnd);
                if (source != null)
                    setGeneratedBy(typeRef, source);
                refs[idx++] = typeRef;
            }
            result = new ParameterizedSingleTypeReference(typeDecl.name, refs, 0, p);
        } else {
            result = new SingleTypeReference(((TypeDeclaration) type.get()).name, p);
        }
    }
    if (result != null && source != null)
        setGeneratedBy(result, source);
    return result;
}
Also used : TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) EclipseNode(lombok.eclipse.EclipseNode) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 5 with TypeParameter

use of org.eclipse.jdt.internal.compiler.ast.TypeParameter in project lombok by rzwitserloot.

the class EclipseHandlerUtil method namePlusTypeParamsToTypeReference.

public static TypeReference namePlusTypeParamsToTypeReference(char[] typeName, TypeParameter[] params, long p) {
    if (params != null && params.length > 0) {
        TypeReference[] refs = new TypeReference[params.length];
        int idx = 0;
        for (TypeParameter param : params) {
            TypeReference typeRef = new SingleTypeReference(param.name, p);
            refs[idx++] = typeRef;
        }
        return new ParameterizedSingleTypeReference(typeName, refs, 0, p);
    }
    return new SingleTypeReference(typeName, p);
}
Also used : TypeParameter(org.eclipse.jdt.internal.compiler.ast.TypeParameter) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)

Aggregations

TypeParameter (org.eclipse.jdt.internal.compiler.ast.TypeParameter)8 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)5 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)5 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)5 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)4 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)4 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)4 ParameterizedSingleTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference)4 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)4 EclipseNode (lombok.eclipse.EclipseNode)3 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)3 ArrayQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference)3 ArrayTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference)3 AnnotationMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration)2 ConstructorDeclaration (org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration)2 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)2 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Builder (lombok.Builder)1