下面列出了怎么用org.eclipse.core.runtime.IContributor的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns <code>true</code> if this bundle implements the extension point
* with the given <code>extensionPointId</code>.
*
* @param bundle the bundle to test
* @param extensionSimpleId the simple id of the extension point
* @param extensionPointId extension id to search for
*
* @return <code>true</code> if this bundle implements the extension point
* with the given <code>extensionPointId</code>
*/
public static boolean contributesToExtensionPoint(Bundle bundle,
String extensionSimpleId, String extensionPointId) {
IExtensionRegistry registry = RegistryFactory.getRegistry();
IContributor contributor = ContributorFactoryOSGi.createContributor(bundle);
for (IExtension extension : registry.getExtensions(contributor.getName())) {
if (extension.getExtensionPointUniqueIdentifier().equals(extensionPointId)) {
if (extensionSimpleId != null
&& !extensionSimpleId.equals(extension.getSimpleIdentifier())) {
continue;
}
return true;
}
}
return false;
}
private boolean isTargetClass ( final String className, final IContributor contributor, final Class<? extends EObject> clazz )
{
if ( className == null )
{
return false;
}
final Bundle bundle = findBundle ( contributor );
if ( bundle == null )
{
throw new IllegalStateException ( String.format ( "Unable to find bundle '%s'", contributor.getName () ) );
}
try
{
final Class<?> targetClazz = bundle.loadClass ( className );
if ( targetClazz.isAssignableFrom ( clazz ) )
{
return true;
}
}
catch ( final ClassNotFoundException e )
{
throw new IllegalStateException ( String.format ( "Unable to find target class '%s'", className ), e );
}
return false;
}
private Bundle findBundle ( final IContributor contributor )
{
final String cid = contributor.getName ();
for ( final Bundle bundle : this.context.getBundles () )
{
if ( bundle.getSymbolicName ().equals ( cid ) )
{
return bundle;
}
}
return null;
}
private static void addContribution(TreeMap<String, String> set, IConfigurationElement configElt) {
IContributor contributor = configElt.getContributor();
try {
if (contributor == null) {
throw new IllegalArgumentException("Null contributor");
}
String pluginId = configElt.getAttribute(PLUGIN_ID);
if (pluginId == null) {
throw new IllegalArgumentException("Missing '" + PLUGIN_ID + "'");
}
String libPathAsString = configElt.getAttribute(LIBRARY_PATH);
if (libPathAsString == null) {
throw new IllegalArgumentException("Missing '" + LIBRARY_PATH + "'");
}
libPathAsString = resolveRelativePath(contributor, libPathAsString);
if (libPathAsString == null) {
throw new IllegalArgumentException("Failed to resolve library path for: " + pluginId);
}
if (set.containsKey(pluginId)) {
throw new IllegalArgumentException("Duplicated '" + pluginId + "' contribution.");
}
set.put(pluginId, libPathAsString);
} catch (Throwable e) {
String cName = contributor != null ? contributor.getName() : "unknown contributor";
String message = "Failed to read contribution for '" + EXTENSION_POINT_ID
+ "' extension point from " + cName;
FindbugsPlugin.getDefault().logException(e, message);
}
}
/**
* Load the plugins from the extension point, and fill the lists of entries.
*/
protected void load(String extensionId) {
IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint point = registry.getExtensionPoint(extensionId);
if (null == point) {
return;
}
for (IExtension extension: point.getExtensions()) {
IContributor contrib = extension.getContributor();
String bundleId = contrib.getName();
// ... and for each elements
for (IConfigurationElement element :
extension.getConfigurationElements()) {
// obtain an object on the entry
ContributionEntry<T> entry = buildEntry(bundleId, element);
String entryId = entry.getId();
if (Strings.isNullOrEmpty(entryId)) {
LOG.warn("Empty entry id in {} for {}", bundleId, extensionId);
}
entries.put(entryId, entry);
// Try to instantiate the contribution and install it.
try {
T plugin = entry.prepareInstance();
installContribution(entryId, plugin);
} catch (CoreException err) {
reportException(entryId, err);
throw new RuntimeException(err);
}
}
}
}
/**
* Log the status and inform the user about a misbehaving extension.
*
* @param descriptor the descriptor of the misbehaving extension
* @param status a status object that will be logged
*/
void informUser(CompletionProposalComputerDescriptor descriptor, IStatus status) {
JavaPlugin.log(status);
String title= JavaTextMessages.CompletionProposalComputerRegistry_error_dialog_title;
CompletionProposalCategory category= descriptor.getCategory();
IContributor culprit= descriptor.getContributor();
Set<String> affectedPlugins= getAffectedContributors(category, culprit);
final String avoidHint;
final String culpritName= culprit == null ? null : culprit.getName();
if (affectedPlugins.isEmpty())
avoidHint= Messages.format(JavaTextMessages.CompletionProposalComputerRegistry_messageAvoidanceHint, new Object[] {culpritName, category.getDisplayName()});
else
avoidHint= Messages.format(JavaTextMessages.CompletionProposalComputerRegistry_messageAvoidanceHintWithWarning, new Object[] {culpritName, category.getDisplayName(), toString(affectedPlugins)});
String message= status.getMessage();
// inlined from MessageDialog.openError
MessageDialog dialog = new MessageDialog(JavaPlugin.getActiveWorkbenchShell(), title, null /* default image */, message, MessageDialog.ERROR, new String[] { IDialogConstants.OK_LABEL }, 0) {
@Override
protected Control createCustomArea(Composite parent) {
Link link= new Link(parent, SWT.NONE);
link.setText(avoidHint);
link.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
PreferencesUtil.createPreferenceDialogOn(getShell(), "org.eclipse.jdt.ui.preferences.CodeAssistPreferenceAdvanced", null, null).open(); //$NON-NLS-1$
}
});
GridData gridData= new GridData(SWT.FILL, SWT.BEGINNING, true, false);
gridData.widthHint= this.getMinimumMessageWidth();
link.setLayoutData(gridData);
return link;
}
};
dialog.open();
}
/**
* Returns the names of contributors affected by disabling a category.
*
* @param category the category that would be disabled
* @param culprit the culprit plug-in, which is not included in the returned list
* @return the names of the contributors other than <code>culprit</code> that contribute to <code>category</code> (element type: {@link String})
*/
private Set<String> getAffectedContributors(CompletionProposalCategory category, IContributor culprit) {
Set<String> affectedPlugins= new HashSet<String>();
for (Iterator<CompletionProposalComputerDescriptor> it= getProposalComputerDescriptors().iterator(); it.hasNext();) {
CompletionProposalComputerDescriptor desc= it.next();
CompletionProposalCategory cat= desc.getCategory();
if (cat.equals(category)) {
IContributor contributor= desc.getContributor();
if (contributor != null && !culprit.equals(contributor))
affectedPlugins.add(contributor.getName());
}
}
return affectedPlugins;
}
/**
* Returns the contributor of the described extension.
*
* @return the contributor of the described extension
*/
IContributor getContributor() {
try {
return fElement.getContributor();
} catch (InvalidRegistryObjectException e) {
return null;
}
}
public IContributor getContributor( ) throws InvalidRegistryObjectException
{
if( bundle == null )
{
IExtension declaringExtn = getDeclaringExtension();
if( declaringExtn != null )
return declaringExtn.getContributor();
return null;
}
return bundle.getContributor( );
}
public boolean addContribution( InputStream is, IContributor contributor,
boolean persist, String name, ResourceBundle translationBundle,
Object token ) throws IllegalArgumentException
{
throw new UnsupportedOperationException(
"addContribution is not implemented yet" );
}
public IExtensionPoint[] getExtensionPoints( IContributor contributor )
{
for ( Bundle bundle : bundles.values() )
{
if ( bundle.getContributor( ) == contributor )
{
return bundle.getExtensionPoints( );
}
}
return new IExtensionPoint[]{};
}
public IExtension[] getExtensions( IContributor contributor )
{
for ( Bundle bundle : bundles.values() )
{
if ( bundle.getContributor( ) == contributor )
{
return bundle.getExtensions( );
}
}
return new IExtension[]{};
}
private static void addContribution(Map<String, List<QuickFixContribution>> set, final IConfigurationElement configElt) {
IContributor contributor = null;
try {
contributor = configElt.getContributor();
if (contributor == null) {
throw new IllegalArgumentException("Null contributor");
}
String clazzFqn = configElt.getAttribute(CLASS_FQN);
if (isEmpty(clazzFqn)) {
throw new IllegalArgumentException("Missing '" + CLASS_FQN + "' attribute");
}
String label = configElt.getAttribute(LABEL);
if (isEmpty(label)) {
throw new IllegalArgumentException("Missing '" + LABEL + "' attribute");
}
String pattern = configElt.getAttribute(PATTERN);
if (isEmpty(pattern)) {
throw new IllegalArgumentException("Missing '" + PATTERN + "' attribute");
}
String arg = configElt.getAttribute(ARGUMENTS);
Set<String> args;
if (arg == null) {
args = Collections.emptySet();
} else {
args = new HashSet<>();
for (String string : arg.split(",\\s*")) {
args.add(string);
}
}
QuickFixContribution qf = createQuickFix(configElt, clazzFqn, label, pattern, args);
List<QuickFixContribution> list = set.get(pattern);
if (list == null) {
list = new ArrayList<>();
set.put(pattern, list);
}
if (list.contains(qf)) {
throw new IllegalArgumentException("Duplicated quick fix contribution for pattern '"
+ pattern + "': " + qf + ".");
}
list.add(qf);
} catch (Throwable e) {
String cName = contributor != null ? contributor.getName() : "unknown contributor";
String message = "Failed to read contribution for '" + EXTENSION_POINT_ID
+ "' extension point from " + cName;
FindbugsPlugin.getDefault().logException(e, message);
}
}
public IContributor getContributor() throws InvalidRegistryObjectException {
return null;
}
protected void setContributor(IContributor contributor) {
this.contributor = contributor;
}
public IContributor getContributor( ) throws InvalidRegistryObjectException
{
return bundle.getContributor( );
}
public IContributor getContributor( )
{
return contributor;
}
public IContributor getContributor( ) throws InvalidRegistryObjectException
{
return bundle.getContributor( );
}