org.eclipse.jdt.core.ClasspathContainerInitializer Java Examples

The following examples show how to use org.eclipse.jdt.core.ClasspathContainerInitializer. 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: ReorgCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean canModifyAccessRules(IBinding binding) {
	IJavaElement element= binding.getJavaElement();
	if (element == null)
		return false;

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

	try {
		IClasspathEntry classpathEntry= root.getRawClasspathEntry();
		if (classpathEntry == null)
			return false;
		if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
			return true;
		if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
			ClasspathContainerInitializer classpathContainerInitializer= JavaCore.getClasspathContainerInitializer(classpathEntry.getPath().segment(0));
			IStatus status= classpathContainerInitializer.getAccessRulesStatus(classpathEntry.getPath(), root.getJavaProject());
			return status.isOK();
		}
	} catch (JavaModelException e) {
		return false;
	}
	return false;
}
 
Example #2
Source File: JavadocConfigurationPropertyPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IClasspathEntry handleContainerEntry(IPath containerPath, IJavaProject jproject, IPath jarPath) throws JavaModelException {
	ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
	IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
	if (initializer == null || container == null) {
		setDescription(Messages.format(PreferencesMessages.JavadocConfigurationPropertyPage_invalid_container, BasicElementLabels.getPathLabel(containerPath, false)));
		return null;
	}
	String containerName= container.getDescription();
	IStatus status= initializer.getAttributeStatus(containerPath, jproject, IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME);
	if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
		setDescription(Messages.format(PreferencesMessages.JavadocConfigurationPropertyPage_not_supported, containerName));
		return null;
	}
	IClasspathEntry entry= JavaModelUtil.findEntryInContainer(container, jarPath);
	if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
		setDescription(Messages.format(PreferencesMessages.JavadocConfigurationPropertyPage_read_only, containerName));
		fIsReadOnly= true;
		return entry;
	}
	Assert.isNotNull(entry);
	setDescription(PreferencesMessages.JavadocConfigurationPropertyPage_IsPackageFragmentRoot_description);
	return entry;
}
 
Example #3
Source File: NativeLibrariesPropertyPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IClasspathEntry handleContainerEntry(IPath containerPath, IJavaProject jproject, IPath jarPath) throws JavaModelException {
	ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
	IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
	if (initializer == null || container == null) {
		setDescription(Messages.format(PreferencesMessages.NativeLibrariesPropertyPage_invalid_container, BasicElementLabels.getPathLabel(containerPath, false)));
		return null;
	}
	String containerName= container.getDescription();
	IStatus status= initializer.getAttributeStatus(containerPath, jproject, JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY);
	if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED) {
		setDescription(Messages.format(PreferencesMessages.NativeLibrariesPropertyPage_not_supported, containerName));
		return null;
	}
	IClasspathEntry entry= JavaModelUtil.findEntryInContainer(container, jarPath);
	if (status.getCode() == ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY) {
		setDescription(Messages.format(PreferencesMessages.NativeLibrariesPropertyPage_read_only, containerName));
		fIsReadOnly= true;
		return entry;
	}
	Assert.isNotNull(entry);
	return entry;
}
 
Example #4
Source File: CPListElement.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IStatus evaluateContainerChildStatus(CPListElementAttribute attrib) {
	if (fProject != null) {
		ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(fPath.segment(0));
		if (initializer != null && initializer.canUpdateClasspathContainer(fPath, fProject)) {
			if (attrib.isBuiltIn()) {
				if (CPListElement.SOURCEATTACHMENT.equals(attrib.getKey())) {
					return initializer.getSourceAttachmentStatus(fPath, fProject);
				} else if (CPListElement.ACCESSRULES.equals(attrib.getKey())) {
					return initializer.getAccessRulesStatus(fPath, fProject);
				}
			} else {
				return initializer.getAttributeStatus(fPath, fProject, attrib.getKey());
			}
		}
		return new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, ClasspathContainerInitializer.ATTRIBUTE_READ_ONLY, "", null); //$NON-NLS-1$
	}
	return null;
}
 
