summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/net/sf/cglib/proxy/AdvancedEnhancer.java
diff options
context:
space:
mode:
Diffstat (limited to 'platform/platform-impl/src/net/sf/cglib/proxy/AdvancedEnhancer.java')
-rw-r--r--platform/platform-impl/src/net/sf/cglib/proxy/AdvancedEnhancer.java182
1 files changed, 21 insertions, 161 deletions
diff --git a/platform/platform-impl/src/net/sf/cglib/proxy/AdvancedEnhancer.java b/platform/platform-impl/src/net/sf/cglib/proxy/AdvancedEnhancer.java
index 5b22e706508a..a2f21c3d0018 100644
--- a/platform/platform-impl/src/net/sf/cglib/proxy/AdvancedEnhancer.java
+++ b/platform/platform-impl/src/net/sf/cglib/proxy/AdvancedEnhancer.java
@@ -15,7 +15,7 @@
*/
package net.sf.cglib.proxy;
-import com.intellij.ide.plugins.PluginManager;
+import com.intellij.ide.plugins.PluginManagerCore;
import com.intellij.ide.plugins.cl.PluginClassLoader;
import com.intellij.util.ReflectionUtil;
import net.sf.cglib.core.*;
@@ -26,6 +26,7 @@ import org.objectweb.asm.Type;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.*;
/**
@@ -61,6 +62,7 @@ import java.util.*;
* <code>java.lang.reflect.Proxy</code>, see the {@link Proxy} class.
*/
+@SuppressWarnings("StaticFieldReferencedViaSubclass")
public class AdvancedEnhancer extends AbstractClassGenerator
{
private static final CallbackFilter ALL_ZERO = new CallbackFilter(){
@@ -125,7 +127,7 @@ public class AdvancedEnhancer extends AbstractClassGenerator
/** Internal interface, only public due to ClassLoader issues. */
public interface EnhancerKey {
- public Object newInstance(String type,
+ Object newInstance(String type,
String[] interfaces,
CallbackFilter filter,
Type[] callbackTypes,
@@ -250,18 +252,6 @@ public class AdvancedEnhancer extends AbstractClassGenerator
}
/**
- * Set the single type of {@link Callback} to use.
- * This may be used instead of {@link #setCallback} when calling
- * {@link #createClass}, since it may not be possible to have
- * an array of actual callback instances.
- * @param callbackType the type of callback to use for all methods
- * @see #setCallbackTypes
- */
- public void setCallbackType(Class callbackType) {
- setCallbackTypes(new Class[]{ callbackType });
- }
-
- /**
* Set the array of callback types to use.
* This may be used instead of {@link #setCallbacks} when calling
* {@link #createClass}, since it may not be possible to have
@@ -315,27 +305,6 @@ public class AdvancedEnhancer extends AbstractClassGenerator
}
}
- /**
- * Generate a new class if necessary and return it without creating a new instance.
- * This ignores any callbacks that have been set.
- * To create a new instance you will have to use reflection, and methods
- * called during the constructor will not be intercepted. To avoid this problem,
- * use the multi-arg <code>create</code> method.
- * @see #create(Class[], Object[])
- */
- public Class createClass() {
- classOnly = true;
- return (Class)createHelper();
- }
-
- /**
- * Insert a static serialVersionUID field into the generated class.
- * @param sUID the field value, or null to avoid generating field.
- */
- public void setSerialVersionUID(Long sUID) {
- serialVersionUID = sUID;
- }
-
private void validate() {
if (classOnly ^ (callbacks == null)) {
if (classOnly) {
@@ -401,7 +370,7 @@ public class AdvancedEnhancer extends AbstractClassGenerator
for (final Class anInterface : interfaces) {
final ClassLoader loader = anInterface.getClassLoader();
if (loader instanceof PluginClassLoader) {
- final int order = PluginManager.getPluginLoadingOrder(((PluginClassLoader)loader).getPluginId());
+ final int order = PluginManagerCore.getPluginLoadingOrder(((PluginClassLoader)loader).getPluginId());
if (maxIndex < order) {
maxIndex = order;
bestLoader = loader;
@@ -413,7 +382,7 @@ public class AdvancedEnhancer extends AbstractClassGenerator
if (superclass != null) {
superLoader = superclass.getClassLoader();
if (superLoader instanceof PluginClassLoader &&
- maxIndex < PluginManager.getPluginLoadingOrder(((PluginClassLoader)superLoader).getPluginId())) {
+ maxIndex < PluginManagerCore.getPluginLoadingOrder(((PluginClassLoader)superLoader).getPluginId())) {
return superLoader;
}
}
@@ -426,24 +395,6 @@ public class AdvancedEnhancer extends AbstractClassGenerator
sig.getDescriptor());
}
- /**
- * Finds all of the methods that will be extended by an
- * Enhancer-generated class using the specified superclass and
- * interfaces. This can be useful in building a list of Callback
- * objects. The methods are added to the end of the given list. Due
- * to the subclassing nature of the classes generated by Enhancer,
- * the methods are guaranteed to be non-static, non-final, and
- * non-private. Each method signature will only occur once, even if
- * it occurs in multiple classes.
- * @param superclass the class that will be extended, or null
- * @param interfaces the list of interfaces that will be implemented, or null
- * @param methods the list into which to copy the applicable methods
- */
- public static void getMethods(Class superclass, Class[] interfaces, List<Method> methods)
- {
- getMethods(superclass, interfaces, methods, null, null);
- }
-
private static void getMethods(Class superclass, Class[] interfaces, List<Method> methods, List<Method> interfaceMethods, Set forcePublic)
{
ReflectUtils.addAllMethods(superclass, methods);
@@ -520,6 +471,9 @@ public class AdvancedEnhancer extends AbstractClassGenerator
}
final Map<Method, MethodInfo> methodInfoMap = new HashMap<Method, MethodInfo>();
for (Method method : actualMethods) {
+ if (isJdk8DefaultMethod(method)) {
+ continue;
+ }
int modifiers =
Constants.ACC_FINAL | (method.getModifiers() & ~Constants.ACC_ABSTRACT & ~Constants.ACC_NATIVE & ~Constants.ACC_SYNCHRONIZED);
if (forcePublic.contains(MethodWrapper.create(method))) {
@@ -551,6 +505,11 @@ public class AdvancedEnhancer extends AbstractClassGenerator
e.end_class();
}
+ private static boolean isJdk8DefaultMethod(Method method) {
+ return ((method.getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) ==
+ Modifier.PUBLIC) && method.getDeclaringClass().isInterface();
+ }
+
private static void removeAllCovariantMethods(final List<Method> actualMethods, final Method method, final Map<Method, Method> covariantMethods) {
if ((method.getModifiers() & Constants.ACC_SYNTHETIC) != 0) {
return;
@@ -618,57 +577,6 @@ public class AdvancedEnhancer extends AbstractClassGenerator
}
}
- /**
- * Call this method to register the {@link Callback} array to use before
- * creating a new instance of the generated class via reflection. If you are using
- * an instance of <code>Enhancer</code> or the {@link Factory} interface to create
- * new instances, this method is unnecessary. Its primary use is for when you want to
- * cache and reuse a generated class yourself, and the generated class does
- * <i>not</i> implement the {@link Factory} interface.
- * <p>
- * Note that this method only registers the callbacks on the current thread.
- * If you want to register callbacks for instances created by multiple threads,
- * use {@link #registerStaticCallbacks}.
- * <p>
- * The registered callbacks are overwritten and subsequently cleared
- * when calling any of the <code>create</code> methods (such as
- * {@link #create}).
- * @param generatedClass a class previously created by {@link Enhancer}
- * @param callbacks the array of callbacks to use when instances of the generated
- * class are created
- * @see #setUseFactory
- */
- public static void registerCallbacks(Class generatedClass, Callback[] callbacks) {
- setThreadCallbacks(generatedClass, callbacks);
- }
-
- /**
- * Similar to {@link #registerCallbacks}, but suitable for use
- * when multiple threads will be creating instances of the generated class.
- * The thread-level callbacks will always override the static callbacks.
- * Static callbacks are never cleared.
- * @param generatedClass a class previously created by {@link Enhancer}
- * @param callbacks the array of callbacks to use when instances of the generated
- * class are created
- */
- public static void registerStaticCallbacks(Class generatedClass, Callback[] callbacks) {
- setCallbacksHelper(generatedClass, callbacks, SET_STATIC_CALLBACKS_NAME);
- }
-
- /**
- * Determine if a class was generated using <code>Enhancer</code>.
- * @param type any class
- * @return whether the class was generated using <code>Enhancer</code>
- */
- public static boolean isEnhanced(Class type) {
- try {
- getCallbacksSetter(type, SET_THREAD_CALLBACKS_NAME);
- return true;
- } catch (NoSuchMethodException e) {
- return false;
- }
- }
-
private static void setThreadCallbacks(Class type, Callback[] callbacks) {
setCallbacksHelper(type, callbacks, SET_THREAD_CALLBACKS_NAME);
}
@@ -710,54 +618,6 @@ public class AdvancedEnhancer extends AbstractClassGenerator
}
}
- /**
- * Helper method to create an intercepted object.
- * For finer control over the generated instance, use a new instance of <code>Enhancer</code>
- * instead of this static method.
- * @param type class to extend or interface to implement
- * @param callback the callback to use for all methods
- */
- public static Object create(Class type, Callback callback) {
- Enhancer e = new Enhancer();
- e.setSuperclass(type);
- e.setCallback(callback);
- return e.create();
- }
-
- /**
- * Helper method to create an intercepted object.
- * For finer control over the generated instance, use a new instance of <code>Enhancer</code>
- * instead of this static method.
- * @param type class to extend or interface to implement
- * @param interfaces array of interfaces to implement, or null
- * @param callback the callback to use for all methods
- */
- public static Object create(Class superclass, Class interfaces[], Callback callback) {
- Enhancer e = new Enhancer();
- e.setSuperclass(superclass);
- e.setInterfaces(interfaces);
- e.setCallback(callback);
- return e.create();
- }
-
- /**
- * Helper method to create an intercepted object.
- * For finer control over the generated instance, use a new instance of <code>Enhancer</code>
- * instead of this static method.
- * @param type class to extend or interface to implement
- * @param interfaces array of interfaces to implement, or null
- * @param filter the callback filter to use when generating a new class
- * @param callbacks callback implementations to use for the enhanced object
- */
- public static Object create(Class superclass, Class[] interfaces, CallbackFilter filter, Callback[] callbacks) {
- Enhancer e = new Enhancer();
- e.setSuperclass(superclass);
- e.setInterfaces(interfaces);
- e.setCallbackFilter(filter);
- e.setCallbacks(callbacks);
- return e.create();
- }
-
private void emitConstructors(ClassEmitter ce, List constructors) {
boolean seenNull = false;
for (final Object constructor1 : constructors) {
@@ -791,7 +651,7 @@ public class AdvancedEnhancer extends AbstractClassGenerator
return keys;
}
- private void emitGetCallback(ClassEmitter ce, int[] keys) {
+ private static void emitGetCallback(ClassEmitter ce, int[] keys) {
final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, GET_CALLBACK, null);
e.load_this();
e.invoke_static_this(BIND_CALLBACKS);
@@ -866,14 +726,14 @@ public class AdvancedEnhancer extends AbstractClassGenerator
e.end_method();
}
- private void emitNewInstanceCallbacks(ClassEmitter ce) {
+ private static void emitNewInstanceCallbacks(ClassEmitter ce) {
CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, NEW_INSTANCE, null);
e.load_arg(0);
e.invoke_static_this(SET_THREAD_CALLBACKS);
emitCommonNewInstance(e);
}
- private void emitCommonNewInstance(CodeEmitter e) {
+ private static void emitCommonNewInstance(CodeEmitter e) {
e.new_instance_this();
e.dup();
e.invoke_constructor_this();
@@ -905,7 +765,7 @@ public class AdvancedEnhancer extends AbstractClassGenerator
emitCommonNewInstance(e);
}
- private void emitNewInstanceMultiarg(ClassEmitter ce, List constructors) {
+ private static void emitNewInstanceMultiarg(ClassEmitter ce, List constructors) {
final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, MULTIARG_NEW_INSTANCE, null);
e.load_arg(2);
e.invoke_static_this(SET_THREAD_CALLBACKS);
@@ -1034,7 +894,7 @@ public class AdvancedEnhancer extends AbstractClassGenerator
se.end_method();
}
- private void emitSetThreadCallbacks(ClassEmitter ce) {
+ private static void emitSetThreadCallbacks(ClassEmitter ce) {
CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC | Constants.ACC_STATIC,
SET_THREAD_CALLBACKS,
null);
@@ -1045,7 +905,7 @@ public class AdvancedEnhancer extends AbstractClassGenerator
e.end_method();
}
- private void emitSetStaticCallbacks(ClassEmitter ce) {
+ private static void emitSetStaticCallbacks(ClassEmitter ce) {
CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC | Constants.ACC_STATIC,
SET_STATIC_CALLBACKS,
null);
@@ -1055,7 +915,7 @@ public class AdvancedEnhancer extends AbstractClassGenerator
e.end_method();
}
- private void emitCurrentCallback(CodeEmitter e, int index) {
+ private static void emitCurrentCallback(CodeEmitter e, int index) {
e.load_this();
e.getfield(getCallbackField(index));
e.dup();