public AllParameterNamesDiscoveringVisitor(Class type) { super(Opcodes.ASM7); this.methodName = "<init>"; List<Constructor> constructors = new ArrayList<Constructor>(Arrays.asList(type.getConstructors())); constructors.addAll(Arrays.asList(type.getDeclaredConstructors())); for (Constructor constructor : constructors) { Type[] types = new Type[constructor.getParameterTypes().length]; for (int j = 0; j < types.length; j++) { types[j] = Type.getType(constructor.getParameterTypes()[j]); } constructorMap.put(Type.getMethodDescriptor(Type.VOID_TYPE, types), constructor); } }
public MethodInfo(ClassInfo info, Constructor constructor) { super(constructor); this.declaringClass = info; this.name = "<init>"; this.descriptor = Type.getConstructorDescriptor(constructor); }
public AnnotationInfo(Annotation annotation) { this(Type.getType(annotation.annotationType()).getDescriptor()); }
public String getType() { // if this method starts to be used internally move this to constructors and just return type final Type t = Type.getType(type); if (t.getClassName() == null) { return t.getDescriptor(); } return t.getClassName(); }
String methodDescriptor = Type.getMethodDescriptor(delegatedMethod); for (int i = 0; i < exceptionTypes.length; i++) exceptionTypeNames[i] = Type.getType(exceptionTypes[i]).getInternalName(); for (Class<?> aClass : delegatedMethod.getParameterTypes()) Type type = Type.getType(aClass); mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), offset); offset += type.getSize(); Type declaringClass = Type.getType(delegatedMethod.getDeclaringClass()); if (!abstractMethod) mv.visitMethodInsn(Opcodes.INVOKESPECIAL, declaringClass.getInternalName(), delegatedMethod.getName(), methodDescriptor, false);
MethodVisitor mv = cw.visitMethod(modifier, method.getName(), Type.getMethodDescriptor(method), null, null); mv.visitCode(); mv.visitLdcInsn(Type.getType("L" + classNameToOverride + ";")); mv.visitFieldInsn(GETSTATIC, wrapperType, "TYPE", "Ljava/lang/Class;"); } else { mv.visitLdcInsn(Type.getType(parameterType)); Type.getDescriptor(InterceptorHandler.class)); mv.visitFieldInsn(GETSTATIC, proxyClassFileName, FIELD_INTERCEPTED_METHODS, Type.getDescriptor(Method[].class)); .visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(InterceptorHandler.class), "invoke", "(Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;", true); "()" + Type.getDescriptor(returnType), false); Class<?> exceptionType = exceptionTypes[i]; mv.visitLdcInsn(Type.getType("L" + exceptionType.getCanonicalName().replace('.', '/') + ";")); mv.visitVarInsn(ALOAD, length); mv
String[] exceptions = null; if (classToProxy.isInterface()) { parentClassFileName = Type.getInternalName(Object.class); superDefaultCt = Object.class.getConstructor(); } else { exceptions = exceptionTypes.length == 0 ? null : new String[exceptionTypes.length]; for (int i = 0; i < exceptionTypes.length; i++) { exceptions[i] = Type.getInternalName(exceptionTypes[i]); final String descriptor = Type.getConstructorDescriptor(superDefaultCt); final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions); mv.visitCode(); mv .visitFieldInsn(PUTFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class));
@Override protected void createSerialisation(ClassWriter cw, String proxyClassFileName, Class<?> classToProxy, String classFileName) { String[] exceptionTypeNames = {Type.getType(ObjectStreamException.class).getInternalName()}; MethodVisitor mv = cw.visitMethod(Modifier.PUBLIC, "writeReplace", "()Ljava/lang/Object;", null, exceptionTypeNames); // fill method body mv.visitCode(); // load the contextual instance Provider mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INSTANCE_PROVIDER, Type.getDescriptor(Provider.class)); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(-1, -1); mv.visitEnd(); }
MethodVisitor mv = cw.visitMethod(modifier, method.getName(), Type.getMethodDescriptor(method), null, null); mv.visitCode(); mv.visitFieldInsn(Opcodes.GETSTATIC, proxyClassFileName, FIELD_PROTECTED_METHODS, Type.getDescriptor(Method[].class)); mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INSTANCE_PROVIDER, Type.getDescriptor(Provider.class)); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Provider.class), "get", "()Ljava/lang/Object;", true); mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(NormalScopeProxyFactory.class), "delegateProtectedMethod", "(Ljava/lang/reflect/Method;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false); "()" + Type.getDescriptor(returnType), false);
final String classFileName = proxyClassName.replace('.', '/'); final String[] interfaceNames = { Type.getInternalName(Serializable.class) }; final String superClassName = Type.getInternalName(classToProxy); if (hasInterceptors) { cw .visitField(ACC_PRIVATE, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class), null, null) .visitEnd(); cw .visitField(ACC_PRIVATE | ACC_STATIC, FIELD_INTERCEPTED_METHODS, Type.getDescriptor(Method[].class), null, null) .visitEnd();
private void createSerialisation(final ClassWriter cw, final String pluginId, final String key) { final MethodVisitor mv = cw .visitMethod(Modifier.PUBLIC, "writeReplace", "()Ljava/lang/Object;", null, new String[] { Type.getType(ObjectStreamException.class).getInternalName() }); mv.visitCode(); mv.visitTypeInsn(NEW, "org/talend/sdk/component/runtime/serialization/SerializableService"); mv.visitInsn(DUP); mv.visitLdcInsn(pluginId); mv.visitLdcInsn(key); mv .visitMethodInsn(INVOKESPECIAL, "org/talend/sdk/component/runtime/serialization/SerializableService", "<init>", "(Ljava/lang/String;Ljava/lang/String;)V", false); mv.visitInsn(ARETURN); mv.visitMaxs(-1, -1); mv.visitEnd(); }
@Override protected void createInstanceVariables(ClassWriter cw, Class<?> classToProxy, String classFileName) { // variable #1, the Provider<?> for the Contextual Instance cw.visitField(Opcodes.ACC_PRIVATE, FIELD_INSTANCE_PROVIDER, Type.getDescriptor(Provider.class), null, null).visitEnd(); // variable #2, the Method[] for all protected methods cw.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, FIELD_PROTECTED_METHODS, Type.getDescriptor(Method[].class), null, null).visitEnd(); }
public MethodInfo(ClassInfo info, Method method) { super(method); this.declaringClass = info; this.name = method.getName(); this.descriptor = Type.getMethodDescriptor(method); this.method = method; }
if (classToProxy.isInterface()) parentClassFileName = Type.getInternalName(Object.class); superDefaultCt = Object.class.getConstructor(null); for (int i = 0; i < exceptionTypes.length; i++) exceptions[i] = Type.getInternalName(exceptionTypes[i]); String descriptor = Type.getConstructorDescriptor(superDefaultCt); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", descriptor, null, exceptions); mv.visitCode();
private String getCastType(final Class<?> returnType) { if (returnType.isPrimitive()) { return getWrapperType(returnType); } return Type.getInternalName(returnType); }
private Member toMethod() throws ClassNotFoundException { org.objectweb.asm.commons.Method method = new org.objectweb.asm.commons.Method(name, descriptor); Class<?> clazz = this.declaringClass.get(); List<Class> parameterTypes = new LinkedList<Class>(); for (Type type : method.getArgumentTypes()) { String paramType = type.getClassName(); try { parameterTypes.add(Classes.forName(paramType, clazz.getClassLoader())); } catch (ClassNotFoundException cnfe) { throw new IllegalStateException("Parameter class could not be loaded for type " + paramType, cnfe); } } Class[] parameters = parameterTypes.toArray(new Class[parameterTypes.size()]); IllegalStateException noSuchMethod = null; while (clazz != null) { try { if (name.equals("<init>")) { return clazz.getDeclaredConstructor(parameters); } else { return clazz.getDeclaredMethod(name, parameters); } } catch (NoSuchMethodException e) { if (noSuchMethod == null) { noSuchMethod = new IllegalStateException("Callback method does not exist: " + clazz.getName() + "." + name, e); } clazz = clazz.getSuperclass(); } } throw noSuchMethod; }
String methodDescriptor = Type.getMethodDescriptor(delegatedMethod); for (int i = 0; i < exceptionTypes.length; i++) exceptionTypeNames[i] = Type.getType(exceptionTypes[i]).getInternalName(); mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INSTANCE_PROVIDER, Type.getDescriptor(Provider.class)); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(Provider.class), "get", "()Ljava/lang/Object;", true); mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(classToProxy)); for (Class<?> aClass : delegatedMethod.getParameterTypes()) Type type = Type.getType(aClass); mv.visitVarInsn(type.getOpcode(Opcodes.ILOAD), offset); offset += type.getSize(); Type declaringClass = Type.getType(delegatedMethod.getDeclaringClass()); boolean interfaceMethod = Modifier.isInterface(delegatedMethod.getDeclaringClass().getModifiers()); mv.visitMethodInsn(interfaceMethod ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, declaringClass.getInternalName(), delegatedMethod.getName(), methodDescriptor, interfaceMethod);
MethodVisitor mv = cw.visitMethod(modifier, method.getName(), Type.getMethodDescriptor(method), null, null); mv.visitCode(); mv.visitLdcInsn(Type.getType("L" + classNameToOverride + ";")); mv.visitLdcInsn(Type.getType(parameterType)); mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class)); mv.visitFieldInsn(Opcodes.GETSTATIC, proxyClassFileName, FIELD_INTERCEPTED_METHODS, Type.getDescriptor(Method[].class)); mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(InterceptorHandler.class), "invoke", "(Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;", true); "()" + Type.getDescriptor(returnType), false); mv.visitLdcInsn(Type.getType("L" + exceptionType.getCanonicalName().replace('.', '/') + ";")); mv.visitVarInsn(Opcodes.ALOAD, length); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause",
if (classToProxy.isInterface()) parentClassFileName = Type.getInternalName(Object.class); superDefaultCt = Object.class.getConstructor(null); String descriptor = Type.getConstructorDescriptor(superDefaultCt); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", descriptor, null, null); mv.visitCode(); mv.visitFieldInsn(Opcodes.PUTFIELD, proxyClassFileName, FIELD_INSTANCE_PROVIDER, Type.getDescriptor(Provider.class));
@Override protected void createSerialisation(ClassWriter cw, String proxyClassFileName, Class<?> classToProxy, String classFileName) { String[] exceptionTypeNames = {Type.getType(ObjectStreamException.class).getInternalName()}; MethodVisitor mv = cw.visitMethod(Modifier.PUBLIC, "writeReplace", "()Ljava/lang/Object;", null, exceptionTypeNames); // fill method body mv.visitCode(); // load the contextual instance Provider mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitFieldInsn(Opcodes.GETFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class)); mv.visitInsn(Opcodes.ARETURN); mv.visitMaxs(-1, -1); mv.visitEnd(); }