public ArrayType createArrayType(TType elementType, int dimensions) { Assert.isTrue(!elementType.isArrayType()); Assert.isTrue(!elementType.isAnonymous()); Assert.isTrue(dimensions > 0); int index = dimensions - 1; Map<TType, ArrayType> arrayTypes = getArrayTypesMap(index); ArrayType result = arrayTypes.get(elementType); if (result != null) return result; result = new ArrayType( this, BindingKey.createArrayTypeBindingKey(elementType.getBindingKey(), dimensions)); arrayTypes.put(elementType, result); result.initialize(elementType, dimensions); return result; }
protected String getFullyQualifiedParameterizedName(String fullyQualifiedName, String uniqueKey) throws JavaModelException { String[] typeArguments = new BindingKey(uniqueKey).getTypeArguments(); int length = typeArguments.length; if (length == 0) return fullyQualifiedName; StringBuffer buffer = new StringBuffer(); buffer.append(fullyQualifiedName); buffer.append('<'); for (int i = 0; i < length; i++) { String typeArgument = typeArguments[i]; buffer.append(Signature.toString(typeArgument)); if (i < length-1) buffer.append(','); } buffer.append('>'); return buffer.toString(); }
/** * Appends the style label for a field. Considers the F_* flags. * * @param field the element to render * @param flags the rendering flags. Flags with names starting with 'F_' are considered. */ public void appendFieldLabel(IField field, long flags) { try { if (getFlag(flags, JavaElementLabels.F_PRE_TYPE_SIGNATURE) && field.exists() && !Flags.isEnum(field.getFlags())) { if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && field.isResolved()) { appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags); } else { appendTypeSignatureLabel(field, field.getTypeSignature(), flags); } fBuilder.append(' '); } // qualification if (getFlag(flags, JavaElementLabels.F_FULLY_QUALIFIED)) { appendTypeLabel(field.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); fBuilder.append('.'); } fBuilder.append(getElementName(field)); if (getFlag(flags, JavaElementLabels.F_APP_TYPE_SIGNATURE) && field.exists() && !Flags.isEnum(field.getFlags())) { fBuilder.append(JavaElementLabels.DECL_STRING); if (getFlag(flags, JavaElementLabels.USE_RESOLVED) && field.isResolved()) { appendTypeSignatureLabel(field, new BindingKey(field.getKey()).toSignature(), flags); } else { appendTypeSignatureLabel(field, field.getTypeSignature(), flags); } } // post qualification if (getFlag(flags, JavaElementLabels.F_POST_QUALIFIED)) { fBuilder.append(JavaElementLabels.CONCAT_STRING); appendTypeLabel(field.getDeclaringType(), JavaElementLabels.T_FULLY_QUALIFIED | (flags & QUALIFIER_FLAGS)); } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("", e); // NotExistsException will not reach this point } }
public ArrayType createArrayType(TType elementType, int dimensions) { Assert.isTrue(! elementType.isArrayType()); Assert.isTrue(! elementType.isAnonymous()); Assert.isTrue(dimensions > 0); int index= dimensions - 1; Map<TType, ArrayType> arrayTypes= getArrayTypesMap(index); ArrayType result= arrayTypes.get(elementType); if (result != null) return result; result= new ArrayType(this, BindingKey.createArrayTypeBindingKey(elementType.getBindingKey(), dimensions)); arrayTypes.put(elementType, result); result.initialize(elementType, dimensions); return result; }
/** * Translate a typeBinding with a formal type parameter into one with fully substituted type * based on the enclosing type * @param typeBinding: e.g., virtual field of type <E extends java.lang.Object> * @param actualBinding: the enclosing type, e.g., class ArrayList<courses.Course> * where the generic type declaration is class ArrayList<E> {...} * @return typeBinding with the type substitution performed, e.g., courses.Course */ public static ITypeBinding translateFtoA(ITypeBinding typeBinding, ITypeBinding actualBinding) { ITypeBinding toTypeBindingActual = typeBinding; // HACK: Introduced bugs! // Check that it is a generic type, to avoid generating a type that does not exist! // if(!typeBinding.isGenericType()) { // return typeBinding; // } // HACK: Introduced bugs! if (typeBinding.isEqualTo(actualBinding)) { return typeBinding; } // Instantiate generic types... if (actualBinding.isParameterizedType()) { ITypeBinding[] typeParameters = actualBinding.getErasure().getTypeParameters(); int pIndex = -1; int index = 0; for(ITypeBinding typeParameter : typeParameters ) { if ( typeParameter.isEqualTo(typeBinding) ) { pIndex = index; break; } index++; } ITypeBinding[] typeArguments = actualBinding.getTypeArguments(); if ( typeBinding.isTypeVariable() && typeArguments != null && pIndex != -1 && pIndex < typeArguments.length) { toTypeBindingActual = typeArguments[pIndex]; } else { String[] typeArgumentString = getParameters(typeArguments); String bindingKey = BindingKey.createParameterizedTypeBindingKey(typeBinding.getKey(), typeArgumentString); toTypeBindingActual = GenHelper.mapBindingKeyToTypeBinding(bindingKey); } } return toTypeBindingActual; }
private static ITypeBinding getCorrespondingArrayType(ITypeBinding elementType) { String bindingKey = BindingKey.createArrayTypeBindingKey(elementType.getKey(), 1); ITypeBinding typeBinding = GenHelper.mapBindingKeyToTypeBinding(bindingKey); return typeBinding; }
public Variable createVariable( IJavaElement element, IType enclosingType, String expectedType, int positionScore) throws JavaModelException { int variableType; int elementType = element.getElementType(); String elementName = element.getElementName(); String typeSignature; switch (elementType) { case IJavaElement.FIELD: { IField field = (IField) element; if (field.getDeclaringType().equals(enclosingType)) { variableType = Variable.FIELD; } else { variableType = Variable.INHERITED_FIELD; } if (field.isResolved()) { typeSignature = new BindingKey(field.getKey()).toSignature(); } else { typeSignature = field.getTypeSignature(); } break; } case IJavaElement.LOCAL_VARIABLE: { ILocalVariable locVar = (ILocalVariable) element; variableType = Variable.LOCAL; typeSignature = locVar.getTypeSignature(); break; } case IJavaElement.METHOD: { IMethod method = (IMethod) element; if (isMethodToSuggest(method)) { if (method.getDeclaringType().equals(enclosingType)) { variableType = Variable.METHOD; } else { variableType = Variable.INHERITED_METHOD; } if (method.isResolved()) { typeSignature = Signature.getReturnType(new BindingKey(method.getKey()).toSignature()); } else { typeSignature = method.getReturnType(); } elementName = elementName + "()"; // $NON-NLS-1$ } else { return null; } break; } default: return null; } String type = Signature.toString(typeSignature); boolean isAutoboxMatch = isPrimitiveType(expectedType) != isPrimitiveType(type); return new Variable( type, elementName, variableType, isAutoboxMatch, positionScore, NO_TRIGGERS, getImageDescriptor(element)); }
/** * @param baseType the base type * @param typeArgumentCvs type argument constraint variables * @param rewrite the cu rewrite * @param tCModel the type constraints model * @param leaveUnconstraindRaw <code>true</code> to keep unconstrained type references raw, * <code>false</code> to infer <code><?></code> if possible * @return the new type arguments, or <code>null</code> iff an argument could not be inferred */ private static Type[] getTypeArguments(Type baseType, ArrayList<CollectionElementVariable2> typeArgumentCvs, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel, boolean leaveUnconstraindRaw) { if (typeArgumentCvs.size() == 0) return null; Type[] typeArguments= new Type[typeArgumentCvs.size()]; for (int i= 0; i < typeArgumentCvs.size(); i++) { CollectionElementVariable2 elementCv= typeArgumentCvs.get(i); Type typeArgument; TType chosenType= InferTypeArgumentsConstraintsSolver.getChosenType(elementCv); if (chosenType != null) { if (chosenType.isWildcardType() && ! unboundedWildcardAllowed(baseType)) return null; // can't e.g. write "new ArrayList<?>()". if (chosenType.isParameterizedType()) // workaround for bug 99124 chosenType= chosenType.getTypeDeclaration(); BindingKey bindingKey= new BindingKey(chosenType.getBindingKey()); typeArgument= rewrite.getImportRewrite().addImportFromSignature(bindingKey.toSignature(), rewrite.getAST()); ArrayList<CollectionElementVariable2> nestedTypeArgumentCvs= getTypeArgumentCvs(elementCv, tCModel); Type[] nestedTypeArguments= getTypeArguments(typeArgument, nestedTypeArgumentCvs, rewrite, tCModel, leaveUnconstraindRaw); //recursion if (nestedTypeArguments != null) { ParameterizedType parameterizedType= rewrite.getAST().newParameterizedType(typeArgument); for (int j= 0; j < nestedTypeArguments.length; j++) parameterizedType.typeArguments().add(nestedTypeArguments[j]); typeArgument= parameterizedType; } } else { // couldn't infer an element type (no constraints) if (leaveUnconstraindRaw) { // every guess could be wrong => leave the whole thing raw return null; } else { if (unboundedWildcardAllowed(baseType)) { typeArgument= rewrite.getAST().newWildcardType(); } else { String object= rewrite.getImportRewrite().addImport("java.lang.Object"); //$NON-NLS-1$ typeArgument= (Type) rewrite.getASTRewrite().createStringPlaceholder(object, ASTNode.SIMPLE_TYPE); } } // ASTNode baseTypeParent= baseType.getParent(); // if (baseTypeParent instanceof ClassInstanceCreation) { // //No ? allowed. Take java.lang.Object. // typeArgument= rewrite.getAST().newSimpleType(rewrite.getAST().newName(rewrite.getImportRewrite().addImport("java.lang.Object"))); //$NON-NLS-1$ // } else if (baseTypeParent instanceof ArrayCreation || baseTypeParent instanceof InstanceofExpression) { // //Only ? allowed. // typeArgument= rewrite.getAST().newWildcardType(); // } else { // //E.g. field type: can put anything. Choosing ? in order to be most constraining. // typeArgument= rewrite.getAST().newWildcardType(); // } } typeArguments[i]= typeArgument; } return typeArguments; }
public Variable createVariable(IJavaElement element, IType enclosingType, String expectedType, int positionScore) throws JavaModelException { int variableType; int elementType= element.getElementType(); String elementName= element.getElementName(); String typeSignature; switch (elementType) { case IJavaElement.FIELD: { IField field= (IField) element; if (field.getDeclaringType().equals(enclosingType)) { variableType= Variable.FIELD; } else { variableType= Variable.INHERITED_FIELD; } if (field.isResolved()) { typeSignature= new BindingKey(field.getKey()).toSignature(); } else { typeSignature= field.getTypeSignature(); } break; } case IJavaElement.LOCAL_VARIABLE: { ILocalVariable locVar= (ILocalVariable) element; variableType= Variable.LOCAL; typeSignature= locVar.getTypeSignature(); break; } case IJavaElement.METHOD: { IMethod method= (IMethod) element; if (isMethodToSuggest(method)) { if (method.getDeclaringType().equals(enclosingType)) { variableType= Variable.METHOD; } else { variableType= Variable.INHERITED_METHOD; } if (method.isResolved()) { typeSignature= Signature.getReturnType(new BindingKey(method.getKey()).toSignature()); } else { typeSignature= method.getReturnType(); } elementName= elementName + "()"; //$NON-NLS-1$ } else { return null; } break; } default: return null; } String type= Signature.toString(typeSignature); boolean isAutoboxMatch= isPrimitiveType(expectedType) != isPrimitiveType(type); return new Variable(type, elementName, variableType, isAutoboxMatch, positionScore, NO_TRIGGERS, getImageDescriptor(element)); }
public ConstructorPattern( char[] declaringSimpleName, char[] declaringQualification, char[][] parameterQualifications, char[][] parameterSimpleNames, String[] parameterSignatures, IMethod method, int limitTo, int matchRule) { this(declaringSimpleName, declaringQualification, parameterQualifications, parameterSimpleNames, limitTo, matchRule); // Set flags try { this.varargs = (method.getFlags() & Flags.AccVarargs) != 0; } catch (JavaModelException e) { // do nothing } // Get unique key for parameterized constructors String genericDeclaringTypeSignature = null; if (method.isResolved()) { String key = method.getKey(); BindingKey bindingKey = new BindingKey(key); if (bindingKey.isParameterizedType()) { genericDeclaringTypeSignature = Util.getDeclaringTypeSignature(key); // Store type signature and arguments for declaring type if (genericDeclaringTypeSignature != null) { this.typeSignatures = Util.splitTypeLevelsSignature(genericDeclaringTypeSignature); setTypeArguments(Util.getAllTypeArguments(this.typeSignatures)); } } } else { this.constructorParameters = true; storeTypeSignaturesAndArguments(method.getDeclaringType()); } // store type signatures and arguments for method parameters type if (parameterSignatures != null) { int length = parameterSignatures.length; if (length > 0) { this.parametersTypeSignatures = new char[length][][]; this.parametersTypeArguments = new char[length][][][]; for (int i=0; i<length; i++) { this.parametersTypeSignatures[i] = Util.splitTypeLevelsSignature(parameterSignatures[i]); this.parametersTypeArguments[i] = Util.getAllTypeArguments(this.parametersTypeSignatures[i]); } } } // Store type signatures and arguments for method this.constructorArguments = extractMethodArguments(method); if (hasConstructorArguments()) this.mustResolve = true; }
/** * Returns a new ast node corresponding to the given type. * * @param rewrite the compilation unit rewrite to use * @param type the new type * @param node the old node * @return A corresponding ast node */ protected static ASTNode createCorrespondingNode(final CompilationUnitRewrite rewrite, final TType type, ASTNode node) { ImportRewrite importRewrite= rewrite.getImportRewrite(); ImportRewriteContext context = new ContextSensitiveImportRewriteContext(node, importRewrite); return importRewrite.addImportFromSignature(new BindingKey(type.getBindingKey()).toSignature(), rewrite.getAST(), context); }
/** * Returns a new ast node corresponding to the given type. * * @param rewrite * the compilation unit rewrite to use * @param type * the specified type * @return A corresponding ast node */ protected static ASTNode createCorrespondingNode(final CompilationUnitRewrite rewrite, final TType type) { return rewrite.getImportRewrite().addImportFromSignature(new BindingKey(type.getBindingKey()).toSignature(), rewrite.getAST()); }