Java Code Examples for org.eclipse.jdt.core.IPackageFragmentRoot#K_BINARY

The following examples show how to use org.eclipse.jdt.core.IPackageFragmentRoot#K_BINARY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Object getPackageFragmentObjectToOpen(IPackageFragment packageFragment) throws JavaModelException {
	ITypeRoot typeRoot= null;
	IPackageFragmentRoot root= (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (root.getKind() == IPackageFragmentRoot.K_BINARY)
		typeRoot= (packageFragment).getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
	else
		typeRoot= (packageFragment).getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
	if (typeRoot.exists())
		return typeRoot;
	
	Object[] nonJavaResources= (packageFragment).getNonJavaResources();
	for (Object nonJavaResource : nonJavaResources) {
		if (nonJavaResource instanceof IFile) {
			IFile file= (IFile) nonJavaResource;
			if (file.exists() && JavaModelUtil.PACKAGE_HTML.equals(file.getName())) {
				return file;
			}
		}
	}
	return packageFragment;
}
 
Example 2
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the classpath entry of the given package fragment root. This is the raw entry, except
 * if the root is a referenced library, in which case it's the resolved entry.
 * 
 * @param root a package fragment root
 * @return the corresponding classpath entry
 * @throws JavaModelException if accessing the entry failed
 * @since 3.6
 */
public static IClasspathEntry getClasspathEntry(IPackageFragmentRoot root) throws JavaModelException {
	IClasspathEntry rawEntry= root.getRawClasspathEntry();
	int rawEntryKind= rawEntry.getEntryKind();
	switch (rawEntryKind) {
		case IClasspathEntry.CPE_LIBRARY:
		case IClasspathEntry.CPE_VARIABLE:
		case IClasspathEntry.CPE_CONTAINER: // should not happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=305037
			if (root.isArchive() && root.getKind() == IPackageFragmentRoot.K_BINARY) {
				IClasspathEntry resolvedEntry= root.getResolvedClasspathEntry();
				if (resolvedEntry.getReferencingEntry() != null)
					return resolvedEntry;
				else
					return rawEntry;
			}
	}
	return rawEntry;
}
 
Example 3
Source File: CollectingSearchRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isBinaryElement(Object element) throws JavaModelException {
	if (element instanceof IMember) {
		return ((IMember)element).isBinary();

	} else if (element instanceof ICompilationUnit) {
		return true;

	} else if (element instanceof IClassFile) {
		return false;

	} else if (element instanceof IPackageFragment) {
		return isBinaryElement(((IPackageFragment) element).getParent());

	} else if (element instanceof IPackageFragmentRoot) {
		return ((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_BINARY;

	}
	return false;

}
 
Example 4
Source File: JavaUI.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a selection dialog that lists all packages of the given Java project.
 * The caller is responsible for opening the dialog with <code>Window.open</code>,
 * and subsequently extracting the selected package (of type
 * <code>IPackageFragment</code>) via <code>SelectionDialog.getResult</code>.
 *
 * @param parent the parent shell of the dialog to be created
 * @param project the Java project
 * @param style flags defining the style of the dialog; the valid flags are:
 *   <code>IJavaElementSearchConstants.CONSIDER_BINARIES</code>, indicating that
 *   packages from binary package fragment roots should be included in addition
 *   to those from source package fragment roots;
 *   <code>IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS</code>, indicating that
 *   packages from required projects should be included as well.
 * @param filter the initial pattern to filter the set of packages. For example "com" shows
 * all packages starting with "com". The meta character '?' representing any character and
 * '*' representing any string are supported. Clients can pass an empty string if no filtering
 * is required.
 * @return a new selection dialog
 * @exception JavaModelException if the selection dialog could not be opened
 *
 * @since 2.0
 */
public static SelectionDialog createPackageDialog(Shell parent, IJavaProject project, int style, String filter) throws JavaModelException {
	Assert.isTrue((style | IJavaElementSearchConstants.CONSIDER_BINARIES | IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS) ==
		(IJavaElementSearchConstants.CONSIDER_BINARIES | IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS));

	IPackageFragmentRoot[] roots= null;
	if ((style & IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS) != 0) {
	    roots= project.getAllPackageFragmentRoots();
	} else {
		roots= project.getPackageFragmentRoots();
	}

	List<IPackageFragmentRoot> consideredRoots= null;
	if ((style & IJavaElementSearchConstants.CONSIDER_BINARIES) != 0) {
		consideredRoots= Arrays.asList(roots);
	} else {
		consideredRoots= new ArrayList<IPackageFragmentRoot>(roots.length);
		for (int i= 0; i < roots.length; i++) {
			IPackageFragmentRoot root= roots[i];
			if (root.getKind() != IPackageFragmentRoot.K_BINARY)
				consideredRoots.add(root);

		}
	}

	IJavaSearchScope searchScope= SearchEngine.createJavaSearchScope(consideredRoots.toArray(new IJavaElement[consideredRoots.size()]));
	BusyIndicatorRunnableContext context= new BusyIndicatorRunnableContext();
	if (style == 0 || style == IJavaElementSearchConstants.CONSIDER_REQUIRED_PROJECTS) {
		return createPackageDialog(parent, context, searchScope, false, true, filter);
	} else {
		return createPackageDialog(parent, context, searchScope, false, false, filter);
	}
}
 
Example 5
Source File: JavadocConfigurationPropertyPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
 */
@Override
public void createControl(Composite parent) {
	IJavaElement elem= getJavaElement();
	try {
		if (elem instanceof IPackageFragmentRoot && ((IPackageFragmentRoot) elem).getKind() == IPackageFragmentRoot.K_BINARY) {
			IPackageFragmentRoot root= (IPackageFragmentRoot) elem;

			IClasspathEntry entry= JavaModelUtil.getClasspathEntry(root);
			if (entry == null) {
				fIsValidElement= false;
				setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsIncorrectElement_description);
			} else {
				if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
					fContainerPath= entry.getPath();
					fEntry= handleContainerEntry(fContainerPath, elem.getJavaProject(), root.getPath());
					fIsValidElement= fEntry != null;
				} else {
					fContainerPath= null;
					fEntry= entry;
					fIsValidElement= true;
					setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsPackageFragmentRoot_description);
				}
			}

		} else if (elem instanceof IJavaProject) {
			fIsValidElement= true;
			setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsJavaProject_description);
		} else {
			fIsValidElement= false;
			setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsIncorrectElement_description);
		}
	} catch (JavaModelException e) {
		fIsValidElement= false;
		setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsIncorrectElement_description);
	}
	super.createControl(parent);
	PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.JAVADOC_CONFIGURATION_PROPERTY_PAGE);
}
 
