public static String getMatchedPackage(String className, ICompilationUnit cUnit) throws JavaModelException { int higherAccuracy = 0; String matchedPackage = cUnit.getPackageDeclarations()[0].getElementName(); IImportDeclaration[] imports = cUnit.getImports(); for (IImportDeclaration impDec : imports) { String fullName = impDec.getElementName(); int dotIndex = fullName.lastIndexOf('.'); String lastNameOfImport = fullName.substring(dotIndex + 1); if (lastNameOfImport.equals(className) && higherAccuracy < 2) { matchedPackage = fullName.substring(0, dotIndex); higherAccuracy = 2; } else if (lastNameOfImport.equals("*") && higherAccuracy < 1) { matchedPackage = fullName.substring(0, dotIndex); higherAccuracy = 1; } } if (higherAccuracy > 0) return matchedPackage; if (cUnit.getType(className) != null) return cUnit.getPackageDeclarations()[0].getElementName(); return "java.lang"; }
/** * Determines if the supplied src file contains an import for the supplied type (including * wildcard .* imports). * * @param src The compilation unit. * @param type The type. * @return true if the src file has a qualifying import. */ public static boolean containsImport(ICompilationUnit src, IType type) throws Exception { String typePkg = type.getPackageFragment().getElementName(); IPackageDeclaration[] packages = src.getPackageDeclarations(); String pkg = packages.length > 0 ? packages[0].getElementName() : null; // classes in same package are auto imported. if ((pkg == null && typePkg == null) || (pkg != null && pkg.equals(typePkg))) { return true; } // java.lang is auto imported. if (JAVA_LANG.equals(typePkg)) { return true; } typePkg = typePkg + ".*"; String typeName = type.getFullyQualifiedName().replace('$', '.'); IImportDeclaration[] imports = src.getImports(); for (int ii = 0; ii < imports.length; ii++) { String name = imports[ii].getElementName(); if (name.equals(typeName) || name.equals(typePkg)) { return true; } } return false; }
/** * Gets the fully qualified class name for an active file. For example, its value is foo.bar.Baz. * * @param file Get fully qualified class file. * @return The fully qualified class name. For example,foo.bar.Baz. */ private String getFullyQualifedClassName(IFile file) { String fullClassName = ""; if (file.exists() && file.getName().endsWith(EclipseSensorConstants.JAVA_EXT)) { ICompilationUnit compilationUnit = (ICompilationUnit) JavaCore.create(file); String className = compilationUnit.getElementName(); if (className.endsWith(EclipseSensorConstants.JAVA_EXT)) { className = className.substring(0, className.length() - 5); } try { IPackageDeclaration[] packageDeclarations = compilationUnit.getPackageDeclarations(); // Should only have one package declaration if (packageDeclarations == null || packageDeclarations.length == 0) { fullClassName = className; } else { fullClassName = packageDeclarations[0].getElementName() + '.' + className; } } catch (JavaModelException e) { // This exception will be thrown if user is working on a Java but did not open // it with "Java Perspective". Thus, the Java Model does not exist to parse // Java files. So we only log out exception while Eclipse's Java Perspective // exits. if (!e.isDoesNotExist()) { EclipseSensorPlugin.getDefault().log(file.getName(), e); } } } return fullClassName; }