Example #5
Source File: SourceAttachmentCommand.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void updateContainerClasspath(IJavaProject javaProject, IPath containerPath, IClasspathEntry newEntry) throws CoreException {
	IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, javaProject);
	List<IClasspathEntry> newEntries = updateElements(container.getClasspathEntries(), newEntry, (entry) -> {
		return entry.getEntryKind() == newEntry.getEntryKind() && entry.getPath().equals(newEntry.getPath());
	});
	IClasspathContainer updatedContainer = new UpdatedClasspathContainer(container, newEntries.toArray(new IClasspathEntry[0]));
	ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
	if (initializer != null) {
		initializer.requestClasspathContainerUpdate(containerPath, javaProject, updatedContainer);
	}
}
 
Example #6
Source File: JavaElementLabels.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the label of a classpath container.
 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}.
 *
 * @param containerPath the path of the container
 * @param project the project the container is resolved in
 * @return the label of the classpath container
 * @throws JavaModelException when resolving of the container failed
 */
public static String getContainerEntryLabel(IPath containerPath, IJavaProject project) throws JavaModelException {
	IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, project);
	if (container != null) {
		return org.eclipse.jdt.internal.core.manipulation.util.Strings.markLTR(container.getDescription());
	}
	ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
	if (initializer != null) {
		return org.eclipse.jdt.internal.core.manipulation.util.Strings.markLTR(initializer.getDescription(containerPath, project));
	}
	return BasicElementLabels.getPathLabel(containerPath, false);
}
 
Example #7
Source File: JavaElementLabels.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the label of a classpath container.
 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}.
 *
 * @param containerPath the path of the container
 * @param project the project the container is resolved in
 * @return the label of the classpath container
 * @throws JavaModelException when resolving of the container failed
 */
public static String getContainerEntryLabel(IPath containerPath, IJavaProject project) throws JavaModelException {
	IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, project);
	if (container != null) {
		return Strings.markLTR(container.getDescription());
	}
	ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
	if (initializer != null) {
		return Strings.markLTR(initializer.getDescription(containerPath, project));
	}
	return BasicElementLabels.getPathLabel(containerPath, false);
}
 
Example #8
Source File: JavaElementLabels.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the styled label of a classpath container.
 * The returned label is BiDi-processed with {@link TextProcessor#process(String, String)}.
 *
 * @param containerPath the path of the container
 * @param project the project the container is resolved in
 * @return the label of the classpath container
 *
 * @since 3.4
 */
public static StyledString getStyledContainerEntryLabel(IPath containerPath, IJavaProject project) {
	try {
		IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, project);
		String description= null;
		if (container != null) {
			description= container.getDescription();
		}
		if (description == null) {
			ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
			if (initializer != null) {
				description= initializer.getDescription(containerPath, project);
			}
		}
		if (description != null) {
			StyledString str= new StyledString(description);
			if (containerPath.segmentCount() > 0 && JavaRuntime.JRE_CONTAINER.equals(containerPath.segment(0))) {
				int index= description.indexOf('[');
				if (index != -1) {
					str.setStyle(index, description.length() - index, DECORATIONS_STYLE);
				}
			}
			return Strings.markLTR(str);
		}
	} catch (JavaModelException e) {
		// ignore
	}
	return new StyledString(BasicElementLabels.getPathLabel(containerPath, false));
}
 
Example #9
Source File: ClassPathContainer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public String getLabel() {
	if (fContainer != null)
		return fContainer.getDescription();

	IPath path= fClassPathEntry.getPath();
	String containerId= path.segment(0);
	ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerId);
	if (initializer != null) {
		String description= initializer.getDescription(path, getJavaProject());
		return Messages.format(PackagesMessages.ClassPathContainer_unbound_label, description);
	}
	return Messages.format(PackagesMessages.ClassPathContainer_unknown_label, BasicElementLabels.getPathLabel(path, false));
}
 
Example #10
Source File: BuildPathSupport.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Request a container update.
 * @param jproject The project of the container
 * @param container The container to request a change to
 * @param newEntries The updated entries
 * @throws CoreException if the request failed
 */
