protected static Object instantiate(Class<?> type) { Object returnValue; try { returnValue = type.newInstance(); } catch (Throwable throwable) { throw new RuntimeException( "Failed to instantiate a new instance of: \'" + type.getName() + "\'", throwable); } return returnValue; }
@Override public <E, L> EventExecutor<E, L> create(EventBus<E, L> eventBus, Method method) { RegisteredListener.validate( Objects.requireNonNull(eventBus, "Null eventBus"), Objects.requireNonNull(method, "Null method")); Class<? extends EventExecutor> executorClass = cache.computeIfAbsent(method, this::generateExecutor); try { return executorClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException("Unable to initialize " + executorClass, e); } }
public static FieldAccess get(Class type) { ArrayList<Field> fields = new ArrayList<Field>(); Class nextClass = type; while (nextClass != Object.class) { Field[] declaredFields = nextClass.getDeclaredFields(); fields.addAll(Arrays.asList(declaredFields)); nextClass = nextClass.getSuperclass(); } String[] fieldNames = new String[fields.size()]; Class[] fieldTypes = new Class[fields.size()]; for (int i = 0, n = fieldNames.length; i < n; i++) { fieldNames[i] = fields.get(i).getName(); fieldTypes[i] = fields.get(i).getType(); } String className = type.getName(); String accessClassName = className + "FieldAccess"; if (accessClassName.startsWith("java.")) accessClassName = "reflectasm." + accessClassName; Class accessClass = null; AccessClassLoader loader = AccessClassLoader.get(type); synchronized (loader) { try { accessClass = loader.loadClass(accessClassName); } catch (ClassNotFoundException ignored) { String accessClassNameInternal = accessClassName.replace('.', '/'); String classNameInternal = className.replace('.', '/'); ClassWriter cw = new ClassWriter(0); cw.visit( V1_1, ACC_PUBLIC + ACC_SUPER, accessClassNameInternal, null, "com/esotericsoftware/reflectasm/FieldAccess", null); insertConstructor(cw); insertGetObject(cw, classNameInternal, fields); insertSetObject(cw, classNameInternal, fields); insertGetPrimitive(cw, classNameInternal, fields, Type.BOOLEAN_TYPE); insertSetPrimitive(cw, classNameInternal, fields, Type.BOOLEAN_TYPE); insertGetPrimitive(cw, classNameInternal, fields, Type.BYTE_TYPE); insertSetPrimitive(cw, classNameInternal, fields, Type.BYTE_TYPE); insertGetPrimitive(cw, classNameInternal, fields, Type.SHORT_TYPE); insertSetPrimitive(cw, classNameInternal, fields, Type.SHORT_TYPE); insertGetPrimitive(cw, classNameInternal, fields, Type.INT_TYPE); insertSetPrimitive(cw, classNameInternal, fields, Type.INT_TYPE); insertGetPrimitive(cw, classNameInternal, fields, Type.LONG_TYPE); insertSetPrimitive(cw, classNameInternal, fields, Type.LONG_TYPE); insertGetPrimitive(cw, classNameInternal, fields, Type.DOUBLE_TYPE); insertSetPrimitive(cw, classNameInternal, fields, Type.DOUBLE_TYPE); insertGetPrimitive(cw, classNameInternal, fields, Type.FLOAT_TYPE); insertSetPrimitive(cw, classNameInternal, fields, Type.FLOAT_TYPE); insertGetPrimitive(cw, classNameInternal, fields, Type.CHAR_TYPE); insertSetPrimitive(cw, classNameInternal, fields, Type.CHAR_TYPE); insertGetString(cw, classNameInternal, fields); cw.visitEnd(); accessClass = loader.defineClass(accessClassName, cw.toByteArray()); } } try { FieldAccess access = (FieldAccess) accessClass.newInstance(); access.fieldNames = fieldNames; access.fieldTypes = fieldTypes; return access; } catch (Throwable t) { throw new RuntimeException("Error constructing field access class: " + accessClassName, t); } }
@SuppressWarnings({"rawtypes", "unchecked"}) public static <T> ConstructorAccess<T> get(Class<T> type) { try { type.getConstructor((Class[]) null); } catch (Exception ex) { if (type.isMemberClass() && !Modifier.isStatic(type.getModifiers())) throw new RuntimeException( "Class cannot be created (non-static member class): " + type.getName()); else throw new RuntimeException( "Class cannot be created (missing no-arg constructor): " + type.getName()); } AccessClassLoader loader = AccessClassLoader.get(type); String className = type.getName(); String accessClassName = className + "ConstructorAccess"; if (accessClassName.startsWith("java.")) accessClassName = "reflectasm." + accessClassName; Class accessClass = null; try { accessClass = loader.loadClass(accessClassName); } catch (ClassNotFoundException ignored) { } if (accessClass == null) { String accessClassNameInternal = accessClassName.replace('.', '/'); String classNameInternal = className.replace('.', '/'); ClassWriter cw = new ClassWriter(0); cw.visit( V1_1, ACC_PUBLIC + ACC_SUPER, accessClassNameInternal, null, "com/esotericsoftware/reflectasm/ConstructorAccess", null); MethodVisitor mv; { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn( INVOKESPECIAL, "com/esotericsoftware/reflectasm/ConstructorAccess", "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(1, 1); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "newInstance", "()Ljava/lang/Object;", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, classNameInternal); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, classNameInternal, "<init>", "()V"); mv.visitInsn(ARETURN); mv.visitMaxs(2, 1); mv.visitEnd(); } cw.visitEnd(); byte[] data = cw.toByteArray(); accessClass = loader.defineClass(accessClassName, data); } try { return (ConstructorAccess) accessClass.newInstance(); } catch (Exception ex) { throw new RuntimeException( "Error constructing constructor access class: " + accessClassName, ex); } }