Search in sources :

Example 6 with TypeBinding

use of org.eclipse.jdt.internal.compiler.lookup.TypeBinding in project lombok by rzwitserloot.

the class PatchVal method is.

private static boolean is(TypeReference ref, BlockScope scope, String key) {
    if (!couldBe(key, ref))
        return false;
    TypeBinding resolvedType = ref.resolvedType;
    if (resolvedType == null)
        resolvedType = ref.resolveType(scope, false);
    if (resolvedType == null)
        return false;
    char[] pkg = resolvedType.qualifiedPackageName();
    char[] nm = resolvedType.qualifiedSourceName();
    int pkgFullLength = pkg.length > 0 ? pkg.length + 1 : 0;
    char[] fullName = new char[pkgFullLength + nm.length];
    if (pkg.length > 0) {
        System.arraycopy(pkg, 0, fullName, 0, pkg.length);
        fullName[pkg.length] = '.';
    }
    System.arraycopy(nm, 0, fullName, pkgFullLength, nm.length);
    return matches(key, fullName);
}
Also used : ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding)

Example 7 with TypeBinding

use of org.eclipse.jdt.internal.compiler.lookup.TypeBinding in project lombok by rzwitserloot.

the class PatchVal method handleValForForEach.

public static boolean handleValForForEach(ForeachStatement forEach, BlockScope scope) {
    if (forEach.elementVariable == null)
        return false;
    boolean val = isVal(forEach.elementVariable, scope);
    boolean var = isVar(forEach.elementVariable, scope);
    if (!(val || var))
        return false;
    TypeBinding component = getForEachComponentType(forEach.collection, scope);
    if (component == null)
        return false;
    TypeReference replacement = makeType(component, forEach.elementVariable.type, false);
    if (val)
        forEach.elementVariable.modifiers |= ClassFileConstants.AccFinal;
    forEach.elementVariable.annotations = addValAnnotation(forEach.elementVariable.annotations, forEach.elementVariable.type, scope);
    forEach.elementVariable.type = replacement != null ? replacement : new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(forEach.elementVariable.type, 3));
    return false;
}
Also used : ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)

Example 8 with TypeBinding

use of org.eclipse.jdt.internal.compiler.lookup.TypeBinding in project lombok by rzwitserloot.

the class PatchVal method handleValForLocalDeclaration.

public static boolean handleValForLocalDeclaration(LocalDeclaration local, BlockScope scope) {
    if (local == null || !LocalDeclaration.class.equals(local.getClass()))
        return false;
    boolean decomponent = false;
    boolean val = isVal(local, scope);
    boolean var = isVar(local, scope);
    if (!(val || var))
        return false;
    StackTraceElement[] st = new Throwable().getStackTrace();
    for (int i = 0; i < st.length - 2 && i < 10; i++) {
        if (st[i].getClassName().equals("lombok.launch.PatchFixesHider$Val")) {
            boolean valInForStatement = val && st[i + 1].getClassName().equals("org.eclipse.jdt.internal.compiler.ast.LocalDeclaration") && st[i + 2].getClassName().equals("org.eclipse.jdt.internal.compiler.ast.ForStatement");
            if (valInForStatement)
                return false;
            break;
        }
    }
    Expression init = local.initialization;
    if (init == null && Reflection.initCopyField != null) {
        try {
            init = (Expression) Reflection.initCopyField.get(local);
        } catch (Exception e) {
        // init remains null.
        }
    }
    if (init == null && Reflection.iterableCopyField != null) {
        try {
            init = (Expression) Reflection.iterableCopyField.get(local);
            decomponent = true;
        } catch (Exception e) {
        // init remains null.
        }
    }
    TypeReference replacement = null;
    if (init != null) {
        if (init.getClass().getName().equals("org.eclipse.jdt.internal.compiler.ast.LambdaExpression")) {
            return false;
        }
        TypeBinding resolved = null;
        try {
            resolved = decomponent ? getForEachComponentType(init, scope) : resolveForExpression(init, scope);
        } catch (NullPointerException e) {
            // This definitely occurs if as part of resolving the initializer expression, a
            // lambda expression in it must also be resolved (such as when lambdas are part of
            // a ternary expression). This can't result in a viable 'val' matching, so, we
            // just go with 'Object' and let the IDE print the appropriate errors.
            resolved = null;
        }
        if (resolved != null) {
            try {
                replacement = makeType(resolved, local.type, false);
            } catch (Exception e) {
            // Some type thing failed. It might be an IntersectionType
            }
        }
    }
    if (val)
        local.modifiers |= ClassFileConstants.AccFinal;
    local.annotations = addValAnnotation(local.annotations, local.type, scope);
    local.type = replacement != null ? replacement : new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(local.type, 3));
    return false;
}
Also used : Expression(org.eclipse.jdt.internal.compiler.ast.Expression) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)

Example 9 with TypeBinding

use of org.eclipse.jdt.internal.compiler.lookup.TypeBinding in project che by eclipse.

the class Util method getUnresolvedJavaElement.

/**
     * Return the java element corresponding to the given compiler binding.
     */
public static JavaElement getUnresolvedJavaElement(TypeBinding typeBinding, WorkingCopyOwner workingCopyOwner, BindingsToNodesMap bindingsToNodes) {
    if (typeBinding == null)
        return null;
    switch(typeBinding.kind()) {
        case Binding.ARRAY_TYPE:
            typeBinding = ((org.eclipse.jdt.internal.compiler.lookup.ArrayBinding) typeBinding).leafComponentType();
            return getUnresolvedJavaElement(typeBinding, workingCopyOwner, bindingsToNodes);
        case Binding.BASE_TYPE:
        case Binding.WILDCARD_TYPE:
        case Binding.INTERSECTION_TYPE:
            return null;
        default:
            if (typeBinding.isCapture())
                return null;
    }
    ReferenceBinding referenceBinding;
    if (typeBinding.isParameterizedType() || typeBinding.isRawType())
        referenceBinding = (ReferenceBinding) typeBinding.erasure();
    else
        referenceBinding = (ReferenceBinding) typeBinding;
    char[] fileName = referenceBinding.getFileName();
    if (referenceBinding.isLocalType() || referenceBinding.isAnonymousType()) {
        // local or anonymous type
        if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(fileName)) {
            int jarSeparator = CharOperation.indexOf(IDependent.JAR_FILE_ENTRY_SEPARATOR, fileName);
            // pkgEnd is exclusive
            int pkgEnd = CharOperation.lastIndexOf('/', fileName);
            if (pkgEnd == -1)
                pkgEnd = CharOperation.lastIndexOf(File.separatorChar, fileName);
            if (// if in a jar and no slash, it is a default package -> pkgEnd should be equal to jarSeparator
            jarSeparator != -1 && pkgEnd < jarSeparator)
                pkgEnd = jarSeparator;
            if (pkgEnd == -1)
                return null;
            IPackageFragment pkg = getPackageFragment(fileName, pkgEnd, jarSeparator);
            char[] constantPoolName = referenceBinding.constantPoolName();
            if (constantPoolName == null) {
                ClassFile classFile = (ClassFile) getClassFile(fileName);
                return classFile == null ? null : (JavaElement) classFile.getType();
            }
            pkgEnd = CharOperation.lastIndexOf('/', constantPoolName);
            char[] classFileName = CharOperation.subarray(constantPoolName, pkgEnd + 1, constantPoolName.length);
            ClassFile classFile = (ClassFile) pkg.getClassFile(new String(classFileName) + SuffixConstants.SUFFIX_STRING_class);
            return (JavaElement) classFile.getType();
        }
        ICompilationUnit cu = getCompilationUnit(fileName, workingCopyOwner);
        if (cu == null)
            return null;
        // must use getElementAt(...) as there is no back pointer to the defining method (scope is null after resolution has ended)
        try {
            int sourceStart = ((org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) referenceBinding).sourceStart;
            return (JavaElement) cu.getElementAt(sourceStart);
        } catch (JavaModelException e) {
            // does not exist
            return null;
        }
    } else if (referenceBinding.isTypeVariable()) {
        // type parameter
        final String typeVariableName = new String(referenceBinding.sourceName());
        org.eclipse.jdt.internal.compiler.lookup.Binding declaringElement = ((org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) referenceBinding).declaringElement;
        if (declaringElement instanceof MethodBinding) {
            IMethod declaringMethod = (IMethod) getUnresolvedJavaElement((MethodBinding) declaringElement, workingCopyOwner, bindingsToNodes);
            return (JavaElement) declaringMethod.getTypeParameter(typeVariableName);
        } else {
            IType declaringType = (IType) getUnresolvedJavaElement((TypeBinding) declaringElement, workingCopyOwner, bindingsToNodes);
            return (JavaElement) declaringType.getTypeParameter(typeVariableName);
        }
    } else {
        // case of a WilCardBinding that doesn't have a corresponding Java element
        if (fileName == null)
            return null;
        // member or top level type
        TypeBinding declaringTypeBinding = typeBinding.enclosingType();
        if (declaringTypeBinding == null) {
            // top level type
            if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(fileName)) {
                ClassFile classFile = (ClassFile) getClassFile(fileName);
                if (classFile == null)
                    return null;
                return (JavaElement) classFile.getType();
            }
            ICompilationUnit cu = getCompilationUnit(fileName, workingCopyOwner);
            if (cu == null)
                return null;
            return (JavaElement) cu.getType(new String(referenceBinding.sourceName()));
        } else {
            // member type
            IType declaringType = (IType) getUnresolvedJavaElement(declaringTypeBinding, workingCopyOwner, bindingsToNodes);
            if (declaringType == null)
                return null;
            return (JavaElement) declaringType.getType(new String(referenceBinding.sourceName()));
        }
    }
}
Also used : FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.jdt.internal.core.ClassFile) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) IType(org.eclipse.jdt.core.IType) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) IMethod(org.eclipse.jdt.core.IMethod)

Example 10 with TypeBinding

use of org.eclipse.jdt.internal.compiler.lookup.TypeBinding in project lombok by rzwitserloot.

the class EclipseHandlerUtil method makeType.

public static TypeReference makeType(TypeBinding binding, ASTNode pos, boolean allowCompound) {
    int dims = binding.dimensions();
    binding = binding.leafComponentType();
    // Primitives
    char[] base = null;
    switch(binding.id) {
        case TypeIds.T_int:
            base = TypeConstants.INT;
            break;
        case TypeIds.T_long:
            base = TypeConstants.LONG;
            break;
        case TypeIds.T_short:
            base = TypeConstants.SHORT;
            break;
        case TypeIds.T_byte:
            base = TypeConstants.BYTE;
            break;
        case TypeIds.T_double:
            base = TypeConstants.DOUBLE;
            break;
        case TypeIds.T_float:
            base = TypeConstants.FLOAT;
            break;
        case TypeIds.T_boolean:
            base = TypeConstants.BOOLEAN;
            break;
        case TypeIds.T_char:
            base = TypeConstants.CHAR;
            break;
        case TypeIds.T_void:
            base = TypeConstants.VOID;
            break;
        case TypeIds.T_null:
            return null;
    }
    if (base != null) {
        if (dims > 0) {
            TypeReference result = new ArrayTypeReference(base, dims, pos(pos));
            setGeneratedBy(result, pos);
            return result;
        }
        TypeReference result = new SingleTypeReference(base, pos(pos));
        setGeneratedBy(result, pos);
        return result;
    }
    if (binding.isAnonymousType()) {
        ReferenceBinding ref = (ReferenceBinding) binding;
        ReferenceBinding[] supers = ref.superInterfaces();
        if (supers == null || supers.length == 0)
            supers = new ReferenceBinding[] { ref.superclass() };
        if (supers[0] == null) {
            TypeReference result = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(pos, 3));
            setGeneratedBy(result, pos);
            return result;
        }
        return makeType(supers[0], pos, false);
    }
    if (binding instanceof CaptureBinding) {
        return makeType(((CaptureBinding) binding).wildcard, pos, allowCompound);
    }
    if (binding.isUnboundWildcard()) {
        if (!allowCompound) {
            TypeReference result = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(pos, 3));
            setGeneratedBy(result, pos);
            return result;
        } else {
            Wildcard out = new Wildcard(Wildcard.UNBOUND);
            setGeneratedBy(out, pos);
            out.sourceStart = pos.sourceStart;
            out.sourceEnd = pos.sourceEnd;
            return out;
        }
    }
    if (binding.isWildcard()) {
        WildcardBinding wildcard = (WildcardBinding) binding;
        if (wildcard.boundKind == Wildcard.EXTENDS) {
            if (!allowCompound) {
                return makeType(wildcard.bound, pos, false);
            } else {
                Wildcard out = new Wildcard(Wildcard.EXTENDS);
                setGeneratedBy(out, pos);
                out.bound = makeType(wildcard.bound, pos, false);
                out.sourceStart = pos.sourceStart;
                out.sourceEnd = pos.sourceEnd;
                return out;
            }
        } else if (allowCompound && wildcard.boundKind == Wildcard.SUPER) {
            Wildcard out = new Wildcard(Wildcard.SUPER);
            setGeneratedBy(out, pos);
            out.bound = makeType(wildcard.bound, pos, false);
            out.sourceStart = pos.sourceStart;
            out.sourceEnd = pos.sourceEnd;
            return out;
        } else {
            TypeReference result = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, poss(pos, 3));
            setGeneratedBy(result, pos);
            return result;
        }
    }
    // Keep moving up via 'binding.enclosingType()' and gather generics from each binding. We stop after a local type, or a static type, or a top-level type.
    // Finally, add however many nullTypeArgument[] arrays as that are missing, inverse the list, toArray it, and use that as PTR's typeArgument argument.
    List<TypeReference[]> params = new ArrayList<TypeReference[]>();
    /* Calculate generics */
    {
        TypeBinding b = binding;
        while (true) {
            boolean isFinalStop = b.isLocalType() || !b.isMemberType() || b.enclosingType() == null;
            TypeReference[] tyParams = null;
            if (b instanceof ParameterizedTypeBinding) {
                ParameterizedTypeBinding paramized = (ParameterizedTypeBinding) b;
                if (paramized.arguments != null) {
                    tyParams = new TypeReference[paramized.arguments.length];
                    for (int i = 0; i < tyParams.length; i++) {
                        tyParams[i] = makeType(paramized.arguments[i], pos, true);
                    }
                }
            }
            params.add(tyParams);
            if (isFinalStop)
                break;
            b = b.enclosingType();
        }
    }
    char[][] parts;
    if (binding.isTypeVariable()) {
        parts = new char[][] { binding.shortReadableName() };
    } else if (binding.isLocalType()) {
        parts = new char[][] { binding.sourceName() };
    } else {
        String[] pkg = new String(binding.qualifiedPackageName()).split("\\.");
        String[] name = new String(binding.qualifiedSourceName()).split("\\.");
        if (pkg.length == 1 && pkg[0].isEmpty())
            pkg = new String[0];
        parts = new char[pkg.length + name.length][];
        int ptr;
        for (ptr = 0; ptr < pkg.length; ptr++) parts[ptr] = pkg[ptr].toCharArray();
        for (; ptr < pkg.length + name.length; ptr++) parts[ptr] = name[ptr - pkg.length].toCharArray();
    }
    while (params.size() < parts.length) params.add(null);
    Collections.reverse(params);
    boolean isParamized = false;
    for (TypeReference[] tyParams : params) {
        if (tyParams != null) {
            isParamized = true;
            break;
        }
    }
    if (isParamized) {
        if (parts.length > 1) {
            TypeReference[][] typeArguments = params.toArray(new TypeReference[0][]);
            TypeReference result = new ParameterizedQualifiedTypeReference(parts, typeArguments, dims, poss(pos, parts.length));
            setGeneratedBy(result, pos);
            return result;
        }
        TypeReference result = new ParameterizedSingleTypeReference(parts[0], params.get(0), dims, pos(pos));
        setGeneratedBy(result, pos);
        return result;
    }
    if (dims > 0) {
        if (parts.length > 1) {
            TypeReference result = new ArrayQualifiedTypeReference(parts, dims, poss(pos, parts.length));
            setGeneratedBy(result, pos);
            return result;
        }
        TypeReference result = new ArrayTypeReference(parts[0], dims, pos(pos));
        setGeneratedBy(result, pos);
        return result;
    }
    if (parts.length > 1) {
        TypeReference result = new QualifiedTypeReference(parts, poss(pos, parts.length));
        setGeneratedBy(result, pos);
        return result;
    }
    TypeReference result = new SingleTypeReference(parts[0], pos(pos));
    setGeneratedBy(result, pos);
    return result;
}
Also used : ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) WildcardBinding(org.eclipse.jdt.internal.compiler.lookup.WildcardBinding) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ArrayList(java.util.ArrayList) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) CaptureBinding(org.eclipse.jdt.internal.compiler.lookup.CaptureBinding) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference) Wildcard(org.eclipse.jdt.internal.compiler.ast.Wildcard) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ArrayQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) 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) ArrayTypeReference(org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference)

Aggregations

TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)13 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)7 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)7 ArrayList (java.util.ArrayList)5 ParameterizedTypeBinding (org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding)5 TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)4 EclipseNode (lombok.eclipse.EclipseNode)3 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)3 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)3 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)3 Binding (org.eclipse.jdt.internal.compiler.lookup.Binding)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 IMethod (org.eclipse.jdt.core.IMethod)2 IType (org.eclipse.jdt.core.IType)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 CompletionOnMemberAccess (org.eclipse.jdt.internal.codeassist.complete.CompletionOnMemberAccess)2 CompletionOnQualifiedNameReference (org.eclipse.jdt.internal.codeassist.complete.CompletionOnQualifiedNameReference)2 CompletionOnSingleNameReference (org.eclipse.jdt.internal.codeassist.complete.CompletionOnSingleNameReference)2 ASTNode (org.eclipse.jdt.internal.compiler.ast.ASTNode)2 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)2