public static void requestContainerUpdate(IJavaProject jproject, IClasspathContainer container, IClasspathEntry[] newEntries) throws CoreException {
	IPath containerPath= container.getPath();
	IClasspathContainer updatedContainer= new UpdatedClasspathContainer(container, newEntries);
	ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
	if (initializer != null) {
		initializer.requestClasspathContainerUpdate(containerPath, jproject, updatedContainer);
	}
}
 
Example #11
Source File: CloudLibrariesPage.java    From google-cloud-eclipse with Apache License 2.0 4 votes vote down vote up
@Override
public boolean finish() {
  List<Library> libraries = getSelectedLibraries();
  try {
    if (isMavenProject) {
      // remove any library that wasn't selected
      Set<Library> removed = new HashSet<>(getAvailableLibraries());
      removed.removeAll(libraries);
      // No need for an Analytics ping here; addMavenLibraries will do it.
      BuildPath.updateMavenLibraries(project.getProject(), libraries, removed,
          new NullProgressMonitor());
    } else {
      if (!libraries.isEmpty()) {
        AnalyticsLibraryPingHelper.sendLibrarySelectionPing(
            AnalyticsEvents.NATIVE_PROJECT, libraries);
      }

      /*
       * FIXME: BuildPath.addNativeLibrary() is too heavy-weight here. ClasspathContainerWizard,
       * our wizard, is responsible for installing the classpath entry returned by getSelection(),
       * which will perform the library resolution. We just need to save the selected libraries
       * so that they are resolved later.
       */
      BuildPath.saveLibraryList(project, libraries, new NullProgressMonitor());
      Library masterLibrary =
          BuildPath.collectLibraryFiles(project, libraries, new NullProgressMonitor());
      // skip computeEntry() if we have an existing entry: unnecessary and simplifies testing too
      if (originalEntry == null) {
        newEntry = BuildPath.computeEntry(project, masterLibrary, new NullProgressMonitor());
        Verify.verifyNotNull(newEntry); // new entry should be created
      } else {
        // request update of existing entry
        ClasspathContainerInitializer initializer =
            JavaCore.getClasspathContainerInitializer(
                LibraryClasspathContainer.CONTAINER_PATH_PREFIX);
        // this is always true for our initializer
        if (initializer.canUpdateClasspathContainer(originalEntry.getPath(), project)) {
          // existing entry needs to be updated
          initializer.requestClasspathContainerUpdate(
              originalEntry.getPath(), project, null /*containerSuggestion*/);
        }
      }
    }
    return true;
  } catch (CoreException ex) {
    StatusUtil.setErrorStatus(this, "Error updating container definition", ex); //$NON-NLS-1$
    return false;
  }
}
 
Example #12
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 #13
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 #14
Source File: CPListElementAttribute.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * @return Returns <code>true</code> if the attribute a on a container child and is not supported
 */
public boolean isNotSupported() {
	return fStatus != null && fStatus.getCode() == ClasspathContainerInitializer.ATTRIBUTE_NOT_SUPPORTED;
}
 
Example #15
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Helper method that tests if an classpath entry can be found in a
 * container. <code>null</code> is returned if the entry can not be found
 * or if the container does not allows the configuration of source
 * attachments
 * @param jproject The container's parent project
 * @param containerPath The path of the container
 * @param libPath The path of the library to be found
 * @return IClasspathEntry A classpath entry from the container of
 * <code>null</code> if the container can not be modified.
 * @throws JavaModelException thrown if accessing the container failed
 */
public static IClasspathEntry getClasspathEntryToEdit(IJavaProject jproject, IPath containerPath, IPath libPath) throws JavaModelException {
	IClasspathContainer container= JavaCore.getClasspathContainer(containerPath, jproject);
	ClasspathContainerInitializer initializer= JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
	if (container != null && initializer != null && initializer.canUpdateClasspathContainer(containerPath, jproject)) {
		return findEntryInContainer(container, libPath);
	}
	return null; // attachment not possible
}