Clean up warnings in CGLIB fork

This commit is contained in:
Sam Brannen 2022-08-13 16:03:28 +02:00
parent 6ba31acf35
commit d6d629a8eb
29 changed files with 431 additions and 231 deletions

View File

@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.core;
import org.springframework.asm.Attribute;
import org.springframework.asm.Type;
abstract public class ClassInfo {
@ -28,6 +28,7 @@ abstract public class ClassInfo {
abstract public Type[] getInterfaces();
abstract public int getModifiers();
@Override
public boolean equals(Object o) {
if (o == null)
return false;
@ -36,10 +37,12 @@ abstract public class ClassInfo {
return getType().equals(((ClassInfo)o).getType());
}
@Override
public int hashCode() {
return getType().hashCode();
}
@Override
public String toString() {
// TODO: include modifiers, superType, interfaces
return getType().getClassName();

View File

@ -13,11 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.core;
import java.io.*;
import java.util.*;
import org.springframework.asm.*;
import java.util.Arrays;
import org.springframework.asm.Label;
import org.springframework.asm.MethodVisitor;
import org.springframework.asm.Type;
/**
* @author Juozas Baliuka, Chris Nokleberg
@ -81,26 +84,26 @@ public class CodeEmitter extends LocalVariablesSorter {
argumentTypes = sig.getArgumentTypes();
}
@Override
public ClassInfo getClassInfo() {
return classInfo;
}
@Override
public int getModifiers() {
return access;
}
@Override
public Signature getSignature() {
return sig;
}
@Override
public Type[] getExceptionTypes() {
return exceptionTypes;
}
public Attribute getAttribute() {
// TODO
return null;
}
}
CodeEmitter(ClassEmitter ce, MethodVisitor mv, int access, Signature sig, Type[] exceptionTypes) {
@ -843,6 +846,7 @@ public class CodeEmitter extends LocalVariablesSorter {
}
}
@Override
public void visitMaxs(int maxStack, int maxLocals) {
if (!TypeUtils.isAbstract(state.access)) {
mv.visitMaxs(0, 0);

View File

@ -13,10 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.core;
import java.util.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author Chris Nokleberg
@ -74,4 +80,3 @@ public class CollectionUtils {
return indexes;
}
}

View File

@ -13,15 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.core;
import org.springframework.asm.ClassWriter;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import org.springframework.asm.ClassReader;
import org.springframework.asm.ClassVisitor;
import org.springframework.asm.Opcodes;
import java.io.*;
import java.lang.reflect.Constructor;
import org.springframework.asm.ClassWriter;
@SuppressWarnings({"rawtypes", "unchecked"})
public class DebuggingClassWriter extends ClassVisitor {
@ -50,6 +55,7 @@ public class DebuggingClassWriter extends ClassVisitor {
super(Constants.ASM_API, new ClassWriter(flags));
}
@Override
public void visit(int version,
int access,
String name,

View File

@ -13,9 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.core;
import java.util.Set;
package org.springframework.cglib.core;
/**
* The default policy used by {@link AbstractClassGenerator}.
@ -35,6 +34,7 @@ public class DefaultNamingPolicy implements NamingPolicy {
*/
private final static boolean STRESS_HASH_CODE = Boolean.getBoolean("org.springframework.cglib.test.stressHashCodes");
@Override
public String getClassName(String prefix, String source, Object key, Predicate names) {
if (prefix == null) {
prefix = "org.springframework.cglib.empty.Object";
@ -61,11 +61,14 @@ public class DefaultNamingPolicy implements NamingPolicy {
return "ByCGLIB";
}
@Override
public int hashCode() {
return getTag().hashCode();
}
@Override
public boolean equals(Object o) {
return (o instanceof DefaultNamingPolicy) && ((DefaultNamingPolicy) o).getTag().equals(getTag());
return (o instanceof DefaultNamingPolicy defaultNamingPolicy) &&
defaultNamingPolicy.getTag().equals(getTag());
}
}

View File

@ -13,15 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.core;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.cglib.core.internal.CustomizerRegistry;
import org.springframework.asm.Label;
import org.springframework.asm.Type;
import org.springframework.cglib.core.internal.CustomizerRegistry;
@SuppressWarnings({"rawtypes", "unchecked", "static", "fallthrough", "deprecation"})
public class EmitUtils {
@ -123,7 +132,7 @@ public class EmitUtils {
e.load_local(loopvar);
e.load_local(array);
e.arraylength();
e.if_icmp(e.LT, loopbody);
e.if_icmp(CodeEmitter.LT, loopbody);
}
/**
@ -160,7 +169,7 @@ public class EmitUtils {
e.load_local(loopvar);
e.load_local(array1);
e.arraylength();
e.if_icmp(e.LT, loopbody);
e.if_icmp(CodeEmitter.LT, loopbody);
}
public static void string_switch(CodeEmitter e, String[] strings, int switchStyle, ObjectSwitchCallback callback) {
@ -193,6 +202,7 @@ public class EmitUtils {
final Label def = e.make_label();
final Label end = e.make_label();
final Map buckets = CollectionUtils.bucket(Arrays.asList(strings), new Transformer() {
@Override
public Object transform(Object value) {
return ((String)value).length();
}
@ -200,10 +210,12 @@ public class EmitUtils {
e.dup();
e.invoke_virtual(Constants.TYPE_STRING, STRING_LENGTH);
e.process_switch(getSwitchKeys(buckets), new ProcessSwitchCallback() {
@Override
public void processCase(int key, Label ignore_end) throws Exception {
List bucket = (List)buckets.get(key);
stringSwitchHelper(e, bucket, callback, def, end, 0);
}
@Override
public void processDefault() {
e.goTo(def);
}
@ -222,6 +234,7 @@ public class EmitUtils {
final int index) throws Exception {
final int len = ((String)strings.get(0)).length();
final Map buckets = CollectionUtils.bucket(strings, new Transformer() {
@Override
public Object transform(Object value) {
return ((String)value).charAt(index);
}
@ -230,6 +243,7 @@ public class EmitUtils {
e.push(index);
e.invoke_virtual(Constants.TYPE_STRING, STRING_CHAR_AT);
e.process_switch(getSwitchKeys(buckets), new ProcessSwitchCallback() {
@Override
public void processCase(int key, Label ignore_end) throws Exception {
List bucket = (List)buckets.get(key);
if (index + 1 == len) {
@ -239,6 +253,7 @@ public class EmitUtils {
stringSwitchHelper(e, bucket, callback, def, end, index + 1);
}
}
@Override
public void processDefault() {
e.goTo(def);
}
@ -260,6 +275,7 @@ public class EmitUtils {
final ObjectSwitchCallback callback,
final boolean skipEquals) throws Exception {
final Map buckets = CollectionUtils.bucket(Arrays.asList(strings), new Transformer() {
@Override
public Object transform(Object value) {
return value.hashCode();
}
@ -269,13 +285,14 @@ public class EmitUtils {
e.dup();
e.invoke_virtual(Constants.TYPE_OBJECT, HASH_CODE);
e.process_switch(getSwitchKeys(buckets), new ProcessSwitchCallback() {
@Override
public void processCase(int key, Label ignore_end) throws Exception {
List bucket = (List)buckets.get(key);
Label next = null;
if (skipEquals && bucket.size() == 1) {
if (skipEquals)
e.pop();
callback.processCase((String)bucket.get(0), end);
callback.processCase(bucket.get(0), end);
} else {
for (Iterator it = bucket.iterator(); it.hasNext();) {
String string = (String)it.next();
@ -288,15 +305,16 @@ public class EmitUtils {
e.push(string);
e.invoke_virtual(Constants.TYPE_OBJECT, EQUALS);
if (it.hasNext()) {
e.if_jump(e.EQ, next = e.make_label());
e.if_jump(CodeEmitter.EQ, next = e.make_label());
e.pop();
} else {
e.if_jump(e.EQ, def);
e.if_jump(CodeEmitter.EQ, def);
}
callback.processCase(string, end);
}
}
}
@Override
public void processDefault() {
e.pop();
}
@ -403,14 +421,14 @@ public class EmitUtils {
} else {
e.swap(Type.INT_TYPE, type);
e.push(multiplier);
e.math(e.MUL, Type.INT_TYPE);
e.math(CodeEmitter.MUL, Type.INT_TYPE);
e.swap(type, Type.INT_TYPE);
if (TypeUtils.isPrimitive(type)) {
hash_primitive(e, type);
} else {
hash_object(e, type, registry);
}
e.math(e.ADD, Type.INT_TYPE);
e.math(CodeEmitter.ADD, Type.INT_TYPE);
}
}
@ -420,6 +438,7 @@ public class EmitUtils {
e.dup();
e.ifnull(skip);
EmitUtils.process_array(e, type, new ProcessArrayCallback() {
@Override
public void processElement(Type type) {
hash_code(e, type, multiplier, registry);
}
@ -461,7 +480,7 @@ public class EmitUtils {
case Type.BOOLEAN:
// f ? 0 : 1
e.push(1);
e.math(e.XOR, Type.INT_TYPE);
e.math(CodeEmitter.XOR, Type.INT_TYPE);
break;
case Type.FLOAT:
// Float.floatToIntBits(f)
@ -480,8 +499,8 @@ public class EmitUtils {
// (int)(f ^ (f >>> 32))
e.dup2();
e.push(32);
e.math(e.USHR, Type.LONG_TYPE);
e.math(e.XOR, Type.LONG_TYPE);
e.math(CodeEmitter.USHR, Type.LONG_TYPE);
e.math(CodeEmitter.XOR, Type.LONG_TYPE);
e.cast_numeric(Type.LONG_TYPE, Type.INT_TYPE);
}
@ -506,6 +525,7 @@ public class EmitUtils {
*/
public static void not_equals(final CodeEmitter e, Type type, final Label notEquals, final CustomizerRegistry registry) {
(new ProcessArrayCallback() {
@Override
public void processElement(Type type) {
not_equals_helper(e, type, notEquals, registry, this);
}
@ -518,7 +538,7 @@ public class EmitUtils {
CustomizerRegistry registry,
ProcessArrayCallback callback) {
if (TypeUtils.isPrimitive(type)) {
e.if_cmp(type, e.NE, notEquals);
e.if_cmp(type, CodeEmitter.NE, notEquals);
} else {
Label end = e.make_label();
nullcmp(e, notEquals, end);
@ -528,7 +548,7 @@ public class EmitUtils {
e.arraylength();
e.swap();
e.arraylength();
e.if_icmp(e.EQ, checkContents);
e.if_icmp(CodeEmitter.EQ, checkContents);
e.pop2();
e.goTo(notEquals);
e.mark(checkContents);
@ -545,7 +565,7 @@ public class EmitUtils {
}
}
e.invoke_virtual(Constants.TYPE_OBJECT, EQUALS);
e.if_jump(e.EQ, notEquals);
e.if_jump(CodeEmitter.EQ, notEquals);
}
e.mark(end);
}
@ -610,6 +630,7 @@ public class EmitUtils {
final CustomizerRegistry registry) {
final ArrayDelimiters d = (delims != null) ? delims : DEFAULT_DELIMITERS;
ProcessArrayCallback callback = new ProcessArrayCallback() {
@Override
public void processElement(Type type) {
append_string_helper(e, type, d, registry, this);
e.push(d.inside);
@ -686,7 +707,7 @@ public class EmitUtils {
e.dup();
e.invoke_virtual(Constants.TYPE_STRING_BUFFER, LENGTH);
e.push(amt);
e.math(e.SUB, Type.INT_TYPE);
e.math(CodeEmitter.SUB, Type.INT_TYPE);
e.invoke_virtual(Constants.TYPE_STRING_BUFFER, SET_LENGTH);
}
@ -732,6 +753,7 @@ public class EmitUtils {
try {
final Map cache = new HashMap();
final ParameterTyper cached = new ParameterTyper() {
@Override
public Type[] getParameterTypes(MethodInfo member) {
Type[] types = (Type[])cache.get(member);
if (types == null) {
@ -745,15 +767,18 @@ public class EmitUtils {
if (useName) {
e.swap();
final Map buckets = CollectionUtils.bucket(members, new Transformer() {
@Override
public Object transform(Object value) {
return ((MethodInfo)value).getSignature().getName();
}
});
String[] names = (String[])buckets.keySet().toArray(new String[buckets.size()]);
EmitUtils.string_switch(e, names, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {
@Override
public void processCase(Object key, Label dontUseEnd) throws Exception {
member_helper_size(e, (List)buckets.get(key), callback, cached, def, end);
}
@Override
public void processDefault() throws Exception {
e.goTo(def);
}
@ -781,6 +806,7 @@ public class EmitUtils {
final Label def,
final Label end) throws Exception {
final Map buckets = CollectionUtils.bucket(members, new Transformer() {
@Override
public Object transform(Object value) {
return typer.getParameterTypes((MethodInfo)value).length;
}
@ -788,10 +814,12 @@ public class EmitUtils {
e.dup();
e.arraylength();
e.process_switch(EmitUtils.getSwitchKeys(buckets), new ProcessSwitchCallback() {
@Override
public void processCase(int key, Label dontUseEnd) throws Exception {
List bucket = (List)buckets.get(key);
member_helper_type(e, bucket, callback, typer, def, end, new BitSet());
}
@Override
public void processDefault() throws Exception {
e.goTo(def);
}
@ -816,7 +844,7 @@ public class EmitUtils {
e.invoke_virtual(Constants.TYPE_CLASS, GET_NAME);
e.push(TypeUtils.emulateClassGetName(types[i]));
e.invoke_virtual(Constants.TYPE_OBJECT, EQUALS);
e.if_jump(e.EQ, def);
e.if_jump(CodeEmitter.EQ, def);
}
}
e.pop();
@ -829,6 +857,7 @@ public class EmitUtils {
for (int i = 0; i < example.length; i++) {
final int j = i;
Map test = CollectionUtils.bucket(members, new Transformer() {
@Override
public Object transform(Object value) {
return TypeUtils.emulateClassGetName(typer.getParameterTypes((MethodInfo)value)[j]);
}
@ -852,9 +881,11 @@ public class EmitUtils {
final Map fbuckets = buckets;
String[] names = (String[])buckets.keySet().toArray(new String[buckets.size()]);
EmitUtils.string_switch(e, names, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {
@Override
public void processCase(Object key, Label dontUseEnd) throws Exception {
member_helper_type(e, (List)fbuckets.get(key), callback, typer, def, end, checked);
}
@Override
public void processDefault() throws Exception {
e.goTo(def);
}

View File

@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.core;
import org.springframework.asm.Attribute;
import org.springframework.asm.Type;
abstract public class MethodInfo {
@ -28,6 +28,7 @@ abstract public class MethodInfo {
abstract public Signature getSignature();
abstract public Type[] getExceptionTypes();
@Override
public boolean equals(Object o) {
if (o == null)
return false;
@ -36,10 +37,12 @@ abstract public class MethodInfo {
return getSignature().equals(((MethodInfo)o).getSignature());
}
@Override
public int hashCode() {
return getSignature().hashCode();
}
@Override
public String toString() {
// TODO: include modifiers, exceptions
return getSignature().toString();

View File

@ -13,9 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.core;
import java.lang.reflect.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class MethodInfoTransformer implements Transformer
{
@ -25,11 +27,12 @@ public class MethodInfoTransformer implements Transformer
return INSTANCE;
}
@Override
public Object transform(Object value) {
if (value instanceof Method) {
return ReflectUtils.getMethodInfo((Method)value);
} else if (value instanceof Constructor) {
return ReflectUtils.getMethodInfo((Constructor)value);
if (value instanceof Method method) {
return ReflectUtils.getMethodInfo(method);
} else if (value instanceof Constructor<?> constructor) {
return ReflectUtils.getMethodInfo(constructor);
} else {
throw new IllegalArgumentException("cannot get method info for " + value);
}

View File

@ -13,9 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.core;
import java.util.Set;
package org.springframework.cglib.core;
/**
* Customize the generated class name for {@link AbstractClassGenerator}-based utilities.
@ -39,5 +38,6 @@ public interface NamingPolicy {
* correctly implement <code>equals</code> and <code>hashCode</code>
* to avoid generating too many classes.
*/
@Override
boolean equals(Object o);
}

View File

@ -21,11 +21,11 @@ public class WeakCacheKey<T> extends WeakReference<T> {
@Override
public boolean equals(Object obj) {
if (!(obj instanceof WeakCacheKey)) {
if (!(obj instanceof WeakCacheKey<?> weakCacheKey)) {
return false;
}
Object ours = get();
Object theirs = ((WeakCacheKey) obj).get();
Object theirs = weakCacheKey.get();
return ours != null && theirs != null && ours.equals(theirs);
}

View File

@ -1,10 +1,13 @@
package org.springframework.cglib.core.internal;
import org.springframework.cglib.core.Customizer;
import org.springframework.cglib.core.FieldTypeCustomizer;
import org.springframework.cglib.core.KeyFactoryCustomizer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import org.springframework.cglib.core.Customizer;
import org.springframework.cglib.core.KeyFactoryCustomizer;
@SuppressWarnings({"rawtypes", "unchecked"})
public class CustomizerRegistry {

View File

@ -13,12 +13,28 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.proxy;
import java.util.*;
import org.springframework.cglib.core.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.asm.Label;
import org.springframework.asm.Type;
import org.springframework.cglib.core.ClassEmitter;
import org.springframework.cglib.core.ClassInfo;
import org.springframework.cglib.core.CodeEmitter;
import org.springframework.cglib.core.CollectionUtils;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.EmitUtils;
import org.springframework.cglib.core.Local;
import org.springframework.cglib.core.MethodInfo;
import org.springframework.cglib.core.ObjectSwitchCallback;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.core.Transformer;
import org.springframework.cglib.core.TypeUtils;
@SuppressWarnings({"rawtypes", "unchecked", "deprecation"})
class MethodInterceptorGenerator
@ -42,8 +58,6 @@ implements CallbackGenerator
TypeUtils.parseType("org.springframework.cglib.proxy.MethodInterceptor");
private static final Signature GET_DECLARED_METHODS =
TypeUtils.parseSignature("java.lang.reflect.Method[] getDeclaredMethods()");
private static final Signature GET_DECLARING_CLASS =
TypeUtils.parseSignature("Class getDeclaringClass()");
private static final Signature FIND_METHODS =
TypeUtils.parseSignature("java.lang.reflect.Method[] findMethods(String[], java.lang.reflect.Method[])");
private static final Signature MAKE_PROXY =
@ -66,12 +80,11 @@ implements CallbackGenerator
private static final Signature TO_STRING =
TypeUtils.parseSignature("String toString()");
private static final Transformer METHOD_TO_CLASS = new Transformer(){
@Override
public Object transform(Object value) {
return ((MethodInfo)value).getClassInfo();
}
};
private static final Signature CSTRUCT_SIGNATURE =
TypeUtils.parseConstructor("String, String");
private String getMethodField(Signature impl) {
return impl.getName() + "$Method";
@ -80,6 +93,7 @@ implements CallbackGenerator
return impl.getName() + "$Proxy";
}
@Override
public void generate(ClassEmitter ce, Context context, List methods) {
Map sigMap = new HashMap();
for (Iterator it = methods.iterator(); it.hasNext();) {
@ -143,6 +157,7 @@ implements CallbackGenerator
}
}
@Override
public void generateStatic(CodeEmitter e, Context context, List methods) throws Exception {
/* generates:
static {
@ -220,10 +235,12 @@ implements CallbackGenerator
e.load_arg(0);
e.invoke_virtual(Constants.TYPE_OBJECT, TO_STRING);
ObjectSwitchCallback callback = new ObjectSwitchCallback() {
@Override
public void processCase(Object key, Label end) {
e.getfield((String)sigMap.get(key));
e.return_value();
}
@Override
public void processDefault() {
e.aconst_null();
e.return_value();

View File

@ -13,16 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.proxy;
import java.security.ProtectionDomain;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.*;
import org.springframework.cglib.core.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.springframework.asm.ClassVisitor;
import org.springframework.cglib.core.AbstractClassGenerator;
import org.springframework.cglib.core.ClassesKey;
import org.springframework.cglib.core.KeyFactory;
import org.springframework.cglib.core.ReflectUtils;
/**
* <code>Mixin</code> allows
@ -106,10 +111,12 @@ abstract public class Mixin {
super(SOURCE);
}
@Override
protected ClassLoader getDefaultClassLoader() {
return classes[0].getClassLoader(); // is this right?
}
@Override
protected ProtectionDomain getProtectionDomain() {
return ReflectUtils.getProtectionDomain(classes[0]);
}
@ -170,6 +177,7 @@ abstract public class Mixin {
return (Mixin)super.create(KEY_FACTORY.newInstance(style, ReflectUtils.getNames( classes ), route));
}
@Override
public void generateClass(ClassVisitor v) {
switch (style) {
case STYLE_INTERFACES:
@ -184,10 +192,12 @@ abstract public class Mixin {
}
}
@Override
protected Object firstInstance(Class type) {
return ((Mixin)ReflectUtils.newInstance(type)).newInstance(delegates);
}
@Override
protected Object nextInstance(Object instance) {
return ((Mixin)instance).newInstance(delegates);
}

View File

@ -13,11 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.proxy;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Member;
import org.springframework.cglib.core.CodeGenerationException;
/**
@ -40,6 +41,7 @@ public class Proxy implements Serializable {
protected InvocationHandler h;
private static final CallbackFilter BAD_OBJECT_METHOD_FILTER = new CallbackFilter() {
@Override
public int accept(Method method) {
if (method.getDeclaringClass().getName().equals("java.lang.Object")) {
String name = method.getName();

View File

@ -13,14 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.reflect;
import java.lang.reflect.*;
import java.lang.reflect.Method;
import java.security.ProtectionDomain;
import org.springframework.cglib.*;
import org.springframework.cglib.core.*;
import org.springframework.asm.ClassVisitor;
import org.springframework.asm.Type;
import org.springframework.cglib.core.AbstractClassGenerator;
import org.springframework.cglib.core.ClassEmitter;
import org.springframework.cglib.core.CodeEmitter;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.EmitUtils;
import org.springframework.cglib.core.KeyFactory;
import org.springframework.cglib.core.MethodInfo;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.core.TypeUtils;
// TODO: don't require exact match for return type
@ -133,11 +143,13 @@ abstract public class MethodDelegate {
return gen.create();
}
@Override
public boolean equals(Object obj) {
MethodDelegate other = (MethodDelegate)obj;
return (other != null && target == other.target) && eqMethod.equals(other.eqMethod);
}
@Override
public int hashCode() {
return target.hashCode() ^ eqMethod.hashCode();
}
@ -181,10 +193,12 @@ abstract public class MethodDelegate {
this.iface = iface;
}
@Override
protected ClassLoader getDefaultClassLoader() {
return targetClass.getClassLoader();
}
@Override
protected ProtectionDomain getProtectionDomain() {
return ReflectUtils.getProtectionDomain(targetClass);
}
@ -195,14 +209,17 @@ abstract public class MethodDelegate {
return (MethodDelegate)super.create(key);
}
@Override
protected Object firstInstance(Class type) {
return ((MethodDelegate)ReflectUtils.newInstance(type)).newInstance(target);
}
@Override
protected Object nextInstance(Object instance) {
return ((MethodDelegate)instance).newInstance(target);
}
@Override
public void generateClass(ClassVisitor v) throws NoSuchMethodException {
Method proxy = ReflectUtils.findInterfaceMethod(iface);
final Method method = targetClass.getMethod(methodName, proxy.getParameterTypes());

View File

@ -13,15 +13,27 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.reflect;
import java.lang.reflect.*;
import java.security.ProtectionDomain;
import java.util.*;
import org.springframework.cglib.core.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.asm.ClassVisitor;
import org.springframework.asm.MethodVisitor;
import org.springframework.asm.Type;
import org.springframework.cglib.core.AbstractClassGenerator;
import org.springframework.cglib.core.ClassEmitter;
import org.springframework.cglib.core.CodeEmitter;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.EmitUtils;
import org.springframework.cglib.core.Local;
import org.springframework.cglib.core.MethodInfo;
import org.springframework.cglib.core.ProcessArrayCallback;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.core.TypeUtils;
@SuppressWarnings({"rawtypes", "unchecked"})
abstract public class MulticastDelegate implements Cloneable {
@ -82,10 +94,12 @@ abstract public class MulticastDelegate implements Cloneable {
super(SOURCE);
}
@Override
protected ClassLoader getDefaultClassLoader() {
return iface.getClassLoader();
}
@Override
protected ProtectionDomain getProtectionDomain() {
return ReflectUtils.getProtectionDomain(iface);
}
@ -99,6 +113,7 @@ abstract public class MulticastDelegate implements Cloneable {
return (MulticastDelegate)super.create(iface.getName());
}
@Override
public void generateClass(ClassVisitor cv) {
final MethodInfo method = ReflectUtils.getMethodInfo(ReflectUtils.findInterfaceMethod(iface));
@ -152,6 +167,7 @@ abstract public class MulticastDelegate implements Cloneable {
e.super_getfield("targets", Constants.TYPE_OBJECT_ARRAY);
final Local result2 = result;
EmitUtils.process_array(e, Constants.TYPE_OBJECT_ARRAY, new ProcessArrayCallback() {
@Override
public void processElement(Type type) {
e.checkcast(Type.getType(iface));
e.load_args();
@ -168,11 +184,13 @@ abstract public class MulticastDelegate implements Cloneable {
e.end_method();
}
@Override
protected Object firstInstance(Class type) {
// make a new instance in case first object is used with a long list of targets
return ((MulticastDelegate)ReflectUtils.newInstance(type)).newInstance();
}
@Override
protected Object nextInstance(Object instance) {
return ((MulticastDelegate)instance).newInstance();
}

View File

@ -13,9 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.transform;
import org.springframework.asm.*;
package org.springframework.cglib.transform;
public class ClassFilterTransformer extends AbstractClassFilterTransformer {
private ClassFilter filter;
@ -25,6 +24,7 @@ public class ClassFilterTransformer extends AbstractClassFilterTransformer {
this.filter = filter;
}
@Override
protected boolean accept(int version, int access, String name, String signature, String superName, String[] interfaces) {
return filter.accept(name.replace('/', '.'));
}

View File

@ -13,9 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.transform;
import org.springframework.asm.Attribute;
package org.springframework.cglib.transform;
public interface MethodFilter {
// TODO: pass class name too?

View File

@ -13,21 +13,22 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.transform;
import org.springframework.cglib.core.ClassGenerator;
import org.springframework.cglib.core.Transformer;
import org.springframework.asm.ClassVisitor;
import org.springframework.cglib.core.ClassGenerator;
public class TransformingClassGenerator implements ClassGenerator {
private ClassGenerator gen;
private ClassTransformer t;
private final ClassGenerator gen;
private final ClassTransformer t;
public TransformingClassGenerator(ClassGenerator gen, ClassTransformer t) {
this.gen = gen;
this.t = t;
}
@Override
public void generateClass(ClassVisitor v) throws Exception {
t.setTarget(v);
gen.generateClass(t);

View File

@ -13,20 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.transform;
import java.util.*;
import org.springframework.asm.ClassReader;
import org.springframework.cglib.core.ClassGenerator;
import org.springframework.asm.*;
public class TransformingClassLoader extends AbstractClassLoader {
private ClassTransformerFactory t;
private final ClassTransformerFactory t;
public TransformingClassLoader(ClassLoader parent, ClassFilter filter, ClassTransformerFactory t) {
super(parent, parent, filter);
this.t = t;
}
@Override
protected ClassGenerator getGenerator(ClassReader r) {
ClassTransformer t2 = t.newInstance();
return new TransformingClassGenerator(super.getGenerator(r), t2);

View File

@ -13,14 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.transform.impl;
import org.springframework.cglib.transform.*;
import org.springframework.cglib.core.*;
import org.springframework.asm.ClassVisitor;
import org.springframework.asm.MethodVisitor;
import org.springframework.asm.Attribute;
import org.springframework.asm.Type;
import org.springframework.cglib.core.CodeEmitter;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.core.TypeUtils;
import org.springframework.cglib.transform.ClassEmitterTransformer;
public class AccessFieldTransformer extends ClassEmitterTransformer {
private Callback callback;
@ -33,6 +34,7 @@ public class AccessFieldTransformer extends ClassEmitterTransformer {
String getPropertyName(Type owner, String fieldName);
}
@Override
public void declare_field(int access, final String name, Type type, Object value) {
super.declare_field(access, name, type, value);

View File

@ -13,14 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.transform.impl;
import org.springframework.cglib.transform.*;
import java.lang.reflect.*;
import java.util.*;
import org.springframework.cglib.core.*;
import org.springframework.asm.Attribute;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.springframework.asm.Type;
import org.springframework.cglib.core.CodeEmitter;
import org.springframework.cglib.core.CodeGenerationException;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.core.TypeUtils;
import org.springframework.cglib.transform.ClassEmitterTransformer;
/**
* @author Juozas Baliuka
@ -47,6 +53,7 @@ public class AddDelegateTransformer extends ClassEmitterTransformer {
}
}
@Override
public void begin_class(int version, int access, String className, Type superType, Type[] interfaces, String sourceFile) {
if(!TypeUtils.isInterface(access)){
@ -71,11 +78,13 @@ public class AddDelegateTransformer extends ClassEmitterTransformer {
}
}
@Override
public CodeEmitter begin_method(int access, Signature sig, Type[] exceptions) {
final CodeEmitter e = super.begin_method(access, sig, exceptions);
if (sig.getName().equals(Constants.CONSTRUCTOR_NAME)) {
return new CodeEmitter(e) {
private boolean transformInit = true;
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
super.visitMethodInsn(opcode, owner, name, desc, itf);
if (transformInit && opcode == Constants.INVOKESPECIAL) {
@ -115,6 +124,3 @@ public class AddDelegateTransformer extends ClassEmitterTransformer {
e.end_method();
}
}

View File

@ -13,10 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.transform.impl;
import java.lang.reflect.Method;
import org.springframework.asm.Type;
import org.springframework.cglib.core.CodeEmitter;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.MethodInfo;
@ -24,9 +26,6 @@ import org.springframework.cglib.core.ReflectUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.transform.ClassEmitterTransformer;
import org.springframework.asm.Attribute;
import org.springframework.asm.Type;
/**
* @author Mark Hobson
*/
@ -44,10 +43,12 @@ public class AddInitTransformer extends ClassEmitterTransformer {
}
}
@Override
public CodeEmitter begin_method(int access, Signature sig, Type[] exceptions) {
final CodeEmitter emitter = super.begin_method(access, sig, exceptions);
if (sig.getName().equals(Constants.CONSTRUCTOR_NAME)) {
return new CodeEmitter(emitter) {
@Override
public void visitInsn(int opcode) {
if (opcode == Constants.RETURN) {
load_this();
@ -60,4 +61,3 @@ public class AddInitTransformer extends ClassEmitterTransformer {
return emitter;
}
}

View File

@ -13,14 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.transform.impl;
import org.springframework.cglib.transform.*;
import java.util.*;
import org.springframework.cglib.core.*;
import org.springframework.asm.Attribute;
import java.util.HashMap;
import java.util.Map;
import org.springframework.asm.Label;
import org.springframework.asm.Type;
import org.springframework.cglib.core.CodeEmitter;
import org.springframework.cglib.core.CodeGenerationException;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.EmitUtils;
import org.springframework.cglib.core.ObjectSwitchCallback;
import org.springframework.cglib.core.ProcessSwitchCallback;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.core.TypeUtils;
import org.springframework.cglib.transform.ClassEmitterTransformer;
@SuppressWarnings({"rawtypes", "unchecked"})
public class FieldProviderTransformer extends ClassEmitterTransformer {
@ -48,6 +57,7 @@ public class FieldProviderTransformer extends ClassEmitterTransformer {
private int access;
private Map fields;
@Override
public void begin_class(int version, int access, String className, Type superType, Type[] interfaces, String sourceFile) {
if (!TypeUtils.isAbstract(access)) {
interfaces = TypeUtils.add(interfaces, FIELD_PROVIDER);
@ -57,6 +67,7 @@ public class FieldProviderTransformer extends ClassEmitterTransformer {
super.begin_class(version, access, className, superType, interfaces, sourceFile);
}
@Override
public void declare_field(int access, String name, Type type, Object value) {
super.declare_field(access, name, type, value);
@ -65,6 +76,7 @@ public class FieldProviderTransformer extends ClassEmitterTransformer {
}
}
@Override
public void end_class() {
if (!TypeUtils.isInterface(access)) {
try {
@ -137,12 +149,14 @@ public class FieldProviderTransformer extends ClassEmitterTransformer {
e.load_arg(1);
e.load_arg(0);
e.process_switch(indexes, new ProcessSwitchCallback() {
@Override
public void processCase(int key, Label end) throws Exception {
Type type = (Type)fields.get(names[key]);
e.unbox(type);
e.putfield(names[key]);
e.return_value();
}
@Override
public void processDefault() throws Exception {
e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Unknown field index");
}
@ -155,12 +169,14 @@ public class FieldProviderTransformer extends ClassEmitterTransformer {
e.load_this();
e.load_arg(0);
e.process_switch(indexes, new ProcessSwitchCallback() {
@Override
public void processCase(int key, Label end) throws Exception {
Type type = (Type)fields.get(names[key]);
e.getfield(names[key]);
e.box(type);
e.return_value();
}
@Override
public void processDefault() throws Exception {
e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Unknown field index");
}
@ -175,12 +191,14 @@ public class FieldProviderTransformer extends ClassEmitterTransformer {
e.load_this();
e.load_arg(0);
EmitUtils.string_switch(e, names, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {
@Override
public void processCase(Object key, Label end) {
Type type = (Type)fields.get(key);
e.getfield((String)key);
e.box(type);
e.return_value();
}
@Override
public void processDefault() {
e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Unknown field name");
}
@ -194,12 +212,14 @@ public class FieldProviderTransformer extends ClassEmitterTransformer {
e.load_arg(1);
e.load_arg(0);
EmitUtils.string_switch(e, names, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {
@Override
public void processCase(Object key, Label end) {
Type type = (Type)fields.get(key);
e.unbox(type);
e.putfield((String)key);
e.return_value();
}
@Override
public void processDefault() {
e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Unknown field name");
}

View File

@ -13,18 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.transform.impl;
import java.lang.reflect.Constructor;
import org.springframework.cglib.core.*;
import org.springframework.cglib.transform.*;
import org.springframework.asm.Attribute;
import org.springframework.asm.Type;
import org.springframework.asm.ClassVisitor;
import org.springframework.cglib.core.Block;
import org.springframework.cglib.core.CodeEmitter;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.EmitUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.core.TypeUtils;
import org.springframework.cglib.transform.ClassEmitterTransformer;
@SuppressWarnings({"rawtypes", "unchecked"})
public class UndeclaredThrowableTransformer extends ClassEmitterTransformer {
private Type wrapper;
private final Type wrapper;
public UndeclaredThrowableTransformer(Class wrapper) {
this.wrapper = Type.getType(wrapper);
@ -41,6 +47,7 @@ public class UndeclaredThrowableTransformer extends ClassEmitterTransformer {
throw new IllegalArgumentException(wrapper + " does not have a single-arg constructor that takes a Throwable");
}
@Override
public CodeEmitter begin_method(int access, final Signature sig, final Type[] exceptions) {
CodeEmitter e = super.begin_method(access, sig, exceptions);
if (TypeUtils.isAbstract(access) || sig.equals(Constants.SIG_STATIC)) {
@ -51,6 +58,7 @@ public class UndeclaredThrowableTransformer extends ClassEmitterTransformer {
/* init */ {
handler = begin_block();
}
@Override
public void visitMaxs(int maxStack, int maxLocals) {
handler.end();
EmitUtils.wrap_undeclared_throwable(this, handler, exceptions, wrapper);

View File

@ -13,12 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.util;
import java.lang.reflect.*;
import java.util.Comparator;
import org.springframework.cglib.core.*;
import org.springframework.asm.ClassVisitor;
import org.springframework.cglib.core.AbstractClassGenerator;
import org.springframework.cglib.core.ClassesKey;
import org.springframework.cglib.core.ReflectUtils;
/**
* For the efficient sorting of multiple arrays in parallel.
@ -173,6 +176,7 @@ abstract public class ParallelSorter extends SorterTemplate {
}
}
@Override
protected int compare(int i, int j) {
return comparer.compare(i, j);
}
@ -190,6 +194,7 @@ abstract public class ParallelSorter extends SorterTemplate {
this.cmp = cmp;
}
@Override
public int compare(int i, int j) {
return cmp.compare(a[i], a[j]);
}
@ -198,6 +203,7 @@ abstract public class ParallelSorter extends SorterTemplate {
static class ObjectComparer implements Comparer {
private Object[] a;
public ObjectComparer(Object[] a) { this.a = a; }
@Override
public int compare(int i, int j) {
return ((Comparable)a[i]).compareTo(a[j]);
}
@ -206,12 +212,14 @@ abstract public class ParallelSorter extends SorterTemplate {
static class IntComparer implements Comparer {
private int[] a;
public IntComparer(int[] a) { this.a = a; }
@Override
public int compare(int i, int j) { return a[i] - a[j]; }
}
static class LongComparer implements Comparer {
private long[] a;
public LongComparer(long[] a) { this.a = a; }
@Override
public int compare(int i, int j) {
long vi = a[i];
long vj = a[j];
@ -222,6 +230,7 @@ abstract public class ParallelSorter extends SorterTemplate {
static class FloatComparer implements Comparer {
private float[] a;
public FloatComparer(float[] a) { this.a = a; }
@Override
public int compare(int i, int j) {
float vi = a[i];
float vj = a[j];
@ -232,6 +241,7 @@ abstract public class ParallelSorter extends SorterTemplate {
static class DoubleComparer implements Comparer {
private double[] a;
public DoubleComparer(double[] a) { this.a = a; }
@Override
public int compare(int i, int j) {
double vi = a[i];
double vj = a[j];
@ -242,12 +252,14 @@ abstract public class ParallelSorter extends SorterTemplate {
static class ShortComparer implements Comparer {
private short[] a;
public ShortComparer(short[] a) { this.a = a; }
@Override
public int compare(int i, int j) { return a[i] - a[j]; }
}
static class ByteComparer implements Comparer {
private byte[] a;
public ByteComparer(byte[] a) { this.a = a; }
@Override
public int compare(int i, int j) { return a[i] - a[j]; }
}
@ -260,6 +272,7 @@ abstract public class ParallelSorter extends SorterTemplate {
super(SOURCE);
}
@Override
protected ClassLoader getDefaultClassLoader() {
return null; // TODO
}
@ -272,6 +285,7 @@ abstract public class ParallelSorter extends SorterTemplate {
return (ParallelSorter)super.create(ClassesKey.create(arrays));
}
@Override
public void generateClass(ClassVisitor v) throws Exception {
if (arrays.length == 0) {
throw new IllegalArgumentException("No arrays specified to sort");
@ -284,10 +298,12 @@ abstract public class ParallelSorter extends SorterTemplate {
new ParallelSorterEmitter(v, getClassName(), arrays);
}
@Override
protected Object firstInstance(Class type) {
return ((ParallelSorter)ReflectUtils.newInstance(type)).newInstance(arrays);
}
@Override
protected Object nextInstance(Object instance) {
return ((ParallelSorter)instance).newInstance(arrays);
}

View File

@ -13,13 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.util;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.springframework.cglib.core.*;
import org.springframework.asm.ClassVisitor;
import org.springframework.asm.Type;
import org.springframework.cglib.core.ClassEmitter;
import org.springframework.cglib.core.CodeEmitter;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.EmitUtils;
import org.springframework.cglib.core.Local;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.core.TypeUtils;
class ParallelSorterEmitter extends ClassEmitter {
private static final Type PARALLEL_SORTER =

View File

@ -13,9 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.util;
import java.util.*;
package org.springframework.cglib.util;
abstract class SorterTemplate {
private static final int MERGESORT_THRESHOLD = 12;

View File

@ -13,16 +13,28 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cglib.util;
import java.util.*;
import org.springframework.cglib.core.*;
import java.util.Arrays;
import java.util.List;
import org.springframework.asm.ClassVisitor;
import org.springframework.asm.Label;
import org.springframework.asm.Type;
import org.springframework.cglib.core.AbstractClassGenerator;
import org.springframework.cglib.core.ClassEmitter;
import org.springframework.cglib.core.CodeEmitter;
import org.springframework.cglib.core.Constants;
import org.springframework.cglib.core.EmitUtils;
import org.springframework.cglib.core.KeyFactory;
import org.springframework.cglib.core.ObjectSwitchCallback;
import org.springframework.cglib.core.ReflectUtils;
import org.springframework.cglib.core.Signature;
import org.springframework.cglib.core.TypeUtils;
/**
* This class implements a simple String->int mapping for a fixed set of keys.
* This class implements a simple String &rarr; int mapping for a fixed set of keys.
*/
@SuppressWarnings({"rawtypes", "unchecked"})
abstract public class StringSwitcher {
@ -104,6 +116,7 @@ abstract public class StringSwitcher {
this.fixedInput = fixedInput;
}
@Override
protected ClassLoader getDefaultClassLoader() {
return getClass().getClassLoader();
}
@ -117,6 +130,7 @@ abstract public class StringSwitcher {
return (StringSwitcher)super.create(key);
}
@Override
public void generateClass(ClassVisitor v) throws Exception {
ClassEmitter ce = new ClassEmitter(v);
ce.begin_class(Constants.V1_8,
@ -131,10 +145,12 @@ abstract public class StringSwitcher {
final List stringList = Arrays.asList(strings);
int style = fixedInput ? Constants.SWITCH_STYLE_HASHONLY : Constants.SWITCH_STYLE_HASH;
EmitUtils.string_switch(e, strings, style, new ObjectSwitchCallback() {
@Override
public void processCase(Object key, Label end) {
e.push(ints[stringList.indexOf(key)]);
e.return_value();
}
@Override
public void processDefault() {
e.push(-1);
e.return_value();
@ -144,10 +160,12 @@ abstract public class StringSwitcher {
ce.end_class();
}
@Override
protected Object firstInstance(Class type) {
return (StringSwitcher)ReflectUtils.newInstance(type);
return ReflectUtils.newInstance(type);
}
@Override
protected Object nextInstance(Object instance) {
return instance;
}