1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
/*******************************************************************************
* Copyright (c) 2008-2010 Sonatype, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Sonatype, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.m2e.core.ui.internal;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.ui.IActionFilter;
/**
* @author Eugene Kuleshov
*/
@SuppressWarnings("unchecked")
public class MavenAdapterFactory implements IAdapterFactory {
private static final Class[] ADAPTER_TYPES = new Class[] { IActionFilter.class };
public Class[] getAdapterList() {
return ADAPTER_TYPES;
}
public Object getAdapter(final Object adaptable, Class adapterType) {
return new IActionFilter() {
public boolean testAttribute(Object target, String name, String value) {
return "label".equals(name) // //$NON-NLS-1$
&& value.equals(getStub(adaptable, LabelProviderStub.class).getLabel());
}
private <T> T getStub(final Object o, Class<T> type) {
// can't use IWorkbenchAdapter here because it can cause recursion
return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {type}, //
new InvocationHandler() {
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
try {
Method method = o.getClass().getDeclaredMethod(m.getName(), m.getParameterTypes());
return method.invoke(o, args);
} catch(RuntimeException ex) {
return null;
} catch(Exception ex) {
return null;
}
}
});
}
};
}
/**
* A stub interface to access org.eclipse.jdt.internal.ui.packageview.ClassPathContainer#getLabel()
*/
public interface LabelProviderStub {
public String getLabel();
}
}
|