List of usage examples for org.eclipse.jdt.core IClasspathEntry getPath
IPath getPath();
From source file:com.google.gwt.eclipse.core.runtime.GWTJarsRuntimeTest.java
License:Open Source License
public void testGetClasspathEntries() throws Exception { IClasspathEntry[] cpEntries = runtime.getClasspathEntries(); // Look for the gwt-specific classpath entries List<IClasspathEntry> gwtCpEntries = new ArrayList<IClasspathEntry>(); for (IClasspathEntry cpEntry : cpEntries) { if (isGWTJar(runtime, cpEntry.getPath().lastSegment())) { gwtCpEntries.add(cpEntry);/*from w ww . j av a 2 s. c o m*/ } } // Make sure that there are two of them assertEquals(3, gwtCpEntries.size()); for (int i = 0; i < gwtCpEntries.size(); i++) { IClasspathEntry gwtClasspathEntry = gwtCpEntries.get(i); assertEquals(IClasspathEntry.CPE_LIBRARY, gwtClasspathEntry.getEntryKind()); assertEquals(IPackageFragmentRoot.K_BINARY, gwtClasspathEntry.getContentKind()); // Verify that our classpath entries point at the GWT javadoc. IClasspathAttribute[] extraAttributes = gwtClasspathEntry.getExtraAttributes(); assertTrue("No extra attributes seen for classpath entry: " + gwtClasspathEntry, extraAttributes.length > 0); assertEquals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, extraAttributes[0].getName()); /* * Entries should have their javadoc location point at a directory with "index.html". * Strangely, the values of these classpath attributes are specified as "file://" urls. */ File jdLocation = new File(new URL(extraAttributes[0].getValue()).getFile()); assertTrue("Javadoc file does not exist", jdLocation.exists()); List<String> files1 = Arrays.asList(jdLocation.list()); assertTrue("Javadoc file is not an index.html file.", files1.contains("index.html")); } }
From source file:com.google.gwt.eclipse.core.runtime.GWTJarsRuntimeTest.java
License:Open Source License
public void testGetValidationClasspathEntries() throws Exception { IClasspathEntry[] cpEntries = runtime.getClasspathEntries(); // Look for the validation-specific classpath entries List<IClasspathEntry> validationCpEntries = new ArrayList<IClasspathEntry>(); for (IClasspathEntry cpEntry : cpEntries) { if (cpEntry.getPath().lastSegment().startsWith(GWTRuntime.VALIDATION_API_JAR_PREFIX)) { validationCpEntries.add(cpEntry); }/*from w ww .j a va 2s . c om*/ } if (validationCpEntries.size() == 0) { String sdkVersion = runtime.getVersion(); // Can't be an internal version, because it would have the // validation jars assertFalse(SdkUtils.isInternal(sdkVersion)); // Sdk version must be pre-GWT 2.3.0 assertTrue(SdkUtils.compareVersionStrings(sdkVersion, "2.3.0") < 0); return; } // Make sure that there are at least two of them assertEquals(2, validationCpEntries.size()); IClasspathEntry sourcesEntry = null; IClasspathEntry binaryEntry = null; for (IClasspathEntry validationClasspathEntry : validationCpEntries) { // Verify the entry types assertEquals(IClasspathEntry.CPE_LIBRARY, validationClasspathEntry.getEntryKind()); assertEquals(IPackageFragmentRoot.K_BINARY, validationClasspathEntry.getContentKind()); if (validationClasspathEntry.getPath().lastSegment().contains("sources")) { sourcesEntry = validationClasspathEntry; } else { binaryEntry = validationClasspathEntry; } } // Verify that the sources and binary entries correspond to each other assertTrue(Util.findSourcesJarForClassesJar(binaryEntry.getPath()).equals(sourcesEntry.getPath())); // Verify that the source attachment path has been set for the binary // entry assertTrue(binaryEntry.getSourceAttachmentPath().equals(sourcesEntry.getPath())); }
From source file:com.google.gwt.eclipse.core.runtime.GWTProjectsRuntime.java
License:Open Source License
/** * FIXME - Were it not for the super source stuff, we would need this method. Can't we provide a * way for users to state which folders are super-source, etc? *//*from ww w .j ava 2 s. c o m*/ public static List<IRuntimeClasspathEntry> getGWTRuntimeProjectSourceEntries(IJavaProject project, boolean includeTestSourceEntries) throws SdkException { assert (isGWTRuntimeProject(project) && project.exists()); String projectName = project.getProject().getName(); List<IRuntimeClasspathEntry> sourceEntries = new ArrayList<IRuntimeClasspathEntry>(); IClasspathEntry[] gwtUserJavaProjClasspathEntries = null; try { gwtUserJavaProjClasspathEntries = project.getRawClasspath(); } catch (JavaModelException e) { throw new SdkException("Cannot extract raw classpath from " + projectName + " project."); } Set<IPath> absoluteSuperSourcePaths = new HashSet<IPath>(); for (IClasspathEntry curClasspathEntry : gwtUserJavaProjClasspathEntries) { if (curClasspathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath sourcePath = curClasspathEntry.getPath(); if (isJavadocPath(sourcePath)) { // Ignore javadoc paths. continue; } if (GWTProjectUtilities.isTestPath(sourcePath) && !includeTestSourceEntries) { // Ignore test paths, unless it is specified explicitly that we should // include them. continue; } sourceEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(sourcePath)); // Figure out the location of the super source path. IPath absoluteSuperSourcePath = sourcePath.removeLastSegments(1).append(SUPER_SOURCE_FOLDER_NAME); IPath relativeSuperSourcePath = absoluteSuperSourcePath.removeFirstSegments(1); if (absoluteSuperSourcePaths.contains(absoluteSuperSourcePath)) { // I've already included this path. continue; } if (project.getProject().getFolder(relativeSuperSourcePath).exists()) { /* * We've found the super source path, and we've not added it already. The existence test * uses a relative path, but the creation of a runtime classpath entry requires an * absolute path. */ sourceEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(absoluteSuperSourcePath)); absoluteSuperSourcePaths.add(absoluteSuperSourcePath); } IPath absoluteTestSuperSourcePath = sourcePath.removeLastSegments(1) .append(TEST_SUPER_SOURCE_FOLDER_NAME); IPath relativeTestSuperSourcePath = absoluteTestSuperSourcePath.removeFirstSegments(1); if (absoluteSuperSourcePaths.contains(absoluteTestSuperSourcePath)) { // I've already included this path. continue; } if (includeTestSourceEntries && project.getProject().getFolder(relativeTestSuperSourcePath).exists()) { /* * We've found the super source path, and we've not added it already. The existence test * uses a relative path, but the creation of a runtime classpath entry requires an * absolute path. */ sourceEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(absoluteTestSuperSourcePath)); absoluteSuperSourcePaths.add(absoluteTestSuperSourcePath); } } } if (absoluteSuperSourcePaths.isEmpty()) { GWTPluginLog.logError("There were no super source folders found for the project '{0}'", project.getProject().getName()); } return sourceEntries; }
From source file:com.google.gwt.eclipse.core.runtime.GWTProjectsRuntime.java
License:Open Source License
@Override public File getDevJar() throws SdkException /* throws Exception */, JavaModelException { IStringVariableManager variableManager = getVariableManager(); IValueVariable valueVariable = variableManager.getValueVariable("gwt_devjar"); if (valueVariable != null) { String value = valueVariable.getValue(); if (value != null) { IPath path = new Path(value); File file = path.toFile(); if (!file.exists()) { throw new SdkException("gwt_devjar Run/Debug variable points to a non-existent jar: " + value); }/* w w w .j a va 2 s .co m*/ return file; } } // We're going to have to search down the trunk to find the built gwt-dev // .jar. This assumes that the user has built the trunk at least once // TODO: can we remove the check for gwt.devjar from applicationCreator? IProject userProject = ResourcesPlugin.getWorkspace().getRoot().getProject(GWT_USER_PROJECT_NAME); if (!userProject.exists()) { throw new SdkException("The project ' " + userProject.getName() + "' does not exist in the workspace."); } IJavaProject javaUserProject = JavaCore.create(userProject); if (!javaUserProject.exists()) { throw new SdkException("The project ' " + userProject.getName() + "' is not a Java project."); } IClasspathEntry[] rawClasspaths = javaUserProject.getRawClasspath(); File stagingDir = null; IPath stagingPathLocation = null; for (IClasspathEntry rawClasspath : rawClasspaths) { if (rawClasspath.getEntryKind() == IClasspathEntry.CPE_SOURCE) { IPath sourcePathLocation = getAbsoluteLocation(rawClasspath.getPath(), userProject); stagingPathLocation = sourcePathLocation.removeLastSegments(2) .append(STAGING_FOLDER_RELATIVE_LOCATION); stagingDir = stagingPathLocation.toFile(); if (stagingDir.exists()) { break; } } } if (stagingPathLocation == null) { throw new SdkException("Contributor SDK build directory not found; Project '" + userProject.getName() + "' does not have any source folders."); } if (stagingDir == null || !stagingDir.exists()) { throw new SdkException("Contributor SDK build directory not found (expected at " + stagingPathLocation.toString() + ")"); } // Find the staging output directory: gwt-<platform>-<version> final File[] buildDirs = stagingDir.listFiles(new FileFilter() { @Override public boolean accept(File file) { return (file.isDirectory() && file.getName().startsWith("gwt-")); } }); if (buildDirs.length == 0) { throw new SdkException("Contributor SDK build directory not found (expected at " + stagingDir.toString() + File.separator + "gwt-<platform>-<version>)"); } // Find the gwt-dev .jar File[] gwtDevJars = buildDirs[0].listFiles(new FileFilter() { @Override public boolean accept(File file) { String name = file.getName(); IPath sdkLocationPath = Path.fromOSString(buildDirs[0].getAbsolutePath()); return (name.equalsIgnoreCase(Util.getDevJarName(sdkLocationPath))); } }); if (gwtDevJars.length == 0) { throw new SdkException("Contributor SDK build directory missing required JAR files"); } return gwtDevJars[0]; }
From source file:com.google.gwt.eclipse.core.runtime.GWTRuntimeContainerInitializerTest.java
License:Open Source License
/** * TODO: We need to revisit this test. Since we don't allow container updates * right now, it is not clear that the test is sufficiently strong. *//* w w w . java2 s .c o m*/ public void testRequestClasspathContainerUpdate() throws CoreException { IJavaProject testProject = getTestProject(); IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(defaultRuntimePath, testProject); final List<IClasspathEntry> newClasspathEntries = new ArrayList<IClasspathEntry>(); IClasspathEntry[] classpathEntries = classpathContainer.getClasspathEntries(); for (IClasspathEntry classpathEntry : classpathEntries) { if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { IClasspathAttribute[] extraAttributes = classpathEntry.getExtraAttributes(); List<IClasspathAttribute> newAttributes = new ArrayList<IClasspathAttribute>(); for (IClasspathAttribute extraAttribute : extraAttributes) { String attributeName = extraAttribute.getName(); if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attributeName)) { String attributeValue = extraAttribute.getValue() + "modified"; extraAttribute = JavaCore.newClasspathAttribute(attributeName, attributeValue); } newAttributes.add(extraAttribute); } IPath sourceAttachmentPath = new Path("/sourceAttachmentPath"); IPath sourceAttachmentRootPath = new Path("sourceAttachmentRootPath"); classpathEntry = JavaCore.newLibraryEntry(classpathEntry.getPath(), sourceAttachmentPath, sourceAttachmentRootPath, classpathEntry.getAccessRules(), newAttributes.toArray(new IClasspathAttribute[0]), classpathEntry.isExported()); } newClasspathEntries.add(classpathEntry); } // Update the classpath container initializer.requestClasspathContainerUpdate(defaultRuntimePath, testProject, new ClasspathContainerAdapter(classpathContainer) { @Override public IClasspathEntry[] getClasspathEntries() { return newClasspathEntries.toArray(new IClasspathEntry[0]); } }); // Check that the modifications took effect IClasspathContainer updatedContainer = JavaCore.getClasspathContainer(defaultRuntimePath, testProject); for (IClasspathEntry classpathEntry : updatedContainer.getClasspathEntries()) { if (classpathEntry.getEntryKind() != IClasspathEntry.CPE_LIBRARY) { // Ignore all non-library entries continue; } for (IClasspathAttribute attribute : classpathEntry.getExtraAttributes()) { if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attribute.getName())) { String value = attribute.getValue(); assertTrue(value.endsWith("modified")); } } IPath sourceAttachmentPath = classpathEntry.getSourceAttachmentPath(); assertEquals(new Path("/sourceAttachmentPath"), sourceAttachmentPath); IPath sourceAttachmentRootPath = classpathEntry.getSourceAttachmentRootPath(); assertEquals(new Path("sourceAttachmentRootPath"), sourceAttachmentRootPath); } }
From source file:com.google.gwt.eclipse.core.runtime.RuntimeClasspathEntryResolver.java
License:Open Source License
/** * Given a list of IClasspathEntry, produce an array of IRuntimeClasspathEntry based on that list. *//*from ww w. j a v a 2 s. c o m*/ private IRuntimeClasspathEntry[] resolveClasspathEntries(List<IClasspathEntry> classpathEntries) throws CoreException { LinkedHashSet<IRuntimeClasspathEntry> runtimeClasspathEntries = new LinkedHashSet<IRuntimeClasspathEntry>(); for (IClasspathEntry classpathEntry : classpathEntries) { if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { String projectName = classpathEntry.getPath().lastSegment(); IJavaProject theproject = JavaProjectUtilities.findJavaProject(projectName); IRuntimeClasspathEntry projectEntry = JavaRuntime.newProjectRuntimeClasspathEntry(theproject); runtimeClasspathEntries.add(projectEntry); runtimeClasspathEntries.addAll(dependenciesForProject(theproject)); } else { runtimeClasspathEntries.add(JavaRuntime.newArchiveRuntimeClasspathEntry(classpathEntry.getPath())); } } return runtimeClasspathEntries.toArray(NO_ENTRIES); }
From source file:com.google.gwt.eclipse.core.runtime.tools.WebAppProjectCreatorRunner.java
License:Open Source License
private static String computeClasspath(GWTRuntime gwtRuntime, String[] extraClassPath) throws CoreException { List<String> cpPaths = new ArrayList<String>(); for (IClasspathEntry c : gwtRuntime.getClasspathEntries()) { if (c.getEntryKind() == IClasspathEntry.CPE_PROJECT) { IJavaProject javaProject = JavaProjectUtilities.findJavaProject(c.getPath().toOSString()); IRuntimeClasspathEntry projectRuntimeEntry = JavaRuntime .newDefaultProjectClasspathEntry(javaProject); IRuntimeClasspathEntry[] resolvedEntries = JavaRuntime .resolveRuntimeClasspathEntry(projectRuntimeEntry, javaProject); for (IRuntimeClasspathEntry resolvedEntry : resolvedEntries) { cpPaths.add(resolvedEntry.getLocation()); }// w ww. jav a 2s.c om } else { cpPaths.add(c.getPath().toFile().getAbsolutePath()); } } if (extraClassPath != null) { cpPaths.addAll(Arrays.asList(extraClassPath)); } return ProcessUtilities.buildClasspathString(cpPaths); }
From source file:com.google.gwt.eclipse.core.speedtracer.ViewSourceServlet.java
License:Open Source License
private IPackageFragmentRoot getPackageFragmentRoot(String jarPathString, String preferredProjectName) throws IOException { try {//from ww w .j a v a 2 s . co m IFile jarIFile = getFile(jarPathString); // The JAR is in the workspace return JavaCore.createJarPackageFragmentRootFrom(jarIFile); } catch (IOException e) { // JAR must not be in the workspace (or is not a file), continue.. } File jarFile = new File(jarPathString); // Iterate projects to find the external JAR IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); List<IProject> projects = new ArrayList<IProject>(Arrays.asList(root.getProjects())); IProject preferredProject = preferredProjectName != null ? root.getProject(preferredProjectName) : null; if (preferredProject != null && preferredProject.exists()) { // Search the preferred project first projects.remove(preferredProject); projects.add(0, preferredProject); } for (IProject project : projects) { IJavaProject javaProject = JavaCore.create(project); if (!javaProject.exists()) { continue; } try { IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true); for (IClasspathEntry classpathEntry : classpathEntries) { IPath path = classpathEntry.getPath(); if (jarFile.equals(path.toFile())) { return javaProject.getPackageFragmentRoot(path.toOSString()); } } } catch (JavaModelException e) { GWTPluginLog.logWarning(e, "Could not check " + project.getName() + " for JAR file"); continue; } } return null; }
From source file:com.google.gwt.eclipse.core.validators.java.JsniJavaRef.java
License:Open Source License
public IJavaElement resolveJavaElement(IJavaProject project) throws UnresolvedJsniJavaRefException { IJavaElement element = null;/*from w w w. j a v a 2s .c om*/ // 0. Ignore the magic null reference if (className().equals("null")) { throw new UnresolvedJsniJavaRefException(null, this); } // 1. Try to find the type in the project's classpath IType type = JavaModelSearch.findType(project, dottedClassName()); if (type == null) { throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_UNRESOLVED_TYPE, this); } // TODO: remove this check once we can validate against super source // 1A. Do not validate JRE types (they could contain references to members // which are only defined in the emulated versions in super source) if (type.isBinary()) { IPackageFragmentRoot pkgRoot = (IPackageFragmentRoot) type .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); try { IClasspathEntry cpEntry = pkgRoot.getRawClasspathEntry(); if (cpEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && cpEntry.getPath().segment(0).equals(JavaRuntime.JRE_CONTAINER)) { throw new UnresolvedJsniJavaRefException(null, this); } } catch (JavaModelException e) { GWTPluginLog.logError(e); throw new UnresolvedJsniJavaRefException(null, this); } } // 2. Create a super-type hierarchy for the type, which we'll use for // finding its super classes and implemented interfaces ITypeHierarchy hierarchy; try { hierarchy = type.newSupertypeHierarchy(null); } catch (JavaModelException e) { GWTPluginLog.logError(e, "Error creating type hierarchy for " + className()); throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_UNRESOLVED_TYPE, this); } if (isMethod()) { if (isConstructor()) { if (matchesAnyOverload()) { element = JavaModelSearch.findFirstCtor(type); } else { String[] ctorParamTypes = paramTypes(); try { /* * If the constructor is on a non-static inner class, the reference * will have an extra parameter that specifies the enclosing type. * We'll need to check that type and then remove the parameter * before using JavaModelSearch to find the constructor. */ IJavaElement typeParent = type.getParent(); if (typeParent.getElementType() == IJavaElement.TYPE && !Flags.isStatic(type.getFlags())) { // Make sure we do have the enclosing type as the first parameter if (ctorParamTypes.length == 0) { throw new UnresolvedJsniJavaRefException( GWTProblemType.JSNI_JAVA_REF_NO_MATCHING_CTOR, this); } // Now verify that the type of the first parameter is actually the // the same type as the enclosing type IType parentType = (IType) typeParent; String enclosingTypeName = parentType.getFullyQualifiedName('.'); String enclosingTypeParam = JavaModelSearch.getQualifiedTypeName(ctorParamTypes[0]); if (!enclosingTypeName.equals(enclosingTypeParam)) { throw new UnresolvedJsniJavaRefException( GWTProblemType.JSNI_JAVA_REF_NO_MATCHING_CTOR, this); } // Drop the first parameter (the enclosing type) ctorParamTypes = new String[paramTypes().length - 1]; System.arraycopy(paramTypes(), 1, ctorParamTypes, 0, ctorParamTypes.length); } } catch (JavaModelException e) { GWTPluginLog.logError(e); // Continue on and try to resolve the reference anyway } // 3A. Find a constructor for this type with matching parameter types element = JavaModelSearch.findCtorInHierarchy(hierarchy, type, ctorParamTypes); if (element == null) { throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_NO_MATCHING_CTOR, this); } } } else { // 3B. Find a method for this type with the same name element = JavaModelSearch.findMethodInHierarchy(hierarchy, type, memberName()); if (element == null) { try { if (type.isEnum()) { // Ignore the magic Enum::values() method if (memberName().equals("values") && !matchesAnyOverload() && paramTypes().length == 0) { // Throwing this exception with a null GWTProblemType indicates // that the ref doesn't resolve, but can be ignored anyway. throw new UnresolvedJsniJavaRefException(null, this); } } } catch (JavaModelException e) { GWTPluginLog.logError(e); } throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_MISSING_METHOD, this); } if (!matchesAnyOverload()) { // Now try to match the method's parameter types element = JavaModelSearch.findMethodInHierarchy(hierarchy, type, memberName(), paramTypes()); if (element == null) { try { if (type.isEnum()) { // Ignore the synthetic Enum::valueOf(String) method. // Note that valueOf(Class,String) is not synthetic. if (memberName().equals("valueOf") && paramTypes().length == 1 && paramTypes()[0].equals("Ljava/lang/String;")) { // Throwing this exception with a null GWTProblemType // indicates that the ref doesn't resolve, but can be ignored // anyway. throw new UnresolvedJsniJavaRefException(null, this); } } } catch (JavaModelException e) { GWTPluginLog.logError(e); } throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_NO_MATCHING_METHOD, this); } } } } else { // 3C. Find a field with the same name assert (isField()); element = JavaModelSearch.findFieldInHierarchy(hierarchy, type, memberName()); if (element == null) { throw new UnresolvedJsniJavaRefException(GWTProblemType.JSNI_JAVA_REF_MISSING_FIELD, this); } } assert (element != null); return element; }
From source file:com.google.inject.tools.ideplugin.eclipse.EclipseJavaProject.java
License:Apache License
private List<String> expandClasspath(IClasspathEntry[] entries, String projectName, String projectLocation) throws Exception { final List<String> args = new ArrayList<String>(); IResource presource;//from w w w . j av a2s. c om String resourceLocation; String path; for (IClasspathEntry entry : entries) { switch (entry.getEntryKind()) { case IClasspathEntry.CPE_CONTAINER: IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project); args.addAll(expandClasspath(container.getClasspathEntries(), projectName, projectLocation)); break; case IClasspathEntry.CPE_SOURCE: IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath()); path = resource.getLocation().makeAbsolute().toOSString(); if (path.startsWith("/" + projectName)) { args.add(path.replaceFirst("/" + projectName, projectLocation)); } else { args.add(path); } break; case IClasspathEntry.CPE_LIBRARY: path = entry.getPath().makeAbsolute().toOSString(); if (path.startsWith("/" + projectName)) { args.add(path.replaceFirst("/" + projectName, projectLocation)); } else { args.add(path); } break; case IClasspathEntry.CPE_PROJECT: presource = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath()); resourceLocation = presource.getLocation().makeAbsolute().toOSString(); String outputLocation = resourceLocation; args.add(outputLocation.replaceFirst(presource.getName(), resourceLocation)); break; case IClasspathEntry.CPE_VARIABLE: break; default: //never happens } } return args; }