@Test public void testDSL() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { final String className = "helloTest"; JiteClass jiteClass = new JiteClass(className) {{ defineMethod("main", ACC_PUBLIC | ACC_STATIC, sig(void.class, String[].class), new CodeBlock() {{ ldc("helloWorld"); getstatic(p(System.class), "out", ci(PrintStream.class)); swap(); invokevirtual(p(PrintStream.class), "println", sig(void.class, Object.class)); voidreturn(); }}); defineMethod("hello", ACC_PUBLIC | ACC_STATIC, sig(String.class), newCodeBlock() .ldc("helloWorld") .areturn() ); }}; Class<?> clazz = new DynamicClassLoader().define(jiteClass); Method helloMethod = clazz.getMethod("hello"); Object result = helloMethod.invoke(null); Assert.assertEquals("helloWorld", result); Method mainMethod = clazz.getMethod("main", String[].class); mainMethod.invoke(null, (Object) new String[]{}); }
public void defineMethod(String methodName, int modifiers, String signature, CodeBlock methodBody) { this.methods.add(new MethodDefinition(methodName, modifiers, signature, methodBody)); }
JiteClass implements Opcodes { public void defineMethod(String methodName, int modifiers, String signature, CodeBlock methodBody) { this.methods.add(new MethodDefinition(methodName, modifiers, signature, methodBody)); } }
JiteClass implements Opcodes { public void defineMethod(String methodName, int modifiers, String signature, CodeBlock methodBody) { this.methods.add(new MethodDefinition(methodName, modifiers, signature, methodBody)); } JiteClass(String className);  JiteClass(String className, String[] interfaces);  JiteClass(String className, String superClassName, String[] interfaces); }
JiteClass implements Opcodes { public void defineMethod(String methodName, int modifiers, String signature, CodeBlock methodBody) { this.methods.add(new MethodDefinition(methodName, modifiers, signature, methodBody)); } JiteClass(String className);  JiteClass(String className, String[] interfaces);  JiteClass(String className, String superClassName, String[] interfaces); AnnotationData annotate(Class<?> annotationType); int getAccess(); String getClassName(); String getParentClassName(); String reserveLambda(); void setAccess(int access); void setParentClassName(String parentClassName); void setSourceFile(String sourceFile); void setSourceDebug(String sourceDebug); void addChildClass(JiteClass child); void addChildClass(String innerName, JiteClass child); List<JiteClass> getChildClasses(); void defineMethod(String methodName, int modifiers, String signature, CodeBlock methodBody); FieldDefinition defineField(String fieldName, int modifiers, String signature, Object value); void defineDefaultConstructor(); void defineDefaultConstructor(int access); byte[] toBytes(); void addAnnotation(AnnotationData annotation); byte[] toBytes(JDKVersion version); }
JiteClass implements Opcodes { public void defineMethod(String methodName, int modifiers, String signature, CodeBlock methodBody) { this.methods.add(new MethodDefinition(methodName, modifiers, signature, methodBody)); } JiteClass(String className);  JiteClass(String className, String[] interfaces);  JiteClass(String className, String superClassName, String[] interfaces); AnnotationData annotate(Class<?> annotationType); int getAccess(); String getClassName(); String getParentClassName(); String reserveLambda(); void setAccess(int access); void setParentClassName(String parentClassName); void setSourceFile(String sourceFile); void setSourceDebug(String sourceDebug); void addChildClass(JiteClass child); void addChildClass(String innerName, JiteClass child); List<JiteClass> getChildClasses(); void defineMethod(String methodName, int modifiers, String signature, CodeBlock methodBody); FieldDefinition defineField(String fieldName, int modifiers, String signature, Object value); void defineDefaultConstructor(); void defineDefaultConstructor(int access); byte[] toBytes(); void addAnnotation(AnnotationData annotation); byte[] toBytes(JDKVersion version); static final String[] INTERFACES; }