public void extractClassFile(IClassFile classFile) { // Verify that it's a top-level type, or a subtype of a top-level type // try { // IType declaring = classFile.getType(); // while (declaring != null) { // if (declaring.isLocal() || declaring.isAnonymous()) { // return; // } // declaring = declaring.getDeclaringType(); // } // } catch (JavaModelException e) { // logger.log(Level.SEVERE, "Error in extracting class file", e); // return; // } IJavaElement parent = classFile.getParent(); while (true) { if (parent == null) { logger.log(Level.SEVERE, "Unable to find package for: " + classFile.getElementName()); break; } else if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) { // Write the class file name = classFile.getElementName(); path = parent.getElementName() + "." + name; fileWriter.writeClassFile(name, path); try { if (classFile.getType().isAnonymous()) { String fqn = classFile.getType().getFullyQualifiedName(); String containingFqn = fqn.substring(0, fqn.lastIndexOf('$')); relationWriter.writeInside( classFile.getType().getFullyQualifiedName(), containingFqn, path); } else { relationWriter.writeInside( classFile.getType().getFullyQualifiedName(), parent.getElementName(), path); entityWriter.writePackage(parent.getElementName()); } } catch (JavaModelException e) { logger.log(Level.SEVERE, "Error in extracting class file", e); } break; } else { logger.log( Level.SEVERE, classFile.getType().getFullyQualifiedName() + " should be top-level!"); parent = parent.getParent(); } } extractIType(classFile.getType()); name = null; path = null; }
public void testPositiveSystemLibrarySourceLocation() throws Exception { IClasspathEntry[] cpes = get14Project().getRawClasspath(); IClasspathEntry lib = null; for (int i = 0; i < cpes.length; i++) { if (cpes[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) { if (cpes[i].getPath().equals(new Path(JRE_CONTAINER_1_4_CPE_NAME))) { lib = cpes[i]; break; } } } assertNotNull("Could not find JRE_CONTAINER entry", lib); IPackageFragmentRoot[] roots = get14Project().findPackageFragmentRoots(lib); Object source = null; for (int i = 0; i < roots.length; i++) { IPackageFragmentRoot root = roots[i]; IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root); source = location.findSourceElement("java.lang.Object"); if (source != null) { break; } } assertTrue("Did not find source for 'Object'", source instanceof IClassFile); IClassFile cf = (IClassFile) source; assertEquals("Did not find source for 'Object'", "Object.class", cf.getElementName()); for (int i = 0; i < roots.length; i++) { IPackageFragmentRoot root = roots[i]; IJavaSourceLocation location = new PackageFragmentRootSourceLocation(root); source = location.findSourceElement("java.util.Vector$1"); if (source != null) { break; } } assertTrue("Did not find source for 'Vector$1'", source instanceof IClassFile); cf = (IClassFile) source; assertEquals("Did not find source for 'Vector$1'", "Vector$1.class", cf.getElementName()); }