List of usage examples for org.eclipse.jdt.internal.compiler.lookup TypeBinding isArrayType
public final boolean isArrayType()
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java
License:Open Source License
private MethodBinding getMethodBinding0(MethodPattern methodPattern) { if (this.unitScope == null) return null; // Try to get binding from cache Binding binding = (Binding) this.bindings.get(methodPattern); if (binding != null) { if (binding instanceof MethodBinding && binding.isValidBinding()) return (MethodBinding) binding; return null; }//from w ww. j a v a2 s . c o m // Get binding from unit scope char[] typeName = PatternLocator.qualifiedPattern(methodPattern.declaringSimpleName, methodPattern.declaringQualification); if (typeName == null) { if (methodPattern.declaringType == null) return null; typeName = methodPattern.declaringType.getFullyQualifiedName().toCharArray(); } TypeBinding declaringTypeBinding = getType(typeName, typeName); if (declaringTypeBinding != null) { if (declaringTypeBinding.isArrayType()) { declaringTypeBinding = declaringTypeBinding.leafComponentType(); } if (!declaringTypeBinding.isBaseType()) { char[][] parameterTypes = methodPattern.parameterSimpleNames; if (parameterTypes == null) return null; int paramTypeslength = parameterTypes.length; ReferenceBinding referenceBinding = (ReferenceBinding) declaringTypeBinding; MethodBinding[] methods = referenceBinding.getMethods(methodPattern.selector); int methodsLength = methods.length; TypeVariableBinding[] refTypeVariables = referenceBinding.typeVariables(); int typeVarLength = refTypeVariables == null ? 0 : refTypeVariables.length; for (int i = 0; i < methodsLength; i++) { TypeBinding[] methodParameters = methods[i].parameters; int paramLength = methodParameters == null ? 0 : methodParameters.length; TypeVariableBinding[] methodTypeVariables = methods[i].typeVariables; int methTypeVarLength = methodTypeVariables == null ? 0 : methodTypeVariables.length; boolean found = false; if (methodParameters != null && paramLength == paramTypeslength) { for (int p = 0; p < paramLength; p++) { if (CharOperation.equals(methodParameters[p].sourceName(), parameterTypes[p])) { // param erasure match found = true; } else { // type variable found = false; if (refTypeVariables != null) { for (int v = 0; v < typeVarLength; v++) { if (!CharOperation.equals(refTypeVariables[v].sourceName, parameterTypes[p])) { found = false; break; } found = true; } } if (!found && methodTypeVariables != null) { for (int v = 0; v < methTypeVarLength; v++) { if (!CharOperation.equals(methodTypeVariables[v].sourceName, parameterTypes[p])) { found = false; break; } found = true; } } if (!found) break; } } } if (found) { this.bindings.put(methodPattern, methods[i]); return methods[i]; } } } } this.bindings.put(methodPattern, new ProblemMethodBinding(methodPattern.selector, null, ProblemReasons.NotFound)); return null; }
From source file:com.redhat.ceylon.eclipse.core.model.mirror.JDTAnnotation.java
License:Open Source License
private Object convertValue(TypeBinding returnType, Object value) { if (value.getClass().isArray()) { Object[] array = (Object[]) value; List<Object> values = new ArrayList<Object>(array.length); TypeBinding elementType = ((ArrayBinding) returnType).elementsType(); for (Object val : array) values.add(convertValue(elementType, val)); return values; }/*from w w w . j av a2 s . c o m*/ if (returnType.isArrayType()) { // got a single value but expecting array List<Object> values = new ArrayList<Object>(1); TypeBinding elementType = ((ArrayBinding) returnType).elementsType(); values.add(convertValue(elementType, value)); return values; } if (value instanceof AnnotationBinding) { return new JDTAnnotation((AnnotationBinding) value); } if (value instanceof TypeBinding) { return new JDTType((TypeBinding) value); } if (value instanceof FieldBinding) { return new String(((FieldBinding) value).name); } if (value instanceof Constant) { Constant constant = (Constant) value; return JDTUtils.fromConstant(constant); } return value; }
From source file:lombok.eclipse.agent.PatchVal.java
License:Open Source License
private static TypeBinding getForEachComponentType(Expression collection, BlockScope scope) { if (collection != null) { TypeBinding resolved = collection.resolvedType; if (resolved == null) resolved = collection.resolveType(scope); if (resolved == null) return null; if (resolved.isArrayType()) { resolved = ((ArrayBinding) resolved).elementsType(); return resolved; } else if (resolved instanceof ReferenceBinding) { ReferenceBinding iterableType = ((ReferenceBinding) resolved) .findSuperTypeOriginatingFrom(TypeIds.T_JavaLangIterable, false); TypeBinding[] arguments = null; if (iterableType != null) switch (iterableType.kind()) { case Binding.GENERIC_TYPE: // for (T t : Iterable<T>) - in case used inside Iterable itself arguments = iterableType.typeVariables(); break; case Binding.PARAMETERIZED_TYPE: // for(E e : Iterable<E>) arguments = ((ParameterizedTypeBinding) iterableType).arguments; break; case Binding.RAW_TYPE: // for(Object e : Iterable) return null; }//from w ww . j a v a 2 s. c o m if (arguments != null && arguments.length == 1) { return arguments[0]; } } } return null; }
From source file:org.codehaus.jdt.groovy.internal.compiler.ast.JDTAnnotationNode.java
License:Open Source License
private Expression createExpressionFor(TypeBinding b, Object value) { if (b.isArrayType()) { ListExpression listExpression = new ListExpression(); // FIXASC is it a groovy optimization that if the value is expected to be an array you don't have to // write it as such if (value.getClass().isArray()) { Object[] values = (Object[]) value; for (Object v : values) { listExpression.addExpression(createExpressionFor(((ArrayBinding) b).leafComponentType, v)); }// ww w . j a v a2s . c om } else { listExpression.addExpression(createExpressionFor(((ArrayBinding) b).leafComponentType, value)); } return listExpression; } else if (b.isEnum()) { ClassExpression classExpression = new ClassExpression(resolver.convertToClassNode(b)); Expression valueExpression = new PropertyExpression(classExpression, new String(((FieldBinding) value).name)); return valueExpression; } else if (CharOperation.equals(b.signature(), jlString)) { String v = ((StringConstant) value).stringValue(); return new ConstantExpression(v); } else if (b.isBaseType()) { char[] sig = b.signature(); if (CharOperation.equals(sig, baseInt)) { return new ConstantExpression(((IntConstant) value).intValue()); } else { throw new GroovyEclipseBug("NYI for signature " + new String(sig)); } } else if (b.isClass()) { ClassExpression classExpression = new ClassExpression(resolver.convertToClassNode((TypeBinding) value)); return classExpression; } throw new GroovyEclipseBug( "Problem in JDTAnnotatioNode.createExpressionFor(binding=" + b + " value=" + value + ")"); }
From source file:org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.java
License:Open Source License
/** * Answer true if the receiver type can be assigned to the argument type (right) *//* ww w . ja v a 2s.co m*/ private boolean isCompatibleWith0(TypeBinding otherType) { if (otherType == this) return true; if (otherType.id == TypeIds.T_JavaLangObject) return true; // equivalence may allow compatibility with array type through wildcard // bound if (isEquivalentTo(otherType)) return true; switch (otherType.kind()) { case Binding.WILDCARD_TYPE: case Binding.INTERSECTION_TYPE: return false; // should have passed equivalence check above if // wildcard case Binding.TYPE_PARAMETER: // check compatibility with capture of ? super X if (otherType.isCapture()) { CaptureBinding otherCapture = (CaptureBinding) otherType; TypeBinding otherLowerBound; if ((otherLowerBound = otherCapture.lowerBound) != null) { if (otherLowerBound.isArrayType()) return false; return isCompatibleWith(otherLowerBound); } } //$FALL-THROUGH$ case Binding.GENERIC_TYPE: case Binding.TYPE: case Binding.PARAMETERIZED_TYPE: case Binding.RAW_TYPE: switch (kind()) { case Binding.GENERIC_TYPE: case Binding.PARAMETERIZED_TYPE: case Binding.RAW_TYPE: if (erasure() == otherType.erasure()) return false; // should have passed equivalence check // above if same erasure } ReferenceBinding otherReferenceType = (ReferenceBinding) otherType; if (otherReferenceType.isInterface()) // could be annotation type return implementsInterface(otherReferenceType, true); if (isInterface()) // Explicit conversion from an interface // to a class is not allowed return false; return otherReferenceType.isSuperclassOf(this); default: return false; } }
From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java
License:Open Source License
/** * Connect type variable supertypes, and returns true if no problem was detected * @param typeParameters//from w w w . j a va2 s.c om * @param checkForErasedCandidateCollisions */ protected boolean connectTypeVariables(TypeParameter[] typeParameters, boolean checkForErasedCandidateCollisions) { /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=305259 - We used to not bother with connecting type variables if source level is < 1.5. This creates problems in the reconciler if a 1.4 project references the generified API of a 1.5 project. The "current" project's source level cannot decide this question for some other project. Now, if we see type parameters at all, we assume that the concerned java element has some legitimate business with them. */ if (typeParameters == null || typeParameters.length == 0) return true; Map invocations = new HashMap(2); boolean noProblems = true; // preinitializing each type variable for (int i = 0, paramLength = typeParameters.length; i < paramLength; i++) { TypeParameter typeParameter = typeParameters[i]; TypeVariableBinding typeVariable = typeParameter.binding; if (typeVariable == null) return false; typeVariable.superclass = getJavaLangObject(); typeVariable.superInterfaces = Binding.NO_SUPERINTERFACES; // set firstBound to the binding of the first explicit bound in parameter declaration typeVariable.firstBound = null; // first bound used to compute erasure } nextVariable: for (int i = 0, paramLength = typeParameters.length; i < paramLength; i++) { TypeParameter typeParameter = typeParameters[i]; TypeVariableBinding typeVariable = typeParameter.binding; TypeReference typeRef = typeParameter.type; if (typeRef == null) continue nextVariable; boolean isFirstBoundTypeVariable = false; TypeBinding superType = this.kind == METHOD_SCOPE ? typeRef.resolveType((BlockScope) this, false/*no bound check*/) : typeRef.resolveType((ClassScope) this); if (superType == null) { typeVariable.tagBits |= TagBits.HierarchyHasProblems; } else { typeRef.resolvedType = superType; // hold onto the problem type firstBound: { switch (superType.kind()) { case Binding.ARRAY_TYPE: problemReporter().boundCannotBeArray(typeRef, superType); typeVariable.tagBits |= TagBits.HierarchyHasProblems; break firstBound; // do not keep first bound case Binding.TYPE_PARAMETER: isFirstBoundTypeVariable = true; TypeVariableBinding varSuperType = (TypeVariableBinding) superType; if (varSuperType.rank >= typeVariable.rank && varSuperType.declaringElement == typeVariable.declaringElement) { if (compilerOptions().complianceLevel <= ClassFileConstants.JDK1_6) { problemReporter().forwardTypeVariableReference(typeParameter, varSuperType); typeVariable.tagBits |= TagBits.HierarchyHasProblems; break firstBound; // do not keep first bound } } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=335751 if (compilerOptions().complianceLevel > ClassFileConstants.JDK1_6) { if (typeVariable.rank >= varSuperType.rank && varSuperType.declaringElement == typeVariable.declaringElement) { SimpleSet set = new SimpleSet(typeParameters.length); set.add(typeVariable); ReferenceBinding superBinding = varSuperType; while (superBinding instanceof TypeVariableBinding) { if (set.includes(superBinding)) { problemReporter().hierarchyCircularity(typeVariable, varSuperType, typeRef); typeVariable.tagBits |= TagBits.HierarchyHasProblems; break firstBound; // do not keep first bound } else { set.add(superBinding); superBinding = ((TypeVariableBinding) superBinding).superclass; } } } } break; default: if (((ReferenceBinding) superType).isFinal()) { problemReporter().finalVariableBound(typeVariable, typeRef); } break; } ReferenceBinding superRefType = (ReferenceBinding) superType; if (!superType.isInterface()) { typeVariable.superclass = superRefType; } else { typeVariable.superInterfaces = new ReferenceBinding[] { superRefType }; } typeVariable.tagBits |= superType.tagBits & TagBits.ContainsNestedTypeReferences; typeVariable.firstBound = superRefType; // first bound used to compute erasure } } TypeReference[] boundRefs = typeParameter.bounds; if (boundRefs != null) { nextBound: for (int j = 0, boundLength = boundRefs.length; j < boundLength; j++) { typeRef = boundRefs[j]; superType = this.kind == METHOD_SCOPE ? typeRef.resolveType((BlockScope) this, false) : typeRef.resolveType((ClassScope) this); if (superType == null) { typeVariable.tagBits |= TagBits.HierarchyHasProblems; continue nextBound; } else { typeVariable.tagBits |= superType.tagBits & TagBits.ContainsNestedTypeReferences; boolean didAlreadyComplain = !typeRef.resolvedType.isValidBinding(); if (isFirstBoundTypeVariable && j == 0) { problemReporter().noAdditionalBoundAfterTypeVariable(typeRef); typeVariable.tagBits |= TagBits.HierarchyHasProblems; didAlreadyComplain = true; //continue nextBound; - keep these bounds to minimize secondary errors } else if (superType.isArrayType()) { if (!didAlreadyComplain) { problemReporter().boundCannotBeArray(typeRef, superType); typeVariable.tagBits |= TagBits.HierarchyHasProblems; } continue nextBound; } else { if (!superType.isInterface()) { if (!didAlreadyComplain) { problemReporter().boundMustBeAnInterface(typeRef, superType); typeVariable.tagBits |= TagBits.HierarchyHasProblems; } continue nextBound; } } // check against superclass if (checkForErasedCandidateCollisions && typeVariable.firstBound == typeVariable.superclass) { if (hasErasedCandidatesCollisions(superType, typeVariable.superclass, invocations, typeVariable, typeRef)) { continue nextBound; } } // check against superinterfaces ReferenceBinding superRefType = (ReferenceBinding) superType; for (int index = typeVariable.superInterfaces.length; --index >= 0;) { ReferenceBinding previousInterface = typeVariable.superInterfaces[index]; if (previousInterface == superRefType) { problemReporter().duplicateBounds(typeRef, superType); typeVariable.tagBits |= TagBits.HierarchyHasProblems; continue nextBound; } if (checkForErasedCandidateCollisions) { if (hasErasedCandidatesCollisions(superType, previousInterface, invocations, typeVariable, typeRef)) { continue nextBound; } } } int size = typeVariable.superInterfaces.length; System.arraycopy(typeVariable.superInterfaces, 0, typeVariable.superInterfaces = new ReferenceBinding[size + 1], 0, size); typeVariable.superInterfaces[size] = superRefType; } } } noProblems &= (typeVariable.tagBits & TagBits.HierarchyHasProblems) == 0; } return noProblems; }
From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java
License:Open Source License
public FieldBinding findField(TypeBinding receiverType, char[] fieldName, InvocationSite invocationSite, boolean needResolve, boolean invisibleFieldsOk) { CompilationUnitScope unitScope = compilationUnitScope(); unitScope.recordTypeReference(receiverType); checkArrayField: {/* w w w . jav a 2 s .c om*/ TypeBinding leafType; switch (receiverType.kind()) { case Binding.BASE_TYPE: return null; case Binding.WILDCARD_TYPE: case Binding.INTERSECTION_TYPE: case Binding.TYPE_PARAMETER: // capture TypeBinding receiverErasure = receiverType.erasure(); if (!receiverErasure.isArrayType()) break checkArrayField; leafType = receiverErasure.leafComponentType(); break; case Binding.ARRAY_TYPE: leafType = receiverType.leafComponentType(); break; default: break checkArrayField; } if (leafType instanceof ReferenceBinding) if (!((ReferenceBinding) leafType).canBeSeenBy(this)) return new ProblemFieldBinding((ReferenceBinding) leafType, fieldName, ProblemReasons.ReceiverTypeNotVisible); if (CharOperation.equals(fieldName, TypeConstants.LENGTH)) { if ((leafType.tagBits & TagBits.HasMissingType) != 0) { return new ProblemFieldBinding(ArrayBinding.ArrayLength, null, fieldName, ProblemReasons.NotFound); } return ArrayBinding.ArrayLength; } return null; } ReferenceBinding currentType = (ReferenceBinding) receiverType; if (!currentType.canBeSeenBy(this)) return new ProblemFieldBinding(currentType, fieldName, ProblemReasons.ReceiverTypeNotVisible); currentType.initializeForStaticImports(); FieldBinding field = currentType.getField(fieldName, needResolve); // https://bugs.eclipse.org/bugs/show_bug.cgi?id=316456 boolean insideTypeAnnotations = this instanceof MethodScope && ((MethodScope) this).insideTypeAnnotation; if (field != null) { if (invisibleFieldsOk) { return field; } if (invocationSite == null || insideTypeAnnotations ? field.canBeSeenBy(getCurrentPackage()) : field.canBeSeenBy(currentType, invocationSite, this)) return field; return new ProblemFieldBinding(field /* closest match*/, field.declaringClass, fieldName, ProblemReasons.NotVisible); } // collect all superinterfaces of receiverType until the field is found in a supertype ReferenceBinding[] interfacesToVisit = null; int nextPosition = 0; FieldBinding visibleField = null; boolean keepLooking = true; FieldBinding notVisibleField = null; // we could hold onto the not visible field for extra error reporting while (keepLooking) { ReferenceBinding[] itsInterfaces = currentType.superInterfaces(); if (itsInterfaces != null && itsInterfaces != Binding.NO_SUPERINTERFACES) { if (interfacesToVisit == null) { interfacesToVisit = itsInterfaces; nextPosition = interfacesToVisit.length; } else { int itsLength = itsInterfaces.length; if (nextPosition + itsLength >= interfacesToVisit.length) System.arraycopy(interfacesToVisit, 0, interfacesToVisit = new ReferenceBinding[nextPosition + itsLength + 5], 0, nextPosition); nextInterface: for (int a = 0; a < itsLength; a++) { ReferenceBinding next = itsInterfaces[a]; for (int b = 0; b < nextPosition; b++) if (next == interfacesToVisit[b]) continue nextInterface; interfacesToVisit[nextPosition++] = next; } } } if ((currentType = currentType.superclass()) == null) break; unitScope.recordTypeReference(currentType); currentType.initializeForStaticImports(); currentType = (ReferenceBinding) currentType.capture(this, invocationSite == null ? 0 : invocationSite.sourceEnd()); if ((field = currentType.getField(fieldName, needResolve)) != null) { if (invisibleFieldsOk) { return field; } keepLooking = false; if (field.canBeSeenBy(receiverType, invocationSite, this)) { if (visibleField == null) visibleField = field; else return new ProblemFieldBinding(visibleField /* closest match*/, visibleField.declaringClass, fieldName, ProblemReasons.Ambiguous); } else { if (notVisibleField == null) notVisibleField = field; } } } // walk all visible interfaces to find ambiguous references if (interfacesToVisit != null) { ProblemFieldBinding ambiguous = null; done: for (int i = 0; i < nextPosition; i++) { ReferenceBinding anInterface = interfacesToVisit[i]; unitScope.recordTypeReference(anInterface); // no need to capture rcv interface, since member field is going to be static anyway if ((field = anInterface.getField(fieldName, true /*resolve*/)) != null) { if (invisibleFieldsOk) { return field; } if (visibleField == null) { visibleField = field; } else { ambiguous = new ProblemFieldBinding(visibleField /* closest match*/, visibleField.declaringClass, fieldName, ProblemReasons.Ambiguous); break done; } } else { ReferenceBinding[] itsInterfaces = anInterface.superInterfaces(); if (itsInterfaces != null && itsInterfaces != Binding.NO_SUPERINTERFACES) { int itsLength = itsInterfaces.length; if (nextPosition + itsLength >= interfacesToVisit.length) System.arraycopy(interfacesToVisit, 0, interfacesToVisit = new ReferenceBinding[nextPosition + itsLength + 5], 0, nextPosition); nextInterface: for (int a = 0; a < itsLength; a++) { ReferenceBinding next = itsInterfaces[a]; for (int b = 0; b < nextPosition; b++) if (next == interfacesToVisit[b]) continue nextInterface; interfacesToVisit[nextPosition++] = next; } } } } if (ambiguous != null) return ambiguous; } if (visibleField != null) return visibleField; if (notVisibleField != null) { return new ProblemFieldBinding(notVisibleField, currentType, fieldName, ProblemReasons.NotVisible); } return null; }
From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java
License:Open Source License
/** * Returns the most specific set of types compatible with all given types. * (i.e. most specific common super types) * If no types is given, will return an empty array. If not compatible * reference type is found, returns null. In other cases, will return an array * of minimal erased types, where some nulls may appear (and must simply be * ignored).//ww w .j a va2 s . co m */ protected TypeBinding[] minimalErasedCandidates(TypeBinding[] types, Map allInvocations) { int length = types.length; int indexOfFirst = -1, actualLength = 0; for (int i = 0; i < length; i++) { TypeBinding type = types[i]; if (type == null) continue; if (type.isBaseType()) return null; if (indexOfFirst < 0) indexOfFirst = i; actualLength++; } switch (actualLength) { case 0: return Binding.NO_TYPES; case 1: return types; } TypeBinding firstType = types[indexOfFirst]; if (firstType.isBaseType()) return null; // record all supertypes of type // intersect with all supertypes of otherType ArrayList typesToVisit = new ArrayList(5); int dim = firstType.dimensions(); TypeBinding leafType = firstType.leafComponentType(); // do not allow type variables/intersection types to match with erasures for free TypeBinding firstErasure; switch (leafType.kind()) { case Binding.PARAMETERIZED_TYPE: case Binding.RAW_TYPE: case Binding.ARRAY_TYPE: firstErasure = firstType.erasure(); break; default: firstErasure = firstType; break; } if (firstErasure != firstType) { allInvocations.put(firstErasure, firstType); } typesToVisit.add(firstType); int max = 1; ReferenceBinding currentType; for (int i = 0; i < max; i++) { TypeBinding typeToVisit = (TypeBinding) typesToVisit.get(i); dim = typeToVisit.dimensions(); if (dim > 0) { leafType = typeToVisit.leafComponentType(); switch (leafType.id) { case TypeIds.T_JavaLangObject: if (dim > 1) { // Object[][] supertype is Object[] TypeBinding elementType = ((ArrayBinding) typeToVisit).elementsType(); if (!typesToVisit.contains(elementType)) { typesToVisit.add(elementType); max++; } continue; } //$FALL-THROUGH$ case TypeIds.T_byte: case TypeIds.T_short: case TypeIds.T_char: case TypeIds.T_boolean: case TypeIds.T_int: case TypeIds.T_long: case TypeIds.T_float: case TypeIds.T_double: TypeBinding superType = getJavaIoSerializable(); if (!typesToVisit.contains(superType)) { typesToVisit.add(superType); max++; } superType = getJavaLangCloneable(); if (!typesToVisit.contains(superType)) { typesToVisit.add(superType); max++; } superType = getJavaLangObject(); if (!typesToVisit.contains(superType)) { typesToVisit.add(superType); max++; } continue; default: } typeToVisit = leafType; } currentType = (ReferenceBinding) typeToVisit; if (currentType.isCapture()) { TypeBinding firstBound = ((CaptureBinding) currentType).firstBound; if (firstBound != null && firstBound.isArrayType()) { TypeBinding superType = dim == 0 ? firstBound : (TypeBinding) environment().createArrayType(firstBound, dim); // recreate array if needed if (!typesToVisit.contains(superType)) { typesToVisit.add(superType); max++; TypeBinding superTypeErasure = (firstBound.isTypeVariable() || firstBound.isWildcard() /*&& !itsInterface.isCapture()*/) ? superType : superType.erasure(); if (superTypeErasure != superType) { allInvocations.put(superTypeErasure, superType); } } continue; } } // inject super interfaces prior to superclass ReferenceBinding[] itsInterfaces = currentType.superInterfaces(); if (itsInterfaces != null) { // can be null during code assist operations that use LookupEnvironment.completeTypeBindings(parsedUnit, buildFieldsAndMethods) for (int j = 0, count = itsInterfaces.length; j < count; j++) { TypeBinding itsInterface = itsInterfaces[j]; TypeBinding superType = dim == 0 ? itsInterface : (TypeBinding) environment().createArrayType(itsInterface, dim); // recreate array if needed if (!typesToVisit.contains(superType)) { typesToVisit.add(superType); max++; TypeBinding superTypeErasure = (itsInterface.isTypeVariable() || itsInterface.isWildcard() /*&& !itsInterface.isCapture()*/) ? superType : superType.erasure(); if (superTypeErasure != superType) { allInvocations.put(superTypeErasure, superType); } } } } TypeBinding itsSuperclass = currentType.superclass(); if (itsSuperclass != null) { TypeBinding superType = dim == 0 ? itsSuperclass : (TypeBinding) environment().createArrayType(itsSuperclass, dim); // recreate array if needed if (!typesToVisit.contains(superType)) { typesToVisit.add(superType); max++; TypeBinding superTypeErasure = (itsSuperclass.isTypeVariable() || itsSuperclass.isWildcard() /*&& !itsSuperclass.isCapture()*/) ? superType : superType.erasure(); if (superTypeErasure != superType) { allInvocations.put(superTypeErasure, superType); } } } } int superLength = typesToVisit.size(); TypeBinding[] erasedSuperTypes = new TypeBinding[superLength]; int rank = 0; for (Iterator iter = typesToVisit.iterator(); iter.hasNext();) { TypeBinding type = (TypeBinding) iter.next(); leafType = type.leafComponentType(); erasedSuperTypes[rank++] = (leafType.isTypeVariable() || leafType.isWildcard() /*&& !leafType.isCapture()*/) ? type : type.erasure(); } // intersecting first type supertypes with other types' ones, nullifying non matching supertypes int remaining = superLength; nextOtherType: for (int i = indexOfFirst + 1; i < length; i++) { TypeBinding otherType = types[i]; if (otherType == null) continue nextOtherType; if (otherType.isArrayType()) { nextSuperType: for (int j = 0; j < superLength; j++) { TypeBinding erasedSuperType = erasedSuperTypes[j]; if (erasedSuperType == null || erasedSuperType == otherType) continue nextSuperType; TypeBinding match; if ((match = otherType.findSuperTypeOriginatingFrom(erasedSuperType)) == null) { erasedSuperTypes[j] = null; if (--remaining == 0) return null; continue nextSuperType; } // record invocation Object invocationData = allInvocations.get(erasedSuperType); if (invocationData == null) { allInvocations.put(erasedSuperType, match); // no array for singleton } else if (invocationData instanceof TypeBinding) { if (match != invocationData) { // using an array to record invocations in order (188103) TypeBinding[] someInvocations = { (TypeBinding) invocationData, match, }; allInvocations.put(erasedSuperType, someInvocations); } } else { // using an array to record invocations in order (188103) TypeBinding[] someInvocations = (TypeBinding[]) invocationData; checkExisting: { int invocLength = someInvocations.length; for (int k = 0; k < invocLength; k++) { if (someInvocations[k] == match) break checkExisting; } System.arraycopy(someInvocations, 0, someInvocations = new TypeBinding[invocLength + 1], 0, invocLength); allInvocations.put(erasedSuperType, someInvocations); someInvocations[invocLength] = match; } } } continue nextOtherType; } nextSuperType: for (int j = 0; j < superLength; j++) { TypeBinding erasedSuperType = erasedSuperTypes[j]; if (erasedSuperType == null) continue nextSuperType; TypeBinding match; if (erasedSuperType == otherType || erasedSuperType.id == TypeIds.T_JavaLangObject && otherType.isInterface()) { match = erasedSuperType; } else { if (erasedSuperType.isArrayType()) { match = null; } else { match = otherType.findSuperTypeOriginatingFrom(erasedSuperType); } if (match == null) { // incompatible super type erasedSuperTypes[j] = null; if (--remaining == 0) return null; continue nextSuperType; } } // record invocation Object invocationData = allInvocations.get(erasedSuperType); if (invocationData == null) { allInvocations.put(erasedSuperType, match); // no array for singleton } else if (invocationData instanceof TypeBinding) { if (match != invocationData) { // using an array to record invocations in order (188103) TypeBinding[] someInvocations = { (TypeBinding) invocationData, match, }; allInvocations.put(erasedSuperType, someInvocations); } } else { // using an array to record invocations in order (188103) TypeBinding[] someInvocations = (TypeBinding[]) invocationData; checkExisting: { int invocLength = someInvocations.length; for (int k = 0; k < invocLength; k++) { if (someInvocations[k] == match) break checkExisting; } System.arraycopy(someInvocations, 0, someInvocations = new TypeBinding[invocLength + 1], 0, invocLength); allInvocations.put(erasedSuperType, someInvocations); someInvocations[invocLength] = match; } } } } // eliminate non minimal super types if (remaining > 1) { nextType: for (int i = 0; i < superLength; i++) { TypeBinding erasedSuperType = erasedSuperTypes[i]; if (erasedSuperType == null) continue nextType; nextOtherType: for (int j = 0; j < superLength; j++) { if (i == j) continue nextOtherType; TypeBinding otherType = erasedSuperTypes[j]; if (otherType == null) continue nextOtherType; if (erasedSuperType instanceof ReferenceBinding) { if (otherType.id == TypeIds.T_JavaLangObject && erasedSuperType.isInterface()) continue nextOtherType; // keep Object for an interface if (erasedSuperType.findSuperTypeOriginatingFrom(otherType) != null) { erasedSuperTypes[j] = null; // discard non minimal supertype remaining--; } } else if (erasedSuperType.isArrayType()) { if (otherType.isArrayType() // keep Object[...] for an interface array (same dimensions) && otherType.leafComponentType().id == TypeIds.T_JavaLangObject && otherType.dimensions() == erasedSuperType.dimensions() && erasedSuperType.leafComponentType().isInterface()) continue nextOtherType; if (erasedSuperType.findSuperTypeOriginatingFrom(otherType) != null) { erasedSuperTypes[j] = null; // discard non minimal supertype remaining--; } } } } } return erasedSuperTypes; }
From source file:org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.java
License:Open Source License
public FieldBinding resolveTypeFor(FieldBinding field) { if ((field.modifiers & ExtraCompilerModifiers.AccUnresolved) == 0) return field; if (this.scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5) { if ((field.getAnnotationTagBits() & TagBits.AnnotationDeprecated) != 0) field.modifiers |= ClassFileConstants.AccDeprecated; }//from ww w . jav a 2s .co m if (isViewedAsDeprecated() && !field.isDeprecated()) field.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly; if (hasRestrictedAccess()) field.modifiers |= ExtraCompilerModifiers.AccRestrictedAccess; FieldDeclaration[] fieldDecls = this.scope.referenceContext.fields; int length = fieldDecls == null ? 0 : fieldDecls.length; for (int f = 0; f < length; f++) { if (fieldDecls[f].binding != field) continue; MethodScope initializationScope = field.isStatic() ? this.scope.referenceContext.staticInitializerScope : this.scope.referenceContext.initializerScope; FieldBinding previousField = initializationScope.initializedField; try { initializationScope.initializedField = field; FieldDeclaration fieldDecl = fieldDecls[f]; TypeBinding fieldType = fieldDecl.getKind() == AbstractVariableDeclaration.ENUM_CONSTANT ? initializationScope.environment().convertToRawType(this, false /*do not force conversion of enclosing types*/) // enum constant is implicitly of declaring enum type : fieldDecl.type.resolveType(initializationScope, true /* check bounds*/); field.type = fieldType; field.modifiers &= ~ExtraCompilerModifiers.AccUnresolved; if (fieldType == null) { fieldDecl.binding = null; return null; } if (fieldType == TypeBinding.VOID) { this.scope.problemReporter().variableTypeCannotBeVoid(fieldDecl); fieldDecl.binding = null; return null; } if (fieldType.isArrayType() && ((ArrayBinding) fieldType).leafComponentType == TypeBinding.VOID) { this.scope.problemReporter().variableTypeCannotBeVoidArray(fieldDecl); fieldDecl.binding = null; return null; } if ((fieldType.tagBits & TagBits.HasMissingType) != 0) { field.tagBits |= TagBits.HasMissingType; } TypeBinding leafType = fieldType.leafComponentType(); if (leafType instanceof ReferenceBinding && (((ReferenceBinding) leafType).modifiers & ExtraCompilerModifiers.AccGenericSignature) != 0) { field.modifiers |= ExtraCompilerModifiers.AccGenericSignature; } } finally { initializationScope.initializedField = previousField; } return field; } return null; // should never reach this point }
From source file:org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.java
License:Open Source License
public MethodBinding resolveTypesFor(MethodBinding method) { if ((method.modifiers & ExtraCompilerModifiers.AccUnresolved) == 0) return method; if (this.scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_5) { if ((method.getAnnotationTagBits() & TagBits.AnnotationDeprecated) != 0) method.modifiers |= ClassFileConstants.AccDeprecated; }/* www . j a v a 2s . c o m*/ if (isViewedAsDeprecated() && !method.isDeprecated()) method.modifiers |= ExtraCompilerModifiers.AccDeprecatedImplicitly; if (hasRestrictedAccess()) method.modifiers |= ExtraCompilerModifiers.AccRestrictedAccess; AbstractMethodDeclaration methodDecl = method.sourceMethod(); // GROOVY /* old { if (methodDecl == null) return null; // method could not be resolved in previous iteration } new*/ if (methodDecl == null) { if (method instanceof LazilyResolvedMethodBinding) { LazilyResolvedMethodBinding lrMethod = (LazilyResolvedMethodBinding) method; // the rest is a copy of the code below but doesn't depend on the method declaration // nothing to do for method type parameters (there are none) // nothing to do for method exceptions (there are none) TypeBinding ptb = lrMethod.getParameterTypeBinding(); if (ptb == null) { method.parameters = Binding.NO_PARAMETERS; } else { method.parameters = new TypeBinding[] { ptb }; } method.returnType = lrMethod.getReturnTypeBinding(); method.modifiers &= ~ExtraCompilerModifiers.AccUnresolved; return method; } // returning null is what this clause would have done anyway return null; } // FIXASC - end TypeParameter[] typeParameters = methodDecl.typeParameters(); if (typeParameters != null) { methodDecl.scope.connectTypeVariables(typeParameters, true); // Perform deferred bound checks for type variables (only done after type variable hierarchy is connected) for (int i = 0, paramLength = typeParameters.length; i < paramLength; i++) typeParameters[i].checkBounds(methodDecl.scope); } TypeReference[] exceptionTypes = methodDecl.thrownExceptions; if (exceptionTypes != null) { int size = exceptionTypes.length; method.thrownExceptions = new ReferenceBinding[size]; int count = 0; ReferenceBinding resolvedExceptionType; for (int i = 0; i < size; i++) { resolvedExceptionType = (ReferenceBinding) exceptionTypes[i].resolveType(methodDecl.scope, true /* check bounds*/); if (resolvedExceptionType == null) continue; if (resolvedExceptionType.isBoundParameterizedType()) { methodDecl.scope.problemReporter().invalidParameterizedExceptionType(resolvedExceptionType, exceptionTypes[i]); continue; } if (resolvedExceptionType.findSuperTypeOriginatingFrom(TypeIds.T_JavaLangThrowable, true) == null) { if (resolvedExceptionType.isValidBinding()) { methodDecl.scope.problemReporter().cannotThrowType(exceptionTypes[i], resolvedExceptionType); continue; } } if ((resolvedExceptionType.tagBits & TagBits.HasMissingType) != 0) { method.tagBits |= TagBits.HasMissingType; } method.modifiers |= (resolvedExceptionType.modifiers & ExtraCompilerModifiers.AccGenericSignature); method.thrownExceptions[count++] = resolvedExceptionType; } if (count < size) System.arraycopy(method.thrownExceptions, 0, method.thrownExceptions = new ReferenceBinding[count], 0, count); } final boolean reportUnavoidableGenericTypeProblems = this.scope .compilerOptions().reportUnavoidableGenericTypeProblems; boolean foundArgProblem = false; Argument[] arguments = methodDecl.arguments; if (arguments != null) { int size = arguments.length; method.parameters = Binding.NO_PARAMETERS; TypeBinding[] newParameters = new TypeBinding[size]; for (int i = 0; i < size; i++) { Argument arg = arguments[i]; if (arg.annotations != null) { method.tagBits |= TagBits.HasParameterAnnotations; } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=322817 boolean deferRawTypeCheck = !reportUnavoidableGenericTypeProblems && !method.isConstructor() && (arg.type.bits & ASTNode.IgnoreRawTypeCheck) == 0; TypeBinding parameterType; if (deferRawTypeCheck) { arg.type.bits |= ASTNode.IgnoreRawTypeCheck; } try { parameterType = arg.type.resolveType(methodDecl.scope, true /* check bounds*/); } finally { if (deferRawTypeCheck) { arg.type.bits &= ~ASTNode.IgnoreRawTypeCheck; } } if (parameterType == null) { foundArgProblem = true; } else if (parameterType == TypeBinding.VOID) { methodDecl.scope.problemReporter().argumentTypeCannotBeVoid(this, methodDecl, arg); foundArgProblem = true; } else { if ((parameterType.tagBits & TagBits.HasMissingType) != 0) { method.tagBits |= TagBits.HasMissingType; } TypeBinding leafType = parameterType.leafComponentType(); if (leafType instanceof ReferenceBinding && (((ReferenceBinding) leafType).modifiers & ExtraCompilerModifiers.AccGenericSignature) != 0) method.modifiers |= ExtraCompilerModifiers.AccGenericSignature; newParameters[i] = parameterType; arg.binding = new LocalVariableBinding(arg, parameterType, arg.modifiers, true); } } // only assign parameters if no problems are found if (!foundArgProblem) { method.parameters = newParameters; } } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=337799 if (this.scope.compilerOptions().sourceLevel >= ClassFileConstants.JDK1_7) { if ((method.tagBits & TagBits.AnnotationSafeVarargs) != 0) { if (!method.isVarargs()) { methodDecl.scope.problemReporter().safeVarargsOnFixedArityMethod(method); } else if (!method.isStatic() && !method.isFinal() && !method.isConstructor()) { methodDecl.scope.problemReporter().safeVarargsOnNonFinalInstanceMethod(method); } } else if (method.parameters != null && method.parameters.length > 0 && method.isVarargs()) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=337795 if (!method.parameters[method.parameters.length - 1].isReifiable()) { methodDecl.scope.problemReporter() .possibleHeapPollutionFromVararg(methodDecl.arguments[methodDecl.arguments.length - 1]); } } } boolean foundReturnTypeProblem = false; if (!method.isConstructor()) { TypeReference returnType = methodDecl instanceof MethodDeclaration ? ((MethodDeclaration) methodDecl).returnType : null; if (returnType == null) { methodDecl.scope.problemReporter().missingReturnType(methodDecl); method.returnType = null; foundReturnTypeProblem = true; } else { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=322817 boolean deferRawTypeCheck = !reportUnavoidableGenericTypeProblems && (returnType.bits & ASTNode.IgnoreRawTypeCheck) == 0; TypeBinding methodType; if (deferRawTypeCheck) { returnType.bits |= ASTNode.IgnoreRawTypeCheck; } try { methodType = returnType.resolveType(methodDecl.scope, true /* check bounds*/); } finally { if (deferRawTypeCheck) { returnType.bits &= ~ASTNode.IgnoreRawTypeCheck; } } if (methodType == null) { foundReturnTypeProblem = true; } else if (methodType.isArrayType() && ((ArrayBinding) methodType).leafComponentType == TypeBinding.VOID) { methodDecl.scope.problemReporter().returnTypeCannotBeVoidArray((MethodDeclaration) methodDecl); foundReturnTypeProblem = true; } else { if ((methodType.tagBits & TagBits.HasMissingType) != 0) { method.tagBits |= TagBits.HasMissingType; } method.returnType = methodType; TypeBinding leafType = methodType.leafComponentType(); if (leafType instanceof ReferenceBinding && (((ReferenceBinding) leafType).modifiers & ExtraCompilerModifiers.AccGenericSignature) != 0) method.modifiers |= ExtraCompilerModifiers.AccGenericSignature; } } } if (foundArgProblem) { methodDecl.binding = null; method.parameters = Binding.NO_PARAMETERS; // see 107004 // nullify type parameter bindings as well as they have a backpointer to the method binding // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=81134) if (typeParameters != null) for (int i = 0, length = typeParameters.length; i < length; i++) typeParameters[i].binding = null; return null; } if (foundReturnTypeProblem) return method; // but its still unresolved with a null return type & is still connected to its method declaration method.modifiers &= ~ExtraCompilerModifiers.AccUnresolved; return method; }