Example 6
Source File: JavaDocLocations.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static URL getJavadocBaseLocation(IJavaElement element) throws JavaModelException {
	if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
		return getProjectJavadocLocation((IJavaProject) element);
	}

	IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element);
	if (root == null) {
		return null;
	}

	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		IClasspathEntry entry = root.getResolvedClasspathEntry();
		URL javadocLocation = getLibraryJavadocLocation(entry);
		if (javadocLocation != null) {
			return getLibraryJavadocLocation(entry);
		}
		entry = root.getRawClasspathEntry();
		switch (entry.getEntryKind()) {
			case IClasspathEntry.CPE_LIBRARY:
			case IClasspathEntry.CPE_VARIABLE:
				return getLibraryJavadocLocation(entry);
			default:
				return null;
		}
	} else {
		return getProjectJavadocLocation(root.getJavaProject());
	}
}
 
Example 7
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IEditorPart openDeclaration(IJavaElement element) throws PartInitException, JavaModelException {
	if (!(element instanceof IPackageFragment)) {
		return JavaUI.openInEditor(element);
	}
	
	IPackageFragment packageFragment= (IPackageFragment) element;
	ITypeRoot typeRoot;
	IPackageFragmentRoot root= (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		typeRoot= packageFragment.getClassFile(JavaModelUtil.PACKAGE_INFO_CLASS);
	} else {
		typeRoot= packageFragment.getCompilationUnit(JavaModelUtil.PACKAGE_INFO_JAVA);
	}

	// open the package-info file in editor if one exists
	if (typeRoot.exists())
		return JavaUI.openInEditor(typeRoot);

	// open the package.html file in editor if one exists
	Object[] nonJavaResources= packageFragment.getNonJavaResources();
	for (Object nonJavaResource : nonJavaResources) {
		if (nonJavaResource instanceof IFile) {
			IFile file= (IFile) nonJavaResource;
			if (file.exists() && JavaModelUtil.PACKAGE_HTML.equals(file.getName())) {
				return EditorUtility.openInEditor(file, true);
			}
		}
	}

	// select the package in the Package Explorer if there is no associated package Javadoc file
	PackageExplorerPart view= (PackageExplorerPart) JavaPlugin.getActivePage().showView(JavaUI.ID_PACKAGES);
	view.tryToReveal(packageFragment);
	return null;
}
 
