Example usage for org.eclipse.jdt.internal.compiler.lookup TypeVariableBinding sourceName

List of usage examples for org.eclipse.jdt.internal.compiler.lookup TypeVariableBinding sourceName

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup TypeVariableBinding sourceName.

Prototype

@Override
    public char[] sourceName() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

private void selectFrom(Binding binding, CompilationUnitDeclaration parsedUnit, ICompilationUnit unit,
        boolean isDeclaration) {
    if (binding instanceof TypeVariableBinding) {
        TypeVariableBinding typeVariableBinding = (TypeVariableBinding) binding;
        Binding enclosingElement = typeVariableBinding.declaringElement;
        this.noProposal = false;

        if (enclosingElement instanceof SourceTypeBinding) {
            SourceTypeBinding enclosingType = (SourceTypeBinding) enclosingElement;
            if (isLocal(enclosingType) && this.requestor instanceof SelectionRequestor) {
                ((SelectionRequestor) this.requestor).acceptLocalTypeParameter(typeVariableBinding);
            } else {
                this.requestor.acceptTypeParameter(enclosingType.qualifiedPackageName(),
                        enclosingType.qualifiedSourceName(), typeVariableBinding.sourceName(), false,
                        this.actualSelectionStart, this.actualSelectionEnd);
            }/*www.  jav a 2  s  .c o m*/
        } else if (enclosingElement instanceof MethodBinding) {
            MethodBinding enclosingMethod = (MethodBinding) enclosingElement;
            if (isLocal(enclosingMethod.declaringClass) && this.requestor instanceof SelectionRequestor) {
                ((SelectionRequestor) this.requestor).acceptLocalMethodTypeParameter(typeVariableBinding);
            } else {
                this.requestor.acceptMethodTypeParameter(enclosingMethod.declaringClass.qualifiedPackageName(),
                        enclosingMethod.declaringClass.qualifiedSourceName(),
                        enclosingMethod.isConstructor() ? enclosingMethod.declaringClass.sourceName()
                                : enclosingMethod.selector,
                        enclosingMethod.sourceStart(), enclosingMethod.sourceEnd(),
                        typeVariableBinding.sourceName(), false, this.actualSelectionStart,
                        this.actualSelectionEnd);
            }
        }
        this.acceptedAnswer = true;
    } else if (binding instanceof ReferenceBinding) {
        ReferenceBinding typeBinding = (ReferenceBinding) binding;
        if (typeBinding instanceof ProblemReferenceBinding) {
            TypeBinding closestMatch = typeBinding.closestMatch();
            if (closestMatch instanceof ReferenceBinding) {
                typeBinding = (ReferenceBinding) closestMatch;
            } else {
                typeBinding = null;
            }
        }
        if (typeBinding == null)
            return;
        if (isLocal(typeBinding) && this.requestor instanceof SelectionRequestor) {
            this.noProposal = false;
            ((SelectionRequestor) this.requestor).acceptLocalType(typeBinding);
        } else {
            this.noProposal = false;

            this.requestor.acceptType(typeBinding.qualifiedPackageName(), typeBinding.qualifiedSourceName(),
                    typeBinding.modifiers, false, typeBinding.computeUniqueKey(), this.actualSelectionStart,
                    this.actualSelectionEnd);
        }
        this.acceptedAnswer = true;
    } else if (binding instanceof MethodBinding) {
        MethodBinding methodBinding = getCorrectMethodBinding((MethodBinding) binding);
        this.noProposal = false;

        boolean isValuesOrValueOf = false;
        if (binding instanceof SyntheticMethodBinding) {
            SyntheticMethodBinding syntheticMethodBinding = (SyntheticMethodBinding) binding;
            if (syntheticMethodBinding.purpose == SyntheticMethodBinding.EnumValues
                    || syntheticMethodBinding.purpose == SyntheticMethodBinding.EnumValueOf) {
                isValuesOrValueOf = true;
            }
        }

        if (!isValuesOrValueOf && !methodBinding.isSynthetic()) {
            TypeBinding[] parameterTypes = methodBinding.original().parameters;
            int length = parameterTypes.length;
            char[][] parameterPackageNames = new char[length][];
            char[][] parameterTypeNames = new char[length][];
            String[] parameterSignatures = new String[length];
            for (int i = 0; i < length; i++) {
                parameterPackageNames[i] = parameterTypes[i].qualifiedPackageName();
                parameterTypeNames[i] = parameterTypes[i].qualifiedSourceName();
                parameterSignatures[i] = new String(getSignature(parameterTypes[i])).replace('/', '.');
            }

            TypeVariableBinding[] typeVariables = methodBinding.original().typeVariables;
            length = typeVariables == null ? 0 : typeVariables.length;
            char[][] typeParameterNames = new char[length][];
            char[][][] typeParameterBoundNames = new char[length][][];
            for (int i = 0; i < length; i++) {
                TypeVariableBinding typeVariable = typeVariables[i];
                typeParameterNames[i] = typeVariable.sourceName;
                if (typeVariable.firstBound == null) {
                    typeParameterBoundNames[i] = new char[0][];
                } else if (TypeBinding.equalsEquals(typeVariable.firstBound, typeVariable.superclass)) {
                    int boundCount = 1
                            + (typeVariable.superInterfaces == null ? 0 : typeVariable.superInterfaces.length);
                    typeParameterBoundNames[i] = new char[boundCount][];
                    typeParameterBoundNames[i][0] = typeVariable.superclass.sourceName;
                    for (int j = 1; j < boundCount; j++) {
                        typeParameterBoundNames[i][j] = typeVariables[i].superInterfaces[j - 1].sourceName;
                    }
                } else {
                    int boundCount = typeVariable.superInterfaces == null ? 0
                            : typeVariable.superInterfaces.length;
                    typeParameterBoundNames[i] = new char[boundCount][];
                    for (int j = 0; j < boundCount; j++) {
                        typeParameterBoundNames[i][j] = typeVariables[i].superInterfaces[j].sourceName;
                    }
                }
            }

            ReferenceBinding declaringClass = methodBinding.declaringClass;
            if (isLocal(declaringClass) && this.requestor instanceof SelectionRequestor) {
                ((SelectionRequestor) this.requestor).acceptLocalMethod(methodBinding);
            } else {
                this.requestor.acceptMethod(declaringClass.qualifiedPackageName(),
                        declaringClass.qualifiedSourceName(),
                        declaringClass.enclosingType() == null ? null
                                : new String(getSignature(declaringClass.enclosingType())),
                        methodBinding.isConstructor() ? declaringClass.sourceName() : methodBinding.selector,
                        parameterPackageNames, parameterTypeNames, parameterSignatures, typeParameterNames,
                        typeParameterBoundNames, methodBinding.isConstructor(), isDeclaration,
                        methodBinding.computeUniqueKey(), this.actualSelectionStart, this.actualSelectionEnd);
            }
        }
        this.acceptedAnswer = true;
    } else if (binding instanceof FieldBinding) {
        FieldBinding fieldBinding = (FieldBinding) binding;
        ReferenceBinding declaringClass = fieldBinding.declaringClass;
        if (declaringClass != null) { // arraylength
            this.noProposal = false;
            if (isLocal(declaringClass) && this.requestor instanceof SelectionRequestor) {
                ((SelectionRequestor) this.requestor).acceptLocalField(fieldBinding);
            } else {
                // if the binding is a problem field binding, we want to make sure
                // we can retrieve the closestMatch if the problem reason is NotVisible
                FieldBinding currentFieldBinding = fieldBinding;
                while (currentFieldBinding instanceof ProblemFieldBinding) {
                    ProblemFieldBinding problemFieldBinding = (ProblemFieldBinding) currentFieldBinding;
                    if (problemFieldBinding.problemId() == ProblemReasons.NotVisible) {
                        currentFieldBinding = problemFieldBinding.closestMatch;
                    } else {
                        currentFieldBinding = null;
                    }
                }
                char[] fieldName = null;
                char[] key = null;
                if (currentFieldBinding != null) {
                    fieldName = currentFieldBinding.name;
                    key = currentFieldBinding.computeUniqueKey();
                } else {
                    fieldName = fieldBinding.name;
                    key = fieldBinding.computeUniqueKey();
                }
                this.requestor.acceptField(declaringClass.qualifiedPackageName(),
                        declaringClass.qualifiedSourceName(), fieldName, false, key, this.actualSelectionStart,
                        this.actualSelectionEnd);
            }
            this.acceptedAnswer = true;
        }
    } else if (binding instanceof LocalVariableBinding) {
        if (this.requestor instanceof SelectionRequestor) {
            ((SelectionRequestor) this.requestor).acceptLocalVariable((LocalVariableBinding) binding, unit);
            this.acceptedAnswer = true;
        } else {
            // open on the type of the variable
            selectFrom(((LocalVariableBinding) binding).type, parsedUnit, false);
        }
    } else if (binding instanceof ArrayBinding) {
        selectFrom(((ArrayBinding) binding).leafComponentType, parsedUnit, false);
        // open on the type of the array
    } else if (binding instanceof PackageBinding) {
        PackageBinding packageBinding = (PackageBinding) binding;
        this.noProposal = false;
        this.requestor.acceptPackage(packageBinding.readableName());
        this.acceptedAnswer = true;
    } else if (binding instanceof BaseTypeBinding) {
        this.acceptedAnswer = true;
    }
}

From source file:lombok.eclipse.agent.PatchDelegate.java

License:Open Source License

public static void checkConflictOfTypeVarNames(BindingTuple binding, EclipseNode typeNode)
        throws CantMakeDelegates {
    TypeVariableBinding[] typeVars = binding.parameterized.typeVariables();
    if (typeVars == null || typeVars.length == 0)
        return;/* w  w w .  ja  v a 2 s.c o m*/

    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;
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.AstGenerator.java

License:Open Source License

private TypeReference createArrayTypeReference(TypeBinding elementType, int dims, boolean makeGeneric) {
    // check generics:
    if (elementType.isTypeVariable()) {
        TypeVariableBinding typeVariable = (TypeVariableBinding) elementType;
        char[] variableName = typeVariable.sourceName();
        if (dims == 0)
            return new SingleTypeReference(variableName, this.pos);
        else/*from www . j  a  v a2s .c o m*/
            return new ArrayTypeReference(variableName, dims, this.pos);
    } else if (makeGeneric && elementType.isParameterizedType()) {
        // this branch currently cannot handle references of this shape: Outer<T>.Inner
        // should that be needed at some point the following variant might do:
        /*
                 ParameterizedTypeBinding paramType = (ParameterizedTypeBinding)elementType;
                 char[][] compoundName = paramType.compoundName;
                 char[]tokenString = CharOperation.concatWith(compoundName, '$');
                 compoundName = CharOperation.splitOn('$', tokenString);
                 TypeReference[][] arguments = new TypeReference[compoundName.length][];
                 int argPos = compoundName.length-1;
                 boolean haveArguments = false;         
                 do {
                    TypeBinding[] argumentTypes = paramType.arguments;
                    if (argumentTypes != null) {
                       haveArguments = true;
                       TypeReference[] currentArgs = new TypeReference[argumentTypes.length];
                       arguments[argPos] = currentArgs;
                       for (int i = 0; i < argumentTypes.length; i++) {
          currentArgs[i] = typeReference(argumentTypes[i]);
                       }
                    }
                    ReferenceBinding enclosing = paramType.enclosingType();
                    argPos--;
                    if (enclosing instanceof ParameterizedTypeBinding)
                       paramType = (ParameterizedTypeBinding) enclosing;
                    else break;
                 } while (argPos >= 0);
                 if (haveArguments) {
                    long[] poss = new long[compoundName.length];
                    Arrays.fill(poss, this.pos);
                    return new ParameterizedQualifiedTypeReference(compoundName, arguments, dims, poss);
                 }
         */
        ParameterizedTypeBinding paramType = (ParameterizedTypeBinding) elementType;
        TypeBinding[] argumentTypes = paramType.arguments;
        if (argumentTypes != null) {
            char[][] compoundName = paramType.compoundName;
            char[] tokenString = CharOperation.concatWith(compoundName, '$');
            compoundName = CharOperation.splitOn('$', tokenString);
            TypeReference[][] arguments = new TypeReference[compoundName.length][];
            TypeReference[] lastArgs = new TypeReference[argumentTypes.length];
            arguments[compoundName.length - 1] = lastArgs;
            for (int i = 0; i < argumentTypes.length; i++) {
                lastArgs[i] = typeReference(argumentTypes[i]);
            }
            long[] poss = new long[argumentTypes.length];
            Arrays.fill(poss, this.pos);
            return new ParameterizedQualifiedTypeReference(compoundName, arguments, dims, poss);
        }
    } else if (elementType.isWildcard()) {
        WildcardBinding wildcard = (WildcardBinding) elementType;
        Wildcard result = new Wildcard(wildcard.boundKind);
        result.sourceStart = this.sourceStart;
        result.sourceEnd = this.sourceEnd;
        if (wildcard.bound != null)
            result.bound = typeReference(wildcard.bound);
        // Note(SH): I don't see dims to be relevant here, OK?
        return result;
    }

    // from this point: not generic:
    char[] typeName = "void".toCharArray(); //$NON-NLS-1$
    char[][] qname = null;
    TypeAnchorReference anchorRef = null;
    if (elementType instanceof BaseTypeBinding) {
        typeName = ((BaseTypeBinding) elementType).simpleName;
    } else if (elementType instanceof ReferenceBinding) {
        ReferenceBinding referenceBinding = (ReferenceBinding) elementType;
        qname = TypeAnalyzer.compoundNameOfReferenceType(referenceBinding, true, false);
        if (referenceBinding instanceof DependentTypeBinding
                && ((DependentTypeBinding) referenceBinding).hasExplicitAnchor()) {
            DependentTypeBinding depBind = (DependentTypeBinding) referenceBinding;
            anchorRef = typeAnchorReference(depBind.getAnchor());
            typeName = referenceBinding.internalName();
            qname = null;
        }
    }
    char[] sname;
    if ((qname != null) && (qname.length == 1)) {
        sname = qname[0]; // use this
        qname = null; // not this
    } else {
        sname = typeName;
    }
    long[] poss = null;
    if (qname != null) {
        poss = new long[qname.length];
        Arrays.fill(poss, this.pos);
    }

    if (anchorRef == null) {
        if (dims == 0) {
            if (qname == null)
                return new SingleTypeReference(sname, this.pos);
            else
                return new QualifiedTypeReference(qname, poss);
        } else {
            if (qname == null)
                return new ArrayTypeReference(sname, dims, this.pos);
            else {
                return new ArrayQualifiedTypeReference(qname, dims, poss);
            }
        }
    } else {
        TypeReference[] typeReferences = new TypeReference[] { anchorRef };
        assert qname == null;
        return new ParameterizedSingleTypeReference(sname, typeReferences, dims, this.pos);
    }
}