/** * Get all compilation units of a selection. * @param javaElements the selected java elements * @return all compilation units containing and contained in elements from javaElements * @throws JavaModelException */ public static ICompilationUnit[] getAllCompilationUnits(IJavaElement[] javaElements) throws JavaModelException { HashSet result= new HashSet(); for (int i= 0; i < javaElements.length; i++) { addAllCus(result, javaElements[i]); } return (ICompilationUnit[]) result.toArray(new ICompilationUnit[result.size()]); }
IMethod method= findMethod(name, paramTypes, isConstructor, type); if (method != null) { return method; IMethod res= findMethodInHierarchy(hierarchy, superClass, name, paramTypes, isConstructor); if (res != null) { return res; IType[] superInterfaces= hierarchy.getSuperInterfaces(type); for (int i= 0; i < superInterfaces.length; i++) { IMethod res= findMethodInHierarchy(hierarchy, superInterfaces[i], name, paramTypes, false); if (res != null) { return res;
public static boolean is50OrHigherJRE(IJavaProject project) throws CoreException { IVMInstall vmInstall= JavaRuntime.getVMInstall(project); if (!(vmInstall instanceof IVMInstall2)) return true; // assume 5.0. String compliance= getCompilerCompliance((IVMInstall2) vmInstall, null); if (compliance == null) return true; // assume 5.0 return compliance.startsWith(JavaCore.VERSION_1_5) || compliance.startsWith(JavaCore.VERSION_1_6); }
private static IType findType(IPackageFragment pack, String fullyQualifiedName) throws JavaModelException{ ICompilationUnit[] cus= pack.getCompilationUnits(); for (int i= 0; i < cus.length; i++) { ICompilationUnit unit= cus[i]; IType type= findType(unit, fullyQualifiedName); if (type != null && type.exists()) return type; } return null; }
private static IType findType(ICompilationUnit cu, String fullyQualifiedName) throws JavaModelException{ IType[] types= cu.getAllTypes(); for (int i= 0; i < types.length; i++) { IType type= types[i]; if (getFullyQualifiedName(type).equals(fullyQualifiedName)) return type; } return null; }
private void getImportStatements(String signature, IType declaringType) throws JavaModelException{ String erasure = Signature.getTypeErasure(signature); String resolvedTypeName = JavaModelUtil.getResolvedTypeName(erasure, declaringType); if (resolvedTypeName != null && !importStatements.contains(resolvedTypeName) && !resolvedTypeName.startsWith("java.lang")) { //$NON-NLS-1$ importStatements.add(resolvedTypeName); } String [] params = Signature.getTypeArguments(signature); for(int i=0;i<params.length; i++){ getImportStatements(params[i], declaringType); } }
public 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= JavaModelUtil.concatenateName(packageName, typeName); return qualifier.equals(mainTypeName); }
/** * Finds a type in a compilation unit. Typical usage is to find the corresponding * type in a working copy. * @param cu the compilation unit to search in * @param typeQualifiedName the type qualified name (type name with enclosing type names (separated by dots)) * @return the type found, or null if not existing */ public static IType findTypeInCompilationUnit(ICompilationUnit cu, String typeQualifiedName) throws JavaModelException { IType[] types= cu.getAllTypes(); for (int i= 0; i < types.length; i++) { String currName= getTypeQualifiedName(types[i]); if (typeQualifiedName.equals(currName)) { return types[i]; } } return null; }
/** * @deprecated use {@link #findMethodInHierarchy(ITypeHierarchy, IType, String, String[], boolean)} */ public static IMethod findMethodImplementationInHierarchy(ITypeHierarchy hierarchy, IType type, String name, String[] paramTypes, boolean isConstructor) throws JavaModelException { return findMethodInHierarchy(hierarchy, type, name, paramTypes, isConstructor); }
public static boolean is50OrHigher(IJavaProject project) { return is50OrHigher(project.getOption(JavaCore.COMPILER_COMPLIANCE, true)); }
private static IType findType(IPackageFragmentRoot root, String fullyQualifiedName) throws JavaModelException{ IJavaElement[] children= root.getChildren(); for (int i= 0; i < children.length; i++) { IJavaElement element= children[i]; if (element.getElementType() == IJavaElement.PACKAGE_FRAGMENT){ IPackageFragment pack= (IPackageFragment)element; if (! fullyQualifiedName.startsWith(pack.getElementName())) continue; IType type= findType(pack, fullyQualifiedName); if (type != null && type.exists()) return type; } } return null; }
/** * Returns the fully qualified name of a type's container. (package name or enclosing type name) */ public static String getTypeContainerName(IType type) { IType outerType= type.getDeclaringType(); if (outerType != null) { return getFullyQualifiedName(outerType); } else { return type.getPackageFragment().getElementName(); } }
/** * This method is intended for internal use only. This will add import statements for the specified * type if it is determined to be necessary. This does accept null parameters. It will not return null. * @see NewJavaClassOperation#getMethodStub(IMethod, String, String) * * @param refTypeSig * @param declaringType * @return String type signature * @throws JavaModelException */ private String resolveAndAdd(String refTypeSig, IType declaringType) throws JavaModelException { if(refTypeSig.indexOf(Signature.C_GENERIC_START, 0) != -1){ getImportStatements(refTypeSig, declaringType); } else { String resolvedTypeName = JavaModelUtil.getResolvedTypeName(refTypeSig, declaringType); // Could type not be resolved and is import statement missing? if (resolvedTypeName != null && !importStatements.contains(resolvedTypeName) && !resolvedTypeName.startsWith("java.lang")) { //$NON-NLS-1$ importStatements.add(resolvedTypeName); } } return Signature.toString(refTypeSig); }
return JavaModelUtil.concatenateName(resolvedNames[0][0], resolvedNames[0][1]);
/** * Returns the qualified type name of the given type using '.' as separators. * This is a replace for IType.getTypeQualifiedName() * which uses '$' as separators. As '$' is also a valid character in an id * this is ambiguous. JavaCore PR: 1GCFUNT */ public static String getTypeQualifiedName(IType type) { try { if (type.isBinary() && !type.isAnonymous()) { IType declaringType= type.getDeclaringType(); if (declaringType != null) { return getTypeQualifiedName(declaringType) + '.' + type.getElementName(); } } } catch (JavaModelException e) { // ignore } return type.getTypeQualifiedName('.'); }
/** * This method is intended for internal use only. This will recursively check the supertype heirarchy for * the passed in method. This will not accept null parameters. It will return null if the method does * not already exist in the heirarchy. * @see NewJavaClassOperation#findMethodImplementationInHierarchy(IMethod, IType) * @see JavaModelUtil#findMethodImplementationInHierarchy(ITypeHierarchy, IType, String, String[], boolean) * * @param tH * @param thisType * @param methodName * @param parameterTypes * @param isConstructor * @return IMethod * @throws JavaModelException */ private IMethod findMethodImplementationInHierarchy(ITypeHierarchy tH, IType thisType, String methodName, String parameterTypes[], boolean isConstructor) throws JavaModelException { IMethod found = JavaModelUtil.findMethod(methodName, parameterTypes, isConstructor, thisType); // If method exists make sure it is not abstract if (found != null && !Flags.isAbstract(found.getFlags())) { return found; } // Check recursively return JavaModelUtil.findMethodInHierarchy(tH, thisType, methodName, parameterTypes, isConstructor); }
/** * Finds a type by its qualified type name (dot separated). * @param jproject The java project to search in * @param fullyQualifiedName The fully qualified name (type name with enclosing type names and package (all separated by dots)) * @param owner the working copy owner * @return The type found, or null if not existing */ public static IType findType(IJavaProject jproject, String fullyQualifiedName, WorkingCopyOwner owner) throws JavaModelException { //workaround for bug 22883 IType type= jproject.findType(fullyQualifiedName, owner); if (type != null) return type; IPackageFragmentRoot[] roots= jproject.getPackageFragmentRoots(); for (int i= 0; i < roots.length; i++) { IPackageFragmentRoot root= roots[i]; type= findType(root, fullyQualifiedName); if (type != null && type.exists()) return type; } return null; }
/** * Returns the fully qualified name of the given type using '.' as separators. * This is a replace for IType.getFullyQualifiedTypeName * which uses '$' as separators. As '$' is also a valid character in an id * this is ambiguous. JavaCore PR: 1GCFUNT */ public static String getFullyQualifiedName(IType type) { try { if (type.isBinary() && !type.isAnonymous()) { IType declaringType= type.getDeclaringType(); if (declaringType != null) { return getFullyQualifiedName(declaringType) + '.' + type.getElementName(); } } } catch (JavaModelException e) { // ignore } return type.getFullyQualifiedName('.'); }
private boolean hasGenericParams(IMethod method) { try { IType parentType = method.getDeclaringType(); String[] paramTypes = method.getParameterTypes(); int nP = paramTypes.length; for (int i = 0; i < nP; i++) { String type = paramTypes[i]; if (!isPrimitiveType(type)) { type = JavaModelUtil.getResolvedTypeName(type, parentType); if(type.indexOf(Signature.C_GENERIC_START, 0) != -1){ return true; } } } } catch (JavaModelException e) { Logger.getLogger().log(e); } return false; }
IPackageFragmentRoot[] packageFragmentRoots= javaProject.getPackageFragmentRoots(); for (int i= 0; i < packageFragmentRoots.length; i++) addAllCus(collector, packageFragmentRoots[i]); return; IJavaElement[] packageFragments= packageFragmentRoot.getChildren(); for (int j= 0; j < packageFragments.length; j++) addAllCus(collector, packageFragments[j]); return;