Example 8
Source File: GWTMavenRuntime.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Find the classpath for get-dev.jar which is used to run super dev mode.
 *
 * @return IClasspathEntry for the path to gwt-dev.jar
 * @throws JavaModelException
 */
private IClasspathEntry findGwtCodeServerClasspathEntry() throws JavaModelException {
  IType type = javaProject.findType(GwtLaunchConfigurationProcessorUtilities.GWT_CODE_SERVER);
  if (type == null) {
    return null;
  }

  IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type
      .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
  if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) {
    return JavaCore.newLibraryEntry(packageFragmentRoot.getPath(), null, null);
  }

  return null;
}
 
Example 9
Source File: GWTMavenRuntime.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private IClasspathEntry findGwtUserClasspathEntry() throws JavaModelException {
  /*
   * Note that the type that we're looking for to determine if we're part of the gwt-user library is different than
   * the one that is used by the superclass. This is because the class that the superclass is querying for,
   * "com.google.gwt.core.client.GWT", also exists in the gwt-servlet library, and for some reason, this sometimes
   * ends up on the build path for Maven projects.
   *
   * TODO: See why Maven is putting gwt-servlet on the build path.
   *
   * TODO: Change the class query in the superclass to "com.google.gwt.junit.client.GWTTestCase"
   */
  IType type = javaProject.findType("com.google.gwt.junit.client.GWTTestCase");

  if (type == null) {
    return null;
  }

  IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type
      .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
  if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) {
    // TODO: If the Maven javadoc and source libs for gwt-dev.jar are
    // available, attach them here.
    return JavaCore.newLibraryEntry(packageFragmentRoot.getPath(), null, null);
  }

  return null;
}
 
Example 10
Source File: GWTMavenRuntime.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private IClasspathEntry findJavaXValidationClasspathEntry() throws JavaModelException {
  IType type = javaProject.findType("javax.validation.Constraint");

  if (type == null) {
    return null;
  }

  IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type
      .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
  if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) {
    return JavaCore.newLibraryEntry(packageFragmentRoot.getPath(), null, null);
  }

  return null;
}
 
Example 11
Source File: JavaWorkingSetPageContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected Object[] getPackageFragmentRootContent(IPackageFragmentRoot root) throws JavaModelException {
	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		// Don't show IJarEntryResource
		return root.getChildren();
	}

	return super.getPackageFragmentRootContent(root);
}
 
Example 12
Source File: PackageFragment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @see IPackageFragment#getCompilationUnits()
 */
public ICompilationUnit[] getCompilationUnits() throws JavaModelException {
	if (getKind() == IPackageFragmentRoot.K_BINARY) {
		return NO_COMPILATION_UNITS;
	}

	ArrayList list = getChildrenOfType(COMPILATION_UNIT);
	ICompilationUnit[] array= new ICompilationUnit[list.size()];
	list.toArray(array);
	return array;
}
 
Example 13
Source File: JavaDocLocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static URL getJavadocBaseLocation(IJavaElement element) throws JavaModelException {
	if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
		return getProjectJavadocLocation((IJavaProject) element);
	}

	IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element);
	if (root == null) {
		return null;
	}

	if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
		IClasspathEntry entry= root.getResolvedClasspathEntry();
		URL javadocLocation= getLibraryJavadocLocation(entry);
		if (javadocLocation != null) {
			return getLibraryJavadocLocation(entry);
		}
		entry= root.getRawClasspathEntry();
		switch (entry.getEntryKind()) {
			case IClasspathEntry.CPE_LIBRARY:
			case IClasspathEntry.CPE_VARIABLE:
				return getLibraryJavadocLocation(entry);
			default:
				return null;
		}
	} else {
		return getProjectJavadocLocation(root.getJavaProject());
	}
}
 
Example 14
Source File: SourceAttachmentPropertyPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private Control createPageContent(Composite composite) {
	try {
		fContainerPath= null;
		fEntry= null;
		fRoot= getJARPackageFragmentRoot();
		if (fRoot == null || fRoot.getKind() != IPackageFragmentRoot.K_BINARY) {
			return createMessageContent(composite, PreferencesMessages.SourceAttachmentPropertyPage_noarchive_message, null);
		}

		IPath containerPath= null;
		IJavaProject jproject= fRoot.getJavaProject();
		IClasspathEntry entry= JavaModelUtil.getClasspathEntry(fRoot);
		boolean canEditEncoding= true;
		if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
			containerPath= entry.getPath();
			ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
			IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
			if (initializer == null || container == null) {
				return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_invalid_container, BasicElementLabels.getPathLabel(containerPath, false)), fRoot);
			}
			String containerName= container.getDescription();

			IStatus status= initializer.getSourceAttachmentStatus(containerPath, jproject);
			if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
				return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_not_supported, containerName), null);
			}
			if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
				return createMessageContent(composite, Messages.format(PreferencesMessages.SourceAttachmentPropertyPage_read_only, containerName), fRoot);
			}
			IStatus attributeStatus= initializer.getAttributeStatus(containerPath, jproject, IClasspathAttribute.SOURCE_ATTACHMENT_ENCODING);
			canEditEncoding= !(attributeStatus.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED || attributeStatus.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY);

			entry= JavaModelUtil.findEntryInContainer(container, fRoot.getPath());
		}
		fContainerPath= containerPath;
		fEntry= entry;

		fSourceAttachmentBlock= new SourceAttachmentBlock(this, entry, canEditEncoding);
		return fSourceAttachmentBlock.createControl(composite);
	} catch (CoreException e) {
		JavaPlugin.log(e);
		return createMessageContent(composite, PreferencesMessages.SourceAttachmentPropertyPage_noarchive_message, null);
	}
}
 
Example 15
Source File: ExternalPackageFragmentRoot.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
int internalKind() throws JavaModelException {
	return IPackageFragmentRoot.K_BINARY;
}
 
Example 16
Source File: ClassFileEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void createSourceAttachmentControls(Composite composite, IPackageFragmentRoot root) throws JavaModelException {
	IClasspathEntry entry;
	try {
		entry= JavaModelUtil.getClasspathEntry(root);
	} catch (JavaModelException ex) {
		if (ex.isDoesNotExist())
			entry= null;
		else
			throw ex;
	}
	IPath containerPath= null;

	if (entry == null || root.getKind() != IPackageFragmentRoot.K_BINARY) {
		createLabel(composite, Messages.format(JavaEditorMessages.SourceAttachmentForm_message_noSource, BasicElementLabels.getFileName( fFile)));
		return;
	}

	IJavaProject jproject= root.getJavaProject();
	boolean canEditEncoding= true;
	if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
		containerPath= entry.getPath();
		ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
		IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
		if (initializer == null || container == null) {
			createLabel(composite, Messages.format(JavaEditorMessages.ClassFileEditor_SourceAttachmentForm_cannotconfigure, BasicElementLabels.getPathLabel(containerPath, false)));
			return;
		}
		String containerName= container.getDescription();
		IStatus status= initializer.getSourceAttachmentStatus(containerPath, jproject);
		if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
			createLabel(composite, Messages.format(JavaEditorMessages.ClassFileEditor_SourceAttachmentForm_notsupported, containerName));
			return;
		}
		if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
			createLabel(composite, Messages.format(JavaEditorMessages.ClassFileEditor_SourceAttachmentForm_readonly, containerName));
			return;
		}
		IStatus attributeStatus= initializer.getAttributeStatus(containerPath, jproject, IClasspathAttribute.SOURCE_ATTACHMENT_ENCODING);
		canEditEncoding= !(attributeStatus.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED || attributeStatus.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY);
		entry= JavaModelUtil.findEntryInContainer(container, root.getPath());
		Assert.isNotNull(entry);
	}


	Button button;

	IPath path= entry.getSourceAttachmentPath();
	if (path == null || path.isEmpty()) {
		String rootLabel= JavaElementLabels.getElementLabel(root, JavaElementLabels.ALL_DEFAULT);
		createLabel(composite, Messages.format(JavaEditorMessages.SourceAttachmentForm_message_noSourceAttachment, rootLabel));
		createLabel(composite, JavaEditorMessages.SourceAttachmentForm_message_pressButtonToAttach);
		createLabel(composite, null);

		button= createButton(composite, JavaEditorMessages.SourceAttachmentForm_button_attachSource);

	} else {
		createLabel(composite, Messages.format(JavaEditorMessages.SourceAttachmentForm_message_noSourceInAttachment, BasicElementLabels.getFileName(fFile)));
		createLabel(composite, JavaEditorMessages.SourceAttachmentForm_message_pressButtonToChange);
		createLabel(composite, null);

		button= createButton(composite, JavaEditorMessages.SourceAttachmentForm_button_changeAttachedSource);
	}

	button.addSelectionListener(getButtonListener(entry, containerPath, jproject, canEditEncoding));
}
 
Example 17
Source File: ReorgUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static boolean isClassFolder(IJavaElement javaElement) throws JavaModelException {
	return (javaElement instanceof IPackageFragmentRoot) &&
			((IPackageFragmentRoot)javaElement).getKind() == IPackageFragmentRoot.K_BINARY;
}
 
Example 18
Source File: JReFrameworkerProject.java    From JReFrameworker with MIT License 4 votes vote down vote up
/**
	 * Replaces the classpath jar entry with the given jar
	 * @param jarName
	 * @param updatedLibrary
	 * @throws IOException 
	 * @throws CoreException 
	 */
	public void updateProjectLibrary(String jarName, File updatedLibrary) throws IOException, CoreException {
		updatedLibrary = updatedLibrary.getCanonicalFile();
		String updatedLibraryPath = updatedLibrary.getCanonicalPath();
		File projectRoot = project.getLocation().toFile().getCanonicalFile();
		
		// check if the path should be relative or absolute
		boolean isUpdatedLibraryContainedInProject = false;
		File parent = updatedLibrary.getParentFile();
		while(parent != null){
			if(parent.equals(projectRoot)){
				isUpdatedLibraryContainedInProject = true;
				break;
			} else {
				parent = parent.getParentFile();
			}
		}
		
		// if the updated library is inside the project, then make the path relative
		// otherwise we must use the absolution path
		if(isUpdatedLibraryContainedInProject){
			String base = projectRoot.getCanonicalPath();
			String relativeFilePath = updatedLibrary.getCanonicalPath().substring(base.length());
			if(relativeFilePath.charAt(0) == File.separatorChar){
				relativeFilePath = relativeFilePath.substring(1);
			}
			updatedLibraryPath = relativeFilePath;
		}
		
		// create path to library
		IPath path;
//		// TODO: figure out why relative paths were causing "Illegal require library path" error during testing on Windows
//		// seems fine on the mac, maybe it was a weird windows error?
//		if(isUpdatedLibraryContainedInProject){
//			updatedLibraryPath = updatedLibraryPath.replace(File.separator, "/");
//	    	// library is at some path relative to project root
//			path = new Path(updatedLibraryPath);
//	    } else {
	    	// library is outside the project, using absolute path
	    	path = jProject.getProject().getFile(updatedLibraryPath).getLocation();
//	    }
		
		// create a classpath entry for the library
		IClasspathEntry updatedLibraryEntry = new org.eclipse.jdt.internal.core.ClasspathEntry(
    	        IPackageFragmentRoot.K_BINARY,
    	        IClasspathEntry.CPE_LIBRARY, path,
    	        ClasspathEntry.INCLUDE_ALL, // inclusion patterns
    	        ClasspathEntry.EXCLUDE_NONE, // exclusion patterns
    	        null, null, null, // specific output folder
    	        false, // exported
    	        ClasspathEntry.NO_ACCESS_RULES, false, // no access rules to combine
    	        ClasspathEntry.NO_EXTRA_ATTRIBUTES);
		
		// get the classpath entries
		ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
		for(IClasspathEntry entry : jProject.getRawClasspath()){
			entries.add(entry);
		}
		
		// search through the classpath's existing entries and replace the corresponding library entry
	    boolean found = false;
	    for(int i=0; i< entries.size(); i++){
	    	if(entries.get(i).getPath().toFile().getName().equals(jarName)){
	    		found = true;
	    		entries.set(i, updatedLibraryEntry);
	    		// assuming there is only one library with the same name...
	    		break;
	    	}
	    }
	    
	    // if the classpath entry was not found (because it was in a JRE container), then we need to add it to the classpath
	    if(!found){
	    	entries.add(updatedLibraryEntry);
	    }
	    
	    // update the classpath
	    IClasspathEntry[] classpath = new IClasspathEntry[entries.size()];
	    entries.toArray(classpath);
	    jProject.setRawClasspath(classpath, null);
	    
	    // refresh project
	 	jProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
	}
 
Example 19
Source File: ExternalPackageFragmentRoot.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @see IPackageFragmentRoot
 */
public int getKind() {
	return IPackageFragmentRoot.K_BINARY;
}
 
Example 20
Source File: ExternalPackageFragmentRoot.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * An external class folder is always K_BINARY.
 */
protected int determineKind(IResource underlyingResource) {
	return IPackageFragmentRoot.K_BINARY;
}