diff options
author | nickl- <github@jigsoft.co.za> | 2017-10-30 20:37:23 +0200 |
---|---|---|
committer | nickl- <github@jigsoft.co.za> | 2017-10-30 20:37:23 +0200 |
commit | 62851d0af30cdb0c54046627585451a468aacd2a (patch) | |
tree | fd7632ac39246a9598cb5bf6176c4bf0bcb21744 /src/main/javassist/util | |
parent | 45b4c55361eef93542db8014e3ef8941169c97bd (diff) | |
download | javassist-62851d0af30cdb0c54046627585451a468aacd2a.tar.gz javassist-62851d0af30cdb0c54046627585451a468aacd2a.zip |
Source walk/Spring clean/Parameterize/Enhance et.al.
The following were applied during multiple itterations through the source.
* Parameterize raw types.
* Mark unused members.
* Annotate override and deprecated methods.
* Convert loops to enhance for loop.
* Remove redundant else statements.
* Widening collection type references.
* Optimize code for improved readability.
* Squash compiler warnings.
* Identify smells.
Diffstat (limited to 'src/main/javassist/util')
-rw-r--r-- | src/main/javassist/util/HotSwapAgent.java | 6 | ||||
-rw-r--r-- | src/main/javassist/util/HotSwapper.java | 45 | ||||
-rw-r--r-- | src/main/javassist/util/proxy/FactoryHelper.java | 17 | ||||
-rw-r--r-- | src/main/javassist/util/proxy/ProxyFactory.java | 225 | ||||
-rw-r--r-- | src/main/javassist/util/proxy/ProxyObject.java | 1 | ||||
-rw-r--r-- | src/main/javassist/util/proxy/ProxyObjectInputStream.java | 10 | ||||
-rw-r--r-- | src/main/javassist/util/proxy/ProxyObjectOutputStream.java | 9 | ||||
-rw-r--r-- | src/main/javassist/util/proxy/RuntimeSupport.java | 33 | ||||
-rw-r--r-- | src/main/javassist/util/proxy/SerializedProxy.java | 26 |
9 files changed, 180 insertions, 192 deletions
diff --git a/src/main/javassist/util/HotSwapAgent.java b/src/main/javassist/util/HotSwapAgent.java index 684f8540..c3773197 100644 --- a/src/main/javassist/util/HotSwapAgent.java +++ b/src/main/javassist/util/HotSwapAgent.java @@ -106,10 +106,10 @@ public class HotSwapAgent { /** * Redefines a class. */ - public static void redefine(Class oldClass, CtClass newClass) + public static void redefine(Class<?> oldClass, CtClass newClass) throws NotFoundException, IOException, CannotCompileException { - Class[] old = { oldClass }; + Class<?>[] old = { oldClass }; CtClass[] newClasses = { newClass }; redefine(old, newClasses); } @@ -117,7 +117,7 @@ public class HotSwapAgent { /** * Redefines classes. */ - public static void redefine(Class[] oldClasses, CtClass[] newClasses) + public static void redefine(Class<?>[] oldClasses, CtClass[] newClasses) throws NotFoundException, IOException, CannotCompileException { startAgent(); diff --git a/src/main/javassist/util/HotSwapper.java b/src/main/javassist/util/HotSwapper.java index f47c3285..a7ab052e 100644 --- a/src/main/javassist/util/HotSwapper.java +++ b/src/main/javassist/util/HotSwapper.java @@ -80,7 +80,7 @@ class Trigger { public class HotSwapper { private VirtualMachine jvm; private MethodEntryRequest request; - private Map newClassFiles; + private Map<ReferenceType,byte[]> newClassFiles; private Trigger trigger; @@ -113,23 +113,20 @@ public class HotSwapper { AttachingConnector connector = (AttachingConnector)findConnector("com.sun.jdi.SocketAttach"); - Map arguments = connector.defaultArguments(); - ((Connector.Argument)arguments.get("hostname")).setValue(HOST_NAME); - ((Connector.Argument)arguments.get("port")).setValue(port); + Map<String,Connector.Argument> arguments = connector.defaultArguments(); + arguments.get("hostname").setValue(HOST_NAME); + arguments.get("port").setValue(port); jvm = connector.attach(arguments); EventRequestManager manager = jvm.eventRequestManager(); request = methodEntryRequests(manager, TRIGGER_NAME); } private Connector findConnector(String connector) throws IOException { - List connectors = Bootstrap.virtualMachineManager().allConnectors(); - Iterator iter = connectors.iterator(); - while (iter.hasNext()) { - Connector con = (Connector)iter.next(); - if (con.name().equals(connector)) { + List<Connector> connectors = Bootstrap.virtualMachineManager().allConnectors(); + + for (Connector con:connectors) + if (con.name().equals(connector)) return con; - } - } throw new IOException("Not found: " + connector); } @@ -145,6 +142,7 @@ public class HotSwapper { /* Stops triggering a hotswapper when reload() is called. */ + @SuppressWarnings("unused") private void deleteEventRequest(EventRequestManager manager, MethodEntryRequest request) { manager.deleteEventRequest(request); @@ -158,7 +156,7 @@ public class HotSwapper { */ public void reload(String className, byte[] classFile) { ReferenceType classtype = toRefType(className); - Map map = new HashMap(); + Map<ReferenceType,byte[]> map = new HashMap<ReferenceType,byte[]>(); map.put(classtype, classFile); reload2(map, className); } @@ -171,14 +169,11 @@ public class HotSwapper { * is <code>String</code> and the type of the * class files is <code>byte[]</code>. */ - public void reload(Map classFiles) { - Set set = classFiles.entrySet(); - Iterator it = set.iterator(); - Map map = new HashMap(); + public void reload(Map<String,byte[]> classFiles) { + Map<ReferenceType,byte[]> map = new HashMap<ReferenceType,byte[]>(); String className = null; - while (it.hasNext()) { - Map.Entry e = (Map.Entry)it.next(); - className = (String)e.getKey(); + for (Map.Entry<String,byte[]> e:classFiles.entrySet()) { + className = e.getKey(); map.put(toRefType(className), e.getValue()); } @@ -187,21 +182,20 @@ public class HotSwapper { } private ReferenceType toRefType(String className) { - List list = jvm.classesByName(className); + List<ReferenceType> list = jvm.classesByName(className); if (list == null || list.isEmpty()) throw new RuntimeException("no such class: " + className); - else - return (ReferenceType)list.get(0); + return list.get(0); } - private void reload2(Map map, String msg) { + private void reload2(Map<ReferenceType,byte[]> map, String msg) { synchronized (trigger) { startDaemon(); newClassFiles = map; request.enable(); trigger.doSwap(); request.disable(); - Map ncf = newClassFiles; + Map<ReferenceType,byte[]> ncf = newClassFiles; if (ncf != null) { newClassFiles = null; throw new RuntimeException("failed to reload: " + msg); @@ -216,6 +210,7 @@ public class HotSwapper { e.printStackTrace(System.err); } + @Override public void run() { EventSet events = null; try { @@ -249,7 +244,7 @@ public class HotSwapper { } void hotswap() { - Map map = newClassFiles; + Map<ReferenceType,byte[]> map = newClassFiles; jvm.redefineClasses(map); newClassFiles = null; } diff --git a/src/main/javassist/util/proxy/FactoryHelper.java b/src/main/javassist/util/proxy/FactoryHelper.java index 34af79d3..1fb63b65 100644 --- a/src/main/javassist/util/proxy/FactoryHelper.java +++ b/src/main/javassist/util/proxy/FactoryHelper.java @@ -39,11 +39,9 @@ public class FactoryHelper { * * @throws RuntimeException if a given type is not a primitive type. */ - public static final int typeIndex(Class type) { - Class[] list = primitiveTypes; - int n = list.length; - for (int i = 0; i < n; i++) - if (list[i] == type) + public static final int typeIndex(Class<?> type) { + for (int i = 0; i < primitiveTypes.length; i++) + if (primitiveTypes[i] == type) return i; throw new RuntimeException("bad type:" + type.getName()); @@ -52,7 +50,7 @@ public class FactoryHelper { /** * <code>Class</code> objects representing primitive types. */ - public static final Class[] primitiveTypes = { + public static final Class<?>[] primitiveTypes = { Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE, Void.TYPE }; @@ -108,7 +106,7 @@ public class FactoryHelper { * * @see #toClass(ClassFile,ClassLoader,ProtectionDomain) */ - public static Class toClass(ClassFile cf, ClassLoader loader) + public static Class<?> toClass(ClassFile cf, ClassLoader loader) throws CannotCompileException { return toClass(cf, loader, null); @@ -120,15 +118,14 @@ public class FactoryHelper { * @param domain if it is null, a default domain is used. * @since 3.3 */ - public static Class toClass(ClassFile cf, ClassLoader loader, ProtectionDomain domain) + public static Class<?> toClass(ClassFile cf, ClassLoader loader, ProtectionDomain domain) throws CannotCompileException { try { byte[] b = toBytecode(cf); if (ProxyFactory.onlyPublicMethods) return DefineClassHelper.toPublicClass(cf.getName(), b); - else - return DefineClassHelper.toClass(cf.getName(), loader, domain, b); + return DefineClassHelper.toClass(cf.getName(), loader, domain, b); } catch (IOException e) { throw new CannotCompileException(e); diff --git a/src/main/javassist/util/proxy/ProxyFactory.java b/src/main/javassist/util/proxy/ProxyFactory.java index 35c6c11e..6ee0c497 100644 --- a/src/main/javassist/util/proxy/ProxyFactory.java +++ b/src/main/javassist/util/proxy/ProxyFactory.java @@ -24,6 +24,7 @@ import java.lang.reflect.Member; import java.lang.reflect.Modifier; import java.security.ProtectionDomain; import java.util.*; +import java.lang.ref.Reference; import java.lang.ref.WeakReference; import javassist.CannotCompileException; @@ -153,17 +154,17 @@ import javassist.bytecode.*; * @author Andrew Dinn */ public class ProxyFactory { - private Class superClass; - private Class[] interfaces; + private Class<?> superClass; + private Class<?>[] interfaces; private MethodFilter methodFilter; private MethodHandler handler; // retained for legacy usage - private List signatureMethods; + private List<Map.Entry<String,Method>> signatureMethods; private boolean hasGetHandler; private byte[] signature; private String classname; private String basename; private String superName; - private Class thisClass; + private Class<?> thisClass; /** * per factory setting initialised from current setting for useCache but able to be reset before each create call */ @@ -208,7 +209,7 @@ public class ProxyFactory { */ public String writeDirectory; - private static final Class OBJECT_TYPE = Object.class; + private static final Class<?> OBJECT_TYPE = Object.class; private static final String HOLDER = "_methods_"; private static final String HOLDER_TYPE = "[Ljava/lang/reflect/Method;"; @@ -310,14 +311,15 @@ public class ProxyFactory { factoryWriteReplace = useWriteReplace; } - private static WeakHashMap proxyCache = new WeakHashMap(); + private static Map<ClassLoader,Map<String,ProxyDetails>> proxyCache = + new WeakHashMap<ClassLoader,Map<String,ProxyDetails>>(); /** * determine if a class is a javassist proxy class * @param cl * @return true if the class is a javassist proxy class otherwise false */ - public static boolean isProxyClass(Class cl) + public static boolean isProxyClass(Class<?> cl) { // all proxies implement Proxy or ProxyObject. nothing else should. return (Proxy.class.isAssignableFrom(cl)); @@ -339,17 +341,17 @@ public class ProxyFactory { * a hexadecimal string representation of the signature bit sequence. this string also forms part * of the proxy class name. */ - WeakReference proxyClass; + Reference<Class<?>> proxyClass; /** * a flag which is true this class employs writeReplace to perform serialization of its instances * and false if serialization must employ of a ProxyObjectOutputStream and ProxyObjectInputStream */ boolean isUseWriteReplace; - ProxyDetails(byte[] signature, Class proxyClass, boolean isUseWriteReplace) + ProxyDetails(byte[] signature, Class<?> proxyClass, boolean isUseWriteReplace) { this.signature = signature; - this.proxyClass = new WeakReference(proxyClass); + this.proxyClass = new WeakReference<Class<?>>(proxyClass); this.isUseWriteReplace = isUseWriteReplace; } } @@ -374,7 +376,7 @@ public class ProxyFactory { /** * Sets the super class of a proxy class. */ - public void setSuperclass(Class clazz) { + public void setSuperclass(Class<?> clazz) { superClass = clazz; // force recompute of signature signature = null; @@ -385,12 +387,12 @@ public class ProxyFactory { * * @since 3.4 */ - public Class getSuperclass() { return superClass; } + public Class<?> getSuperclass() { return superClass; } /** * Sets the interfaces of a proxy class. */ - public void setInterfaces(Class[] ifs) { + public void setInterfaces(Class<?>[] ifs) { interfaces = ifs; // force recompute of signature signature = null; @@ -401,7 +403,7 @@ public class ProxyFactory { * * @since 3.4 */ - public Class[] getInterfaces() { return interfaces; } + public Class<?>[] getInterfaces() { return interfaces; } /** * Sets a filter that selects the methods that will be controlled by a handler. @@ -415,7 +417,7 @@ public class ProxyFactory { /** * Generates a proxy class using the current filter. */ - public Class createClass() { + public Class<?> createClass() { if (signature == null) { computeSignature(methodFilter); } @@ -425,7 +427,7 @@ public class ProxyFactory { /** * Generates a proxy class using the supplied filter. */ - public Class createClass(MethodFilter filter) { + public Class<?> createClass(MethodFilter filter) { computeSignature(filter); return createClass1(); } @@ -436,20 +438,20 @@ public class ProxyFactory { * @param signature * @return */ - Class createClass(byte[] signature) + Class<?> createClass(byte[] signature) { installSignature(signature); return createClass1(); } - private Class createClass1() { - Class result = thisClass; + private Class<?> createClass1() { + Class<?> result = thisClass; if (result == null) { ClassLoader cl = getClassLoader(); synchronized (proxyCache) { if (factoryUseCache) createClass2(cl); - else + else createClass3(cl); result = thisClass; @@ -465,7 +467,7 @@ public class ProxyFactory { { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - public String getKey(Class superClass, Class[] interfaces, byte[] signature, boolean useWriteReplace) + public String getKey(Class<?> superClass, Class<?>[] interfaces, byte[] signature, boolean useWriteReplace) { StringBuffer sbuf = new StringBuffer(); if (superClass != null){ @@ -498,16 +500,16 @@ public class ProxyFactory { * reducing concrrency. */ // synchronized (proxyCache) { - HashMap cacheForTheLoader = (HashMap)proxyCache.get(cl); + Map<String,ProxyDetails> cacheForTheLoader = proxyCache.get(cl); ProxyDetails details; if (cacheForTheLoader == null) { - cacheForTheLoader = new HashMap(); + cacheForTheLoader = new HashMap<String,ProxyDetails>(); proxyCache.put(cl, cacheForTheLoader); } - details = (ProxyDetails)cacheForTheLoader.get(key); + details = cacheForTheLoader.get(key); if (details != null) { - WeakReference reference = details.proxyClass; - thisClass = (Class)reference.get(); + Reference<Class<?>> reference = details.proxyClass; + thisClass = reference.get(); if (thisClass != null) { return; } @@ -553,11 +555,11 @@ public class ProxyFactory { } } - static byte[] getFilterSignature(Class clazz) { + static byte[] getFilterSignature(Class<?> clazz) { return (byte[])getField(clazz, FILTER_SIGNATURE_FIELD); } - private static Object getField(Class clazz, String fieldName) { + private static Object getField(Class<?> clazz, String fieldName) { try { Field f = clazz.getField(fieldName); f.setAccessible(true); @@ -625,12 +627,13 @@ public class ProxyFactory { * * @since 3.4 */ - public static ClassLoaderProvider classLoaderProvider - = new ClassLoaderProvider() { - public ClassLoader get(ProxyFactory pf) { + public static ClassLoaderProvider classLoaderProvider = + new ClassLoaderProvider() { + @Override + public ClassLoader get(ProxyFactory pf) { return pf.getClassLoader0(); } - }; + }; protected ClassLoader getClassLoader() { return classLoaderProvider.get(this); @@ -657,7 +660,7 @@ public class ProxyFactory { } protected ProtectionDomain getDomain() { - Class clazz; + Class<?> clazz; if (superClass != null && !superClass.getName().equals("java.lang.Object")) clazz = superClass; else if (interfaces != null && interfaces.length > 0) @@ -676,7 +679,7 @@ public class ProxyFactory { * @param mh the method handler for the proxy class. * @since 3.4 */ - public Object create(Class[] paramTypes, Object[] args, MethodHandler mh) + public Object create(Class<?>[] paramTypes, Object[] args, MethodHandler mh) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { @@ -691,12 +694,12 @@ public class ProxyFactory { * @param paramTypes parameter types for a constructor. * @param args arguments passed to a constructor. */ - public Object create(Class[] paramTypes, Object[] args) + public Object create(Class<?>[] paramTypes, Object[] args) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { - Class c = createClass(); - Constructor cons = c.getConstructor(paramTypes); + Class<?> c = createClass(); + Constructor<?> cons = c.getConstructor(paramTypes); return cons.newInstance(args); } @@ -710,6 +713,7 @@ public class ProxyFactory { * for each newly created proxy instance. * calling this method will automatically disable caching of classes created by the proxy factory. */ + @Deprecated public void setHandler(MethodHandler mi) { // if we were using the cache and the handler is non-null then we must stop caching if (factoryUseCache && mi != null) { @@ -746,6 +750,7 @@ public class ProxyFactory { private final String sep = "_$$_jvst" + Integer.toHexString(this.hashCode() & 0xfff) + "_"; private int counter = 0; + @Override public String get(String classname) { return classname + sep + Integer.toHexString(counter++); } @@ -784,12 +789,12 @@ public class ProxyFactory { FieldInfo finfo4 = new FieldInfo(pool, SERIAL_VERSION_UID_FIELD, SERIAL_VERSION_UID_TYPE); finfo4.setAccessFlags(AccessFlag.PUBLIC | AccessFlag.STATIC| AccessFlag.FINAL); cf.addField(finfo4); - + // HashMap allMethods = getMethods(superClass, interfaces); // int size = allMethods.size(); makeConstructors(classname, cf, pool, classname); - ArrayList forwarders = new ArrayList(); + List<Find2MethodsArgs> forwarders = new ArrayList<Find2MethodsArgs>(); int s = overrideMethods(cf, pool, classname, forwarders); addClassInitializer(cf, pool, classname, s, forwarders); addSetter(classname, cf, pool); @@ -825,7 +830,7 @@ public class ProxyFactory { if (Modifier.isFinal(superClass.getModifiers())) throw new RuntimeException(superName + " is final"); - + if (basename.startsWith("java.") || onlyPublicMethods) basename = "javassist.util.proxy." + basename.replace('.', '_'); } @@ -834,23 +839,20 @@ public class ProxyFactory { classname = makeProxyName(basename); } - private static Comparator sorter = new Comparator() { - - public int compare(Object o1, Object o2) { - Map.Entry e1 = (Map.Entry)o1; - Map.Entry e2 = (Map.Entry)o2; - String key1 = (String)e1.getKey(); - String key2 = (String)e2.getKey(); - return key1.compareTo(key2); - } - }; + private static Comparator<Map.Entry<String,Method>> sorter = + new Comparator<Map.Entry<String,Method>>() { + @Override + public int compare(Map.Entry<String,Method> e1, Map.Entry<String,Method> e2) { + return e1.getKey().compareTo(e2.getKey()); + } + }; private void makeSortedMethodList() { checkClassAndSuperName(); hasGetHandler = false; // getMethods() may set this to true. - HashMap allMethods = getMethods(superClass, interfaces); - signatureMethods = new ArrayList(allMethods.entrySet()); + Map<String,Method> allMethods = getMethods(superClass, interfaces); + signatureMethods = new ArrayList<Map.Entry<String,Method>>(allMethods.entrySet()); Collections.sort(signatureMethods, sorter); } @@ -863,8 +865,7 @@ public class ProxyFactory { signature = new byte[maxBytes]; for (int idx = 0; idx < l; idx++) { - Map.Entry e = (Map.Entry)signatureMethods.get(idx); - Method m = (Method)e.getValue(); + Method m = signatureMethods.get(idx).getValue(); int mod = m.getModifiers(); if (!Modifier.isFinal(mod) && !Modifier.isStatic(mod) && isVisible(mod, basename, m) && (filter == null || filter.isHandled(m))) { @@ -888,14 +889,12 @@ public class ProxyFactory { private boolean testBit(byte[] signature, int idx) { int byteIdx = idx >> 3; - if (byteIdx > signature.length) { + if (byteIdx > signature.length) return false; - } else { - int bitIdx = idx & 0x7; - int mask = 0x1 << bitIdx; - int sigByte = signature[byteIdx]; - return ((sigByte & mask) != 0); - } + int bitIdx = idx & 0x7; + int mask = 0x1 << bitIdx; + int sigByte = signature[byteIdx]; + return ((sigByte & mask) != 0); } private void setBit(byte[] signature, int idx) { @@ -908,7 +907,7 @@ public class ProxyFactory { } } - private static void setInterfaces(ClassFile cf, Class[] interfaces, Class proxyClass) { + private static void setInterfaces(ClassFile cf, Class<?>[] interfaces, Class<?> proxyClass) { String setterIntf = proxyClass.getName(); String[] list; if (interfaces == null || interfaces.length == 0) @@ -925,7 +924,7 @@ public class ProxyFactory { } private static void addClassInitializer(ClassFile cf, ConstPool cp, - String classname, int size, ArrayList forwarders) + String classname, int size, List<Find2MethodsArgs> forwarders) throws CannotCompileException { FieldInfo finfo = new FieldInfo(cp, HOLDER, HOLDER_TYPE); @@ -933,7 +932,7 @@ public class ProxyFactory { cf.addField(finfo); MethodInfo minfo = new MethodInfo(cp, "<clinit>", "()V"); minfo.setAccessFlags(AccessFlag.STATIC); - setThrows(minfo, cp, new Class[] { ClassNotFoundException.class }); + setThrows(minfo, cp, new Class<?>[] { ClassNotFoundException.class }); Bytecode code = new Bytecode(cp, 0, 2); code.addIconst(size * 2); @@ -949,12 +948,9 @@ public class ProxyFactory { final int varClass = 1; code.addAstore(varClass); - Iterator it = forwarders.iterator(); - while (it.hasNext()) { - Find2MethodsArgs args = (Find2MethodsArgs)it.next(); + for (Find2MethodsArgs args:forwarders) callFind2Methods(code, args.methodName, args.delegatorName, args.origIndex, args.descriptor, varClass, varArray); - } code.addAload(varArray); code.addPutstatic(classname, HOLDER, HOLDER_TYPE); @@ -1017,20 +1013,18 @@ public class ProxyFactory { cf.addMethod(minfo); } - private int overrideMethods(ClassFile cf, ConstPool cp, String className, ArrayList forwarders) + private int overrideMethods(ClassFile cf, ConstPool cp, String className, List<Find2MethodsArgs> forwarders) throws CannotCompileException { String prefix = makeUniqueName("_d", signatureMethods); - Iterator it = signatureMethods.iterator(); + Iterator<Map.Entry<String,Method>> it = signatureMethods.iterator(); int index = 0; while (it.hasNext()) { - Map.Entry e = (Map.Entry)it.next(); - String key = (String)e.getKey(); - Method meth = (Method)e.getValue(); - if (ClassFile.MAJOR_VERSION < ClassFile.JAVA_5 || !isBridge(meth)) + Map.Entry<String,Method> e = it.next(); + if (ClassFile.MAJOR_VERSION < ClassFile.JAVA_5 || !isBridge(e.getValue())) if (testBit(signature, index)) { - override(className, meth, prefix, index, - keyToDesc(key, meth), cf, cp, forwarders); + override(className, e.getValue(), prefix, index, + keyToDesc(e.getKey(), e.getValue()), cf, cp, forwarders); } index++; @@ -1044,10 +1038,11 @@ public class ProxyFactory { } private void override(String thisClassname, Method meth, String prefix, - int index, String desc, ClassFile cf, ConstPool cp, ArrayList forwarders) + int index, String desc, ClassFile cf, ConstPool cp, + List<Find2MethodsArgs> forwarders) throws CannotCompileException { - Class declClass = meth.getDeclaringClass(); + Class<?> declClass = meth.getDeclaringClass(); String delegatorName = prefix + index + meth.getName(); if (Modifier.isAbstract(meth.getModifiers())) delegatorName = null; @@ -1068,11 +1063,11 @@ public class ProxyFactory { private void makeConstructors(String thisClassName, ClassFile cf, ConstPool cp, String classname) throws CannotCompileException { - Constructor[] cons = SecurityActions.getDeclaredConstructors(superClass); + Constructor<?>[] cons = SecurityActions.getDeclaredConstructors(superClass); // legacy: if we are not caching then we need to initialise the default handler boolean doHandlerInit = !factoryUseCache; for (int i = 0; i < cons.length; i++) { - Constructor c = cons[i]; + Constructor<?> c = cons[i]; int mod = c.getModifiers(); if (!Modifier.isFinal(mod) && !Modifier.isPrivate(mod) && isVisible(mod, basename, c)) { @@ -1082,7 +1077,7 @@ public class ProxyFactory { } } - private static String makeUniqueName(String name, List sortedMethods) { + private static String makeUniqueName(String name, List<Map.Entry<String,Method>> sortedMethods) { if (makeUniqueName0(name, sortedMethods.iterator())) return name; @@ -1095,14 +1090,10 @@ public class ProxyFactory { throw new RuntimeException("cannot make a unique method name"); } - private static boolean makeUniqueName0(String name, Iterator it) { - while (it.hasNext()) { - Map.Entry e = (Map.Entry)it.next(); - String key = (String)e.getKey(); - if (key.startsWith(name)) + private static boolean makeUniqueName0(String name, Iterator<Map.Entry<String,Method>> it) { + while (it.hasNext()) + if (it.next().getKey().startsWith(name)) return false; - } - return true; } @@ -1121,8 +1112,7 @@ public class ProxyFactory { String q = getPackageName(meth.getDeclaringClass().getName()); if (p == null) return q == null; - else - return p.equals(q); + return p.equals(q); } } @@ -1130,15 +1120,14 @@ public class ProxyFactory { int i = name.lastIndexOf('.'); if (i < 0) return null; - else - return name.substring(0, i); + return name.substring(0, i); } /* getMethods() may set hasGetHandler to true. */ - private HashMap getMethods(Class superClass, Class[] interfaceTypes) { - HashMap hash = new HashMap(); - HashSet set = new HashSet(); + private Map<String,Method> getMethods(Class<?> superClass, Class<?>[] interfaceTypes) { + Map<String,Method> hash = new HashMap<String,Method>(); + Set<Class<?>> set = new HashSet<Class<?>>(); for (int i = 0; i < interfaceTypes.length; i++) getMethods(hash, interfaceTypes[i], set); @@ -1146,17 +1135,17 @@ public class ProxyFactory { return hash; } - private void getMethods(HashMap hash, Class clazz, Set visitedClasses) { + private void getMethods(Map<String,Method> hash, Class<?> clazz, Set<Class<?>> visitedClasses) { // This both speeds up scanning by avoiding duplicate interfaces and is needed to // ensure that superinterfaces are always scanned before subinterfaces. if (!visitedClasses.add(clazz)) return; - Class[] ifs = clazz.getInterfaces(); + Class<?>[] ifs = clazz.getInterfaces(); for (int i = 0; i < ifs.length; i++) getMethods(hash, ifs[i], visitedClasses); - Class parent = clazz.getSuperclass(); + Class<?> parent = clazz.getSuperclass(); if (parent != null) getMethods(hash, parent, visitedClasses); @@ -1174,8 +1163,8 @@ public class ProxyFactory { hasGetHandler = true; // JIRA JASSIST-85 - // put the method to the cache, retrieve previous definition (if any) - Method oldMethod = (Method)hash.put(key, m); + // put the method to the cache, retrieve previous definition (if any) + Method oldMethod = hash.put(key, m); // JIRA JASSIST-244 // ignore a bridge method with the same signature that the overridden one has. @@ -1211,8 +1200,8 @@ public class ProxyFactory { return key.substring(key.indexOf(':') + 1); } - private static MethodInfo makeConstructor(String thisClassName, Constructor cons, - ConstPool cp, Class superClass, boolean doHandlerInit) { + private static MethodInfo makeConstructor(String thisClassName, Constructor<?> cons, + ConstPool cp, Class<?> superClass, boolean doHandlerInit) { String desc = RuntimeSupport.makeDescriptor(cons.getParameterTypes(), Void.TYPE); MethodInfo minfo = new MethodInfo(cp, "<init>", desc); @@ -1253,7 +1242,7 @@ public class ProxyFactory { } private MethodInfo makeDelegator(Method meth, String desc, - ConstPool cp, Class declClass, String delegatorName) { + ConstPool cp, Class<?> declClass, String delegatorName) { MethodInfo delegator = new MethodInfo(cp, delegatorName, desc); delegator.setAccessFlags(Modifier.FINAL | Modifier.PUBLIC | (meth.getModifiers() & ~(Modifier.PRIVATE @@ -1265,7 +1254,7 @@ public class ProxyFactory { Bytecode code = new Bytecode(cp, 0, 0); code.addAload(0); int s = addLoadParameters(code, meth.getParameterTypes(), 1); - Class targetClass = invokespecialTarget(declClass); + Class<?> targetClass = invokespecialTarget(declClass); code.addInvokespecial(targetClass.isInterface(), cp.addClassInfo(targetClass.getName()), meth.getName(), desc); addReturn(code, meth.getReturnType()); @@ -1279,9 +1268,9 @@ public class ProxyFactory { * (or its interface). If S <: U <: T (S <: T reads "S extends T"), * the target type of invokespecial has to be not T but U. */ - private Class invokespecialTarget(Class declClass) { + private Class<?> invokespecialTarget(Class<?> declClass) { if (declClass.isInterface()) - for (Class i: interfaces) + for (Class<?> i: interfaces) if (declClass.isAssignableFrom(i)) return i; @@ -1293,8 +1282,8 @@ public class ProxyFactory { */ private static MethodInfo makeForwarder(String thisClassName, Method meth, String desc, ConstPool cp, - Class declClass, String delegatorName, int index, - ArrayList forwarders) { + Class<?> declClass, String delegatorName, int index, + List<Find2MethodsArgs> forwarders) { MethodInfo forwarder = new MethodInfo(cp, meth.getName(), desc); forwarder.setAccessFlags(Modifier.FINAL | (meth.getModifiers() & ~(Modifier.ABSTRACT @@ -1339,7 +1328,7 @@ public class ProxyFactory { code.addInvokeinterface(MethodHandler.class.getName(), "invoke", "(Ljava/lang/Object;Ljava/lang/reflect/Method;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;", 5); - Class retType = meth.getReturnType(); + Class<?> retType = meth.getReturnType(); addUnwrapper(code, retType); addReturn(code, retType); @@ -1361,12 +1350,12 @@ public class ProxyFactory { } private static void setThrows(MethodInfo minfo, ConstPool cp, Method orig) { - Class[] exceptions = orig.getExceptionTypes(); + Class<?>[] exceptions = orig.getExceptionTypes(); setThrows(minfo, cp, exceptions); } private static void setThrows(MethodInfo minfo, ConstPool cp, - Class[] exceptions) { + Class<?>[] exceptions) { if (exceptions.length == 0) return; @@ -1379,7 +1368,7 @@ public class ProxyFactory { minfo.setExceptionsAttribute(ea); } - private static int addLoadParameters(Bytecode code, Class[] params, + private static int addLoadParameters(Bytecode code, Class<?>[] params, int offset) { int stacksize = 0; int n = params.length; @@ -1389,7 +1378,7 @@ public class ProxyFactory { return stacksize; } - private static int addLoad(Bytecode code, int n, Class type) { + private static int addLoad(Bytecode code, int n, Class<?> type) { if (type.isPrimitive()) { if (type == Long.TYPE) { code.addLload(n); @@ -1410,7 +1399,7 @@ public class ProxyFactory { return 1; } - private static int addReturn(Bytecode code, Class type) { + private static int addReturn(Bytecode code, Class<?> type) { if (type.isPrimitive()) { if (type == Long.TYPE) { code.addOpcode(Opcode.LRETURN); @@ -1435,7 +1424,7 @@ public class ProxyFactory { return 1; } - private static void makeParameterList(Bytecode code, Class[] params) { + private static void makeParameterList(Bytecode code, Class<?>[] params) { int regno = 1; int n = params.length; code.addIconst(n); @@ -1443,7 +1432,7 @@ public class ProxyFactory { for (int i = 0; i < n; i++) { code.addOpcode(Opcode.DUP); code.addIconst(i); - Class type = params[i]; + Class<?> type = params[i]; if (type.isPrimitive()) regno = makeWrapper(code, type, regno); else { @@ -1455,7 +1444,7 @@ public class ProxyFactory { } } - private static int makeWrapper(Bytecode code, Class type, int regno) { + private static int makeWrapper(Bytecode code, Class<?> type, int regno) { int index = FactoryHelper.typeIndex(type); String wrapper = FactoryHelper.wrapperTypes[index]; code.addNew(wrapper); @@ -1466,7 +1455,7 @@ public class ProxyFactory { return regno + FactoryHelper.dataSize[index]; } - private static void addUnwrapper(Bytecode code, Class type) { + private static void addUnwrapper(Bytecode code, Class<?> type) { if (type.isPrimitive()) { if (type == Void.TYPE) code.addOpcode(Opcode.POP); diff --git a/src/main/javassist/util/proxy/ProxyObject.java b/src/main/javassist/util/proxy/ProxyObject.java index f04ae9a4..a5a39e26 100644 --- a/src/main/javassist/util/proxy/ProxyObject.java +++ b/src/main/javassist/util/proxy/ProxyObject.java @@ -31,6 +31,7 @@ public interface ProxyObject extends Proxy { * Sets a handler. It can be used for changing handlers * during runtime. */ + @Override void setHandler(MethodHandler mi); /** diff --git a/src/main/javassist/util/proxy/ProxyObjectInputStream.java b/src/main/javassist/util/proxy/ProxyObjectInputStream.java index 25ae4d20..2992cc95 100644 --- a/src/main/javassist/util/proxy/ProxyObjectInputStream.java +++ b/src/main/javassist/util/proxy/ProxyObjectInputStream.java @@ -63,13 +63,14 @@ public class ProxyObjectInputStream extends ObjectInputStream } } + @Override protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { boolean isProxy = readBoolean(); if (isProxy) { String name = (String)readObject(); - Class superClass = loader.loadClass(name); + Class<?> superClass = loader.loadClass(name); int length = readInt(); - Class[] interfaces = new Class[length]; + Class<?>[] interfaces = new Class[length]; for (int i = 0; i < length; i++) { name = (String)readObject(); interfaces[i] = loader.loadClass(name); @@ -84,11 +85,10 @@ public class ProxyObjectInputStream extends ObjectInputStream factory.setUseWriteReplace(false); factory.setSuperclass(superClass); factory.setInterfaces(interfaces); - Class proxyClass = factory.createClass(signature); + Class<?> proxyClass = factory.createClass(signature); return ObjectStreamClass.lookup(proxyClass); - } else { - return super.readClassDescriptor(); } + return super.readClassDescriptor(); } /** diff --git a/src/main/javassist/util/proxy/ProxyObjectOutputStream.java b/src/main/javassist/util/proxy/ProxyObjectOutputStream.java index 31c3861b..87351685 100644 --- a/src/main/javassist/util/proxy/ProxyObjectOutputStream.java +++ b/src/main/javassist/util/proxy/ProxyObjectOutputStream.java @@ -44,19 +44,20 @@ public class ProxyObjectOutputStream extends ObjectOutputStream super(out); } + @Override protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException { - Class cl = desc.forClass(); + Class<?> cl = desc.forClass(); if (ProxyFactory.isProxyClass(cl)) { writeBoolean(true); - Class superClass = cl.getSuperclass(); - Class[] interfaces = cl.getInterfaces(); + Class<?> superClass = cl.getSuperclass(); + Class<?>[] interfaces = cl.getInterfaces(); byte[] signature = ProxyFactory.getFilterSignature(cl); String name = superClass.getName(); writeObject(name); // we don't write the marker interface ProxyObject writeInt(interfaces.length - 1); for (int i = 0; i < interfaces.length; i++) { - Class interfaze = interfaces[i]; + Class<?> interfaze = interfaces[i]; if (interfaze != ProxyObject.class && interfaze != Proxy.class) { name = interfaces[i].getName(); writeObject(name); diff --git a/src/main/javassist/util/proxy/RuntimeSupport.java b/src/main/javassist/util/proxy/RuntimeSupport.java index 2c6a5ecc..ba7a9469 100644 --- a/src/main/javassist/util/proxy/RuntimeSupport.java +++ b/src/main/javassist/util/proxy/RuntimeSupport.java @@ -34,6 +34,7 @@ public class RuntimeSupport { /** default serialVersionUID */ private static final long serialVersionUID = 1L; + @Override public Object invoke(Object self, Method m, Method proceed, Object[] args) throws Exception @@ -49,7 +50,7 @@ public class RuntimeSupport { * @throws RuntimeException if the methods are not found. * @see javassist.util.proxy.ProxyFactory */ - public static void find2Methods(Class clazz, String superMethod, + public static void find2Methods(Class<?> clazz, String superMethod, String thisMethod, int index, String desc, java.lang.reflect.Method[] methods) { @@ -68,6 +69,7 @@ public class RuntimeSupport { * @see javassist.util.proxy.ProxyFactory * @deprecated replaced by {@link #find2Methods(Class, String, String, int, String, Method[])} */ + @Deprecated public static void find2Methods(Object self, String superMethod, String thisMethod, int index, String desc, java.lang.reflect.Method[] methods) @@ -86,6 +88,7 @@ public class RuntimeSupport { * @throws RuntimeException if the method is not found. * @deprecated replaced by {@link #findMethod(Class, String, String)} */ + @Deprecated public static Method findMethod(Object self, String name, String desc) { Method m = findMethod2(self.getClass(), name, desc); if (m == null) @@ -100,7 +103,7 @@ public class RuntimeSupport { * * @throws RuntimeException if the method is not found. */ - public static Method findMethod(Class clazz, String name, String desc) { + public static Method findMethod(Class<?> clazz, String name, String desc) { Method m = findMethod2(clazz, name, desc); if (m == null) error(clazz, name, desc); @@ -116,7 +119,7 @@ public class RuntimeSupport { */ public static Method findSuperMethod(Object self, String name, String desc) { // for JBoss Seam. See JASSIST-183. - Class clazz = self.getClass(); + Class<?> clazz = self.getClass(); return findSuperClassMethod(clazz, name, desc); } @@ -126,7 +129,7 @@ public class RuntimeSupport { * * @throws RuntimeException if the method is not found. */ - public static Method findSuperClassMethod(Class clazz, String name, String desc) { + public static Method findSuperClassMethod(Class<?> clazz, String name, String desc) { Method m = findSuperMethod2(clazz.getSuperclass(), name, desc); if (m == null) m = searchInterfaces(clazz, name, desc); @@ -137,17 +140,17 @@ public class RuntimeSupport { return m; } - private static void error(Class clazz, String name, String desc) { + private static void error(Class<?> clazz, String name, String desc) { throw new RuntimeException("not found " + name + ":" + desc + " in " + clazz.getName()); } - private static Method findSuperMethod2(Class clazz, String name, String desc) { + private static Method findSuperMethod2(Class<?> clazz, String name, String desc) { Method m = findMethod2(clazz, name, desc); if (m != null) return m; - Class superClass = clazz.getSuperclass(); + Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { m = findSuperMethod2(superClass, name, desc); if (m != null) @@ -157,9 +160,9 @@ public class RuntimeSupport { return searchInterfaces(clazz, name, desc); } - private static Method searchInterfaces(Class clazz, String name, String desc) { + private static Method searchInterfaces(Class<?> clazz, String name, String desc) { Method m = null; - Class[] interfaces = clazz.getInterfaces(); + Class<?>[] interfaces = clazz.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { m = findSuperMethod2(interfaces[i], name, desc); if (m != null) @@ -169,7 +172,7 @@ public class RuntimeSupport { return m; } - private static Method findMethod2(Class clazz, String name, String desc) { + private static Method findMethod2(Class<?> clazz, String name, String desc) { Method[] methods = SecurityActions.getDeclaredMethods(clazz); int n = methods.length; for (int i = 0; i < n; i++) @@ -184,7 +187,7 @@ public class RuntimeSupport { * Makes a descriptor for a given method. */ public static String makeDescriptor(Method m) { - Class[] params = m.getParameterTypes(); + Class<?>[] params = m.getParameterTypes(); return makeDescriptor(params, m.getReturnType()); } @@ -194,7 +197,7 @@ public class RuntimeSupport { * @param params parameter types. * @param retType return type. */ - public static String makeDescriptor(Class[] params, Class retType) { + public static String makeDescriptor(Class<?>[] params, Class<?> retType) { StringBuffer sbuf = new StringBuffer(); sbuf.append('('); for (int i = 0; i < params.length; i++) @@ -213,13 +216,13 @@ public class RuntimeSupport { * @param params the descriptor of parameter types. * @param retType return type. */ - public static String makeDescriptor(String params, Class retType) { + public static String makeDescriptor(String params, Class<?> retType) { StringBuffer sbuf = new StringBuffer(params); makeDesc(sbuf, retType); return sbuf.toString(); } - private static void makeDesc(StringBuffer sbuf, Class type) { + private static void makeDesc(StringBuffer sbuf, Class<?> type) { if (type.isArray()) { sbuf.append('['); makeDesc(sbuf, type.getComponentType()); @@ -261,7 +264,7 @@ public class RuntimeSupport { public static SerializedProxy makeSerializedProxy(Object proxy) throws java.io.InvalidClassException { - Class clazz = proxy.getClass(); + Class<?> clazz = proxy.getClass(); MethodHandler methodHandler = null; if (proxy instanceof ProxyObject) diff --git a/src/main/javassist/util/proxy/SerializedProxy.java b/src/main/javassist/util/proxy/SerializedProxy.java index 9427e7e5..c7f9c448 100644 --- a/src/main/javassist/util/proxy/SerializedProxy.java +++ b/src/main/javassist/util/proxy/SerializedProxy.java @@ -18,11 +18,12 @@ package javassist.util.proxy; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; +import java.io.InvalidClassException; +import java.io.InvalidObjectException; import java.io.ObjectStreamException; import java.security.AccessController; import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; -import java.security.ProtectionDomain; /** * A proxy object is converted into an instance of this class @@ -38,11 +39,11 @@ class SerializedProxy implements Serializable { private byte[] filterSignature; private MethodHandler handler; - SerializedProxy(Class proxy, byte[] sig, MethodHandler h) { + SerializedProxy(Class<?> proxy, byte[] sig, MethodHandler h) { filterSignature = sig; handler = h; superClass = proxy.getSuperclass().getName(); - Class[] infs = proxy.getInterfaces(); + Class<?>[] infs = proxy.getInterfaces(); int n = infs.length; interfaces = new String[n - 1]; String setterInf = ProxyObject.class.getName(); @@ -61,10 +62,11 @@ class SerializedProxy implements Serializable { * @return loaded class * @throws ClassNotFoundException for any error */ - protected Class loadClass(final String className) throws ClassNotFoundException { + protected Class<?> loadClass(final String className) throws ClassNotFoundException { try { - return (Class)AccessController.doPrivileged(new PrivilegedExceptionAction(){ - public Object run() throws Exception{ + return AccessController.doPrivileged(new PrivilegedExceptionAction<Class<?>>(){ + @Override + public Class<?> run() throws Exception{ ClassLoader cl = Thread.currentThread().getContextClassLoader(); return Class.forName(className, true, cl); } @@ -78,7 +80,7 @@ class SerializedProxy implements Serializable { Object readResolve() throws ObjectStreamException { try { int n = interfaces.length; - Class[] infs = new Class[n]; + Class<?>[] infs = new Class[n]; for (int i = 0; i < n; i++) infs[i] = loadClass(interfaces[i]); @@ -90,19 +92,19 @@ class SerializedProxy implements Serializable { return proxy; } catch (NoSuchMethodException e) { - throw new java.io.InvalidClassException(e.getMessage()); + throw new InvalidClassException(e.getMessage()); } catch (InvocationTargetException e) { - throw new java.io.InvalidClassException(e.getMessage()); + throw new InvalidClassException(e.getMessage()); } catch (ClassNotFoundException e) { - throw new java.io.InvalidClassException(e.getMessage()); + throw new InvalidClassException(e.getMessage()); } catch (InstantiationException e2) { - throw new java.io.InvalidObjectException(e2.getMessage()); + throw new InvalidObjectException(e2.getMessage()); } catch (IllegalAccessException e3) { - throw new java.io.InvalidClassException(e3.getMessage()); + throw new InvalidClassException(e3.getMessage()); } } } |