static boolean isImplicitImport(String qualifier, ICompilationUnit cu) { if ("java.lang".equals(qualifier)) { //$NON-NLS-1$ return true; } String packageName = cu.getParent().getElementName(); if (qualifier.equals(packageName)) { return true; } String typeName = JavaCore.removeJavaLikeExtension(cu.getElementName()); String mainTypeName = concatenateName(packageName, typeName); return qualifier.equals(mainTypeName); }
static String getTypeQualifiedName(ITypeBinding type) { List<String> result= new ArrayList<>(5); createName(type, false, result); return String.join(".",result); }
static int mapTypeParameterIndex(IType[] path, int pathIndex, int paramIndex) throws JavaModelException, ArrayIndexOutOfBoundsException { if (pathIndex == 0) { // break condition: we've reached the top of the hierarchy return paramIndex; } IType subType= path[pathIndex]; IType superType= path[pathIndex - 1]; String superSignature= findMatchingSuperTypeSignature(subType, superType); ITypeParameter param= subType.getTypeParameters()[paramIndex]; int index= findMatchingTypeArgumentIndex(superSignature, param.getElementName()); if (index == -1) { // not mapped through return -1; } return mapTypeParameterIndex(path, pathIndex - 1, index); }
&& TypeProposalUtils.isPackageInfo(compilationUnit)) { return qualifiedTypeName; && TypeProposalUtils.isImplicitImport( Signature.getQualifier(qualifiedTypeName), compilationUnit)) {
IType[] path= TypeProposalUtils.computeInheritancePath(type, expectedType); if (path == null) { indices[paramIdx]= TypeProposalUtils.mapTypeParameterIndex(path, path.length - 1, paramIdx);
static String findMatchingSuperTypeSignature(IType subType, IType superType) throws JavaModelException { String[] signatures= getSuperTypeSignatures(subType, superType); for (String signature : signatures) { String qualified= SignatureUtil.qualifySignature(signature, subType); String subFQN= SignatureUtil.stripSignatureToFQN(qualified); String superFQN= superType.getFullyQualifiedName(); if (subFQN.equals(superFQN)) { return signature; } // TODO handle local types } return null; // throw new JavaModelException(new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, "Illegal hierarchy", null))); //$NON-NLS-1$ }
if (compilationUnit != null && isImplicitImport(Signature.getQualifier(qualifiedTypeName), compilationUnit)) {
private String computeTypeProposal(ITypeBinding binding, ITypeParameter parameter) throws JavaModelException { final String name = TypeProposalUtils.getTypeQualifiedName(binding); if (binding.isWildcardType()) { if (binding.isUpperbound()) { // replace the wildcard ? with the type parameter name to get "E extends Bound" instead of "? extends Bound" // String contextName= name.replaceFirst("\\?", parameter.getElementName()); //$NON-NLS-1$ // upper bound - the upper bound is the bound itself return binding.getBound().getName(); } // no or upper bound - use the type parameter of the inserted type, as it may be more // restrictive (eg. List<?> list= new SerializableList<Serializable>()) return computeTypeProposal(parameter); } // not a wildcard but a type or type variable - this is unambigously the right thing to insert return name; }
this.importRewrite = TypeProposalUtils.createImportRewrite(compilationUnit);
static void createName(ITypeBinding type, boolean includePackage, List<String> list) { ITypeBinding baseType = type; if (type.isArray()) { baseType = type.getElementType(); } if (!baseType.isPrimitive() && !baseType.isNullType()) { ITypeBinding declaringType = baseType.getDeclaringClass(); if (declaringType != null) { createName(declaringType, includePackage, list); } else if (includePackage && !baseType.getPackage().isUnnamed()) { String[] components = baseType.getPackage().getNameComponents(); for (int i = 0; i < components.length; i++) { list.add(components[i]); } } } if (!baseType.isAnonymous()) { list.add(type.getName()); } else { list.add("$local$"); //$NON-NLS-1$ } }