Search in sources :

Example 1 with Binding

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

the class PatchExtensionMethod method resolveType.

public static TypeBinding resolveType(TypeBinding resolvedType, MessageSend methodCall, BlockScope scope) {
    List<Extension> extensions = new ArrayList<Extension>();
    TypeDeclaration decl = scope.classScope().referenceContext;
    EclipseNode owningType = null;
    for (EclipseNode typeNode = getTypeNode(decl); typeNode != null; typeNode = upToType(typeNode)) {
        Annotation ann = getAnnotation(ExtensionMethod.class, typeNode);
        if (ann != null) {
            extensions.addAll(0, getApplicableExtensionMethods(typeNode, ann, methodCall.receiver.resolvedType));
            if (owningType == null)
                owningType = typeNode;
        }
    }
    boolean skip = false;
    if (methodCall.receiver instanceof ThisReference && (((ThisReference) methodCall.receiver).bits & ASTNode.IsImplicitThis) != 0)
        skip = true;
    if (methodCall.receiver instanceof SuperReference)
        skip = true;
    if (methodCall.receiver instanceof NameReference) {
        Binding binding = ((NameReference) methodCall.receiver).binding;
        if (binding instanceof TypeBinding)
            skip = true;
    }
    if (!skip)
        for (Extension extension : extensions) {
            if (!extension.suppressBaseMethods && !(methodCall.binding instanceof ProblemMethodBinding))
                continue;
            for (MethodBinding extensionMethod : extension.extensionMethods) {
                if (!Arrays.equals(methodCall.selector, extensionMethod.selector))
                    continue;
                MessageSend_postponedErrors.clear(methodCall);
                if (methodCall.receiver instanceof ThisReference) {
                    methodCall.receiver.bits &= ~ASTNode.IsImplicitThis;
                }
                List<Expression> arguments = new ArrayList<Expression>();
                arguments.add(methodCall.receiver);
                if (methodCall.arguments != null)
                    arguments.addAll(Arrays.asList(methodCall.arguments));
                List<TypeBinding> argumentTypes = new ArrayList<TypeBinding>();
                for (Expression argument : arguments) {
                    if (argument.resolvedType != null)
                        argumentTypes.add(argument.resolvedType);
                // TODO: Instead of just skipping nulls entirely, there is probably a 'unresolved type' placeholder. THAT is what we ought to be adding here!
                }
                Expression[] originalArgs = methodCall.arguments;
                methodCall.arguments = arguments.toArray(new Expression[0]);
                MethodBinding fixedBinding = scope.getMethod(extensionMethod.declaringClass, methodCall.selector, argumentTypes.toArray(new TypeBinding[0]), methodCall);
                if (fixedBinding instanceof ProblemMethodBinding) {
                    methodCall.arguments = originalArgs;
                    if (fixedBinding.declaringClass != null) {
                        PostponedInvalidMethodError.invoke(scope.problemReporter(), methodCall, fixedBinding, scope);
                    }
                } else {
                    for (int i = 0, iend = arguments.size(); i < iend; i++) {
                        Expression arg = arguments.get(i);
                        if (fixedBinding.parameters[i].isArrayType() != arg.resolvedType.isArrayType())
                            break;
                        if (arg instanceof MessageSend) {
                            ((MessageSend) arg).valueCast = arg.resolvedType;
                        }
                        if (!fixedBinding.parameters[i].isBaseType() && arg.resolvedType.isBaseType()) {
                            int id = arg.resolvedType.id;
                            // magic see TypeIds
                            arg.implicitConversion = TypeIds.BOXING | (id + (id << 4));
                        } else if (fixedBinding.parameters[i].isBaseType() && !arg.resolvedType.isBaseType()) {
                            int id = fixedBinding.parameters[i].id;
                            // magic see TypeIds
                            arg.implicitConversion = TypeIds.UNBOXING | (id + (id << 4));
                        }
                    }
                    methodCall.receiver = createNameRef(extensionMethod.declaringClass, methodCall);
                    methodCall.actualReceiverType = extensionMethod.declaringClass;
                    methodCall.binding = fixedBinding;
                    methodCall.resolvedType = methodCall.binding.returnType;
                }
                return methodCall.resolvedType;
            }
        }
    PostponedError error = MessageSend_postponedErrors.get(methodCall);
    if (error != null)
        error.fire();
    MessageSend_postponedErrors.clear(methodCall);
    return resolvedType;
}
Also used : Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference) NameReference(org.eclipse.jdt.internal.compiler.ast.NameReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) EclipseHandlerUtil.createAnnotation(lombok.eclipse.handlers.EclipseHandlerUtil.createAnnotation) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) SuperReference(org.eclipse.jdt.internal.compiler.ast.SuperReference) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) EclipseNode(lombok.eclipse.EclipseNode) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) ArrayList(java.util.ArrayList) List(java.util.List) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)

Example 2 with Binding

use of org.eclipse.jdt.internal.compiler.lookup.Binding in project spoon by INRIA.

the class ReferenceBuilder method getDeclaringReferenceFromImports.

/**
 * Try to get the declaring reference (package or type) from imports of the current
 * compilation unit declaration (current class). This method returns a CtReference
 * which can be a CtTypeReference if it retrieves the information in an static import,
 * a CtPackageReference if it retrieves the information in an standard import, otherwise
 * it returns null.
 *
 * @param expectedName Name expected in imports.
 * @return CtReference which can be a CtTypeReference, a CtPackageReference or null.
 */
CtReference getDeclaringReferenceFromImports(char[] expectedName) {
    CompilationUnitDeclaration cuDeclaration = this.jdtTreeBuilder.getContextBuilder().compilationunitdeclaration;
    LookupEnvironment environment = cuDeclaration.scope.environment;
    if (cuDeclaration != null && cuDeclaration.imports != null) {
        for (ImportReference anImport : cuDeclaration.imports) {
            if (CharOperation.equals(anImport.getImportName()[anImport.getImportName().length - 1], expectedName)) {
                if (anImport.isStatic()) {
                    int indexDeclaring = 2;
                    if ((anImport.bits & ASTNode.OnDemand) != 0) {
                        // With .*
                        indexDeclaring = 1;
                    }
                    char[][] packageName = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - indexDeclaring);
                    char[][] className = CharOperation.subarray(anImport.getImportName(), anImport.getImportName().length - indexDeclaring, anImport.getImportName().length - (indexDeclaring - 1));
                    PackageBinding aPackage;
                    try {
                        if (packageName.length != 0) {
                            aPackage = environment.createPackage(packageName);
                        } else {
                            aPackage = null;
                        }
                        final MissingTypeBinding declaringType = environment.createMissingType(aPackage, className);
                        this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports = true;
                        final CtTypeReference<Object> typeReference = getTypeReference(declaringType);
                        this.jdtTreeBuilder.getContextBuilder().ignoreComputeImports = false;
                        return typeReference;
                    } catch (NullPointerException e) {
                        return null;
                    }
                } else {
                    PackageBinding packageBinding = null;
                    char[][] chars = CharOperation.subarray(anImport.getImportName(), 0, anImport.getImportName().length - 1);
                    // ArrayIndexOutOfBoundsException if `chars.length == 0`. Fixes #759.
                    if (chars.length > 0) {
                        Binding someBinding = cuDeclaration.scope.findImport(chars, false, false);
                        if (someBinding != null && someBinding.isValidBinding() && someBinding instanceof PackageBinding) {
                            packageBinding = (PackageBinding) someBinding;
                        } else {
                            try {
                                packageBinding = environment.createPackage(chars);
                            } catch (NullPointerException e) {
                                packageBinding = null;
                            }
                        }
                    }
                    if (packageBinding == null || packageBinding instanceof ProblemPackageBinding) {
                        // Big crisis here. We are already in noclasspath mode but JDT doesn't support always
                        // creation of a package in this mode. So, if we are in this brace, we make the job of JDT...
                        packageBinding = new PackageBinding(chars, null, environment, environment.module);
                    }
                    return getPackageReference(packageBinding);
                }
            }
        }
    }
    return null;
}
Also used : BinaryTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding) LocalVariableBinding(org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding) FieldBinding(org.eclipse.jdt.internal.compiler.lookup.FieldBinding) ProblemBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemBinding) ParameterizedTypeBinding(org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) CatchParameterBinding(org.eclipse.jdt.internal.compiler.lookup.CatchParameterBinding) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding) ArrayBinding(org.eclipse.jdt.internal.compiler.lookup.ArrayBinding) SourceTypeBinding(org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding) WildcardBinding(org.eclipse.jdt.internal.compiler.lookup.WildcardBinding) CaptureBinding(org.eclipse.jdt.internal.compiler.lookup.CaptureBinding) Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) JDTTreeBuilderQuery.searchTypeBinding(spoon.support.compiler.jdt.JDTTreeBuilderQuery.searchTypeBinding) TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) ProblemMethodBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding) PolyTypeBinding(org.eclipse.jdt.internal.compiler.lookup.PolyTypeBinding) ReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding) LocalTypeBinding(org.eclipse.jdt.internal.compiler.lookup.LocalTypeBinding) BaseTypeBinding(org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding) RawTypeBinding(org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding) ProblemReferenceBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemReferenceBinding) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) LookupEnvironment(org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment) ImportReference(org.eclipse.jdt.internal.compiler.ast.ImportReference) MissingTypeBinding(org.eclipse.jdt.internal.compiler.lookup.MissingTypeBinding) CompilationUnitDeclaration(org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding) PackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding) ProblemPackageBinding(org.eclipse.jdt.internal.compiler.lookup.ProblemPackageBinding)

Example 3 with Binding

use of org.eclipse.jdt.internal.compiler.lookup.Binding 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 4 with Binding

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

the class PatchExtensionMethodCompletionProposal method getFirstParameterType.

static TypeBinding getFirstParameterType(TypeDeclaration decl, CompletionProposalCollector completionProposalCollector) {
    TypeBinding firstParameterType = null;
    ASTNode node = getAssistNode(completionProposalCollector);
    if (node == null)
        return null;
    if (!(node instanceof CompletionOnQualifiedNameReference) && !(node instanceof CompletionOnSingleNameReference) && !(node instanceof CompletionOnMemberAccess))
        return null;
    // Never offer on 'super.<autocomplete>'.
    if (node instanceof FieldReference && ((FieldReference) node).receiver instanceof SuperReference)
        return null;
    if (node instanceof NameReference) {
        Binding binding = ((NameReference) node).binding;
        /*			if ((node instanceof SingleNameReference) && (((SingleNameReference) node).token.length == 0)) {
				firstParameterType = decl.binding;
			} else */
        if (binding instanceof VariableBinding) {
            firstParameterType = ((VariableBinding) binding).type;
        }
    } else if (node instanceof FieldReference) {
        firstParameterType = ((FieldReference) node).actualReceiverType;
    }
    return firstParameterType;
}
Also used : Binding(org.eclipse.jdt.internal.compiler.lookup.Binding) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) MethodBinding(org.eclipse.jdt.internal.compiler.lookup.MethodBinding) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) CompletionOnQualifiedNameReference(org.eclipse.jdt.internal.codeassist.complete.CompletionOnQualifiedNameReference) NameReference(org.eclipse.jdt.internal.compiler.ast.NameReference) CompletionOnSingleNameReference(org.eclipse.jdt.internal.codeassist.complete.CompletionOnSingleNameReference) CompletionOnMemberAccess(org.eclipse.jdt.internal.codeassist.complete.CompletionOnMemberAccess) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) TypeBinding(org.eclipse.jdt.internal.compiler.lookup.TypeBinding) ASTNode(org.eclipse.jdt.internal.compiler.ast.ASTNode) CompletionOnSingleNameReference(org.eclipse.jdt.internal.codeassist.complete.CompletionOnSingleNameReference) CompletionOnQualifiedNameReference(org.eclipse.jdt.internal.codeassist.complete.CompletionOnQualifiedNameReference) VariableBinding(org.eclipse.jdt.internal.compiler.lookup.VariableBinding) SuperReference(org.eclipse.jdt.internal.compiler.ast.SuperReference)

Aggregations

Binding (org.eclipse.jdt.internal.compiler.lookup.Binding)4 MethodBinding (org.eclipse.jdt.internal.compiler.lookup.MethodBinding)4 TypeBinding (org.eclipse.jdt.internal.compiler.lookup.TypeBinding)4 ReferenceBinding (org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)3 NameReference (org.eclipse.jdt.internal.compiler.ast.NameReference)2 SuperReference (org.eclipse.jdt.internal.compiler.ast.SuperReference)2 FieldBinding (org.eclipse.jdt.internal.compiler.lookup.FieldBinding)2 ProblemMethodBinding (org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding)2 VariableBinding (org.eclipse.jdt.internal.compiler.lookup.VariableBinding)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EclipseNode (lombok.eclipse.EclipseNode)1 EclipseHandlerUtil.createAnnotation (lombok.eclipse.handlers.EclipseHandlerUtil.createAnnotation)1 IClassFile (org.eclipse.jdt.core.IClassFile)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IMethod (org.eclipse.jdt.core.IMethod)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IType (org.eclipse.jdt.core.IType)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1