@Override protected Class<?> findClass(String name) throws ClassNotFoundException { ClassGen cg = new ClassGen(name, Object.class.getName(), "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, new String[] {Serializable.class.getName()}); cg.addEmptyConstructor(Constants.ACC_PUBLIC); JavaClass jClazz = cg.getJavaClass(); byte[] bytes = jClazz.getBytes(); return defineClass(jClazz.getClassName(), bytes, 0, bytes.length); }
/** * <pre> * public class SerializableImpl implements Serializable { * * public SerializableImpl() {} * * } * </pre> */ private Class<?> generateSerializableImpl() throws ClassNotFoundException { ClassGen cg = new ClassGen(CLASS_NAME_SERIALIZABLE_IMPL, Object.class.getName(), GENERATED, Constants.ACC_PUBLIC | Constants.ACC_SUPER, new String[] {Serializable.class.getName()}); cg.addEmptyConstructor(Constants.ACC_PUBLIC); JavaClass jClazz = cg.getJavaClass(); byte[] bytes = jClazz.getBytes(); return defineClass(jClazz.getClassName(), bytes, 0, bytes.length); }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { ClassGen cg = new ClassGen(name, "java.lang.Object", "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null); cg.addEmptyConstructor(Constants.ACC_PUBLIC); JavaClass jClazz = cg.getJavaClass(); byte[] bytes = jClazz.getBytes(); return defineClass(jClazz.getClassName(), bytes, 0, bytes.length); }
setter.dispose(); JavaClass jClazz = cg.getJavaClass(); byte[] bytes = jClazz.getBytes(); return defineClass(jClazz.getClassName(), bytes, 0, bytes.length);
public static void main(String[] args) throws Exception { System.out.println(F.class.getField("a").getModifiers()); JavaClass aClass = Repository.lookupClass(F.class); ClassGen aGen = new ClassGen(aClass); for (Field field : aGen.getFields()) { if (field.getName().equals("a")) { int mods = field.getModifiers(); field.setModifiers(mods | Modifier.FINAL); } } final byte[] classBytes = aGen.getJavaClass().getBytes(); ClassLoader cl = new ClassLoader(null) { @Override protected synchronized Class<?> findClass(String name) throws ClassNotFoundException { return defineClass("F", classBytes, 0, classBytes.length); } }; Class<?> fWithoutDeprecated = cl.loadClass("F"); System.out.println(fWithoutDeprecated.getField("a").getModifiers()); }
public static void dumpJar(String path, Collection<ClassGen> cgs) { FileOutputStream os; path = path.replace(".jar", "") + "-deob.jar"; try { os = new FileOutputStream(new File(path)); } catch (FileNotFoundException fnfe) { throw new RuntimeException("could not create file \"" + path + "\": " + fnfe); } JarOutputStream jos; try { jos = new JarOutputStream(os); for (JarEntry jbe : NonClassEntries.entries) { JarEntry destEntry = new JarEntry(jbe.getName()); byte[] bite = IOUtils.toByteArray(NonClassEntries.ins.get(jbe)); jos.putNextEntry(destEntry); jos.write(bite); jos.closeEntry(); } for (ClassGen classIt : cgs) { JarEntry classEntry = new JarEntry(classIt.getClassName().replace('.', '/') + ".class"); jos.putNextEntry(classEntry); jos.write(classIt.getJavaClass().getBytes()); jos.closeEntry(); } jos.closeEntry(); jos.close(); } catch (IOException ignored) { } }
if (this.baseDir == null) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); cg.getJavaClass().dump(out); final Class< ? > clazzB = ((ByteClassLoader) loader).loadClass(className, out.toByteArray()); return (T) clazzB.newInstance(); try { fos = new FileOutputStream(clazz); cg.getJavaClass().dump(fos); } finally { if (fos != null) {
public void dumpJar(String path) { FileOutputStream os; try { os = new FileOutputStream(new File(path)); } catch (FileNotFoundException fnfe) { throw new RuntimeException("could not create file \"" + path + "\": " + fnfe); } JarOutputStream jos; try { jos = new JarOutputStream(os); for (JarEntry jbe : NonClassEntries.entries) { JarEntry destEntry = new JarEntry(jbe.getName()); byte[] bite = IOUtils.toByteArray(NonClassEntries.ins.get(jbe)); jos.putNextEntry(destEntry); jos.write(bite); jos.closeEntry(); } for (ClassGen classIt : cgs.values()) { jos.putNextEntry(new JarEntry(classIt.getClassName().replace('.', '/') + ".class")); jos.write(classIt.getJavaClass().getBytes()); jos.closeEntry(); jos.flush(); } jos.closeEntry(); jos.close(); } catch (IOException ioe) { } }
public void dumpJar(String path) { FileOutputStream os; try { os = new FileOutputStream(new File(path)); } catch (FileNotFoundException fnfe) { throw new RuntimeException("could not create file \"" + path + "\": " + fnfe); } JarOutputStream jos; try { jos = new JarOutputStream(os); for (ClassGen classIt : cgs.values()) { jos.putNextEntry(new JarEntry(classIt.getClassName().replace('.', '/') + ".class")); jos.write(classIt.getJavaClass().getBytes()); jos.closeEntry(); jos.flush(); } for (JarEntry jbe : NonClassEntries.entries) { if (!jbe.isDirectory()) { JarEntry destEntry = new JarEntry(jbe.getName()); byte[] bite = IOUtils.toByteArray(NonClassEntries.ins.get(jbe)); jos.putNextEntry(destEntry); jos.write(bite); jos.closeEntry(); } } jos.closeEntry(); jos.close(); } catch (IOException ioe) { } }
private void dumpClass(final ClassGen cg, final String fname) { try { final File f = createTestdataFile(fname); cg.getJavaClass().dump(f); } catch (final java.io.IOException e) { System.err.println(e); } }
String cd = new File(getClassGenDir()).getCanonicalPath(); fos = new FileOutputStream(cd + File.separator + generatedClassName + ".class"); classGen.getJavaClass().dump(fos); fos.close(); } catch (FileNotFoundException fnfe) {
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { ClassGen cg = new ClassGen(name, "java.lang.Object", "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null); cg.addEmptyConstructor(Constants.ACC_PUBLIC); JavaClass jClazz = cg.getJavaClass(); byte[] bytes = jClazz.getBytes(); return defineClass(jClazz.getClassName(), bytes, 0, bytes.length); }
import java.io.FileOutputStream; import org.apache.bcel.Const; import org.apache.bcel.generic.*; public class CreateB { public static void main(String[] args) throws Exception { ClassGen cg = new ClassGen("B", "A", "B.java", Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {}); ConstantPoolGen cp = cg.getConstantPool(); InstructionList il = new InstructionList(); MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", "B", il, cp); il.append(InstructionFactory.createReturn(Type.VOID)); method.setMaxStack(); method.setMaxLocals(); cg.addMethod(method.getMethod()); il.dispose(); cg.getJavaClass().dump(new FileOutputStream("B.class")); } }
@Override public void create(final OutputStream out) throws IOException { createMethod_0(); createMethod_1(); _cg.getJavaClass().dump(out); }
@Override public void create(final OutputStream out) throws IOException { createMethod_0(); createMethod_1(); _cg.getJavaClass().dump(out); }
@Override public void create(final OutputStream out) throws IOException { createMethod_0(); createMethod_1(); _cg.getJavaClass().dump(out); }
@Override public void create(final OutputStream out) throws IOException { createMethod_0(); createMethod_1(); _cg.getJavaClass().dump(out); }
@Override public void create(final OutputStream out) throws IOException { createMethod_0(); createMethod_1(); _cg.getJavaClass().dump(out); }
/** * Generates the wrapper byte code for a given interface. * * @param interfacesToImplement The interfaces we want to generate wrapper * byte code for * @return byte[] The generated byte code */ private byte[] buildWrapper( final JavaClass[] interfacesToImplement ) throws Exception { // Create field for the wrapped class m_classGenerator.addField( m_codeGenerator.createWrappedClassField() ); // Create default constructor m_classGenerator.addMethod( m_codeGenerator.createDefaultConstructor() ); //Create field accessor for wrapped class instance m_classGenerator.addMethod( m_codeGenerator.createWrappedClassAccessor() ); // Implement interfaces Method[] interfaceMethods = m_codeGenerator.createImplementation( interfacesToImplement ); for ( int j = 0; j < interfaceMethods.length; ++j ) { m_classGenerator.addMethod( interfaceMethods[j] ); } return m_classGenerator.getJavaClass().getBytes(); } }
public JavaClass generate(SourceOfRandomness r, GenerationStatus s) { constants = new ConstantPoolGen(); // Generate a class with its meta-data String className = "example.A"; String superName = r.nextBoolean() ? "example.B" : "java.lang.Object"; String fileName = "A.class"; int flags = r.nextInt(0, Short.MAX_VALUE); int numInterfaces = r.nextBoolean() ? 0 : geom.sampleWithMean(MEAN_INTERFACE_COUNT, r); String[] interfaces = new String[numInterfaces]; for (int i = 0; i < numInterfaces; i++) { interfaces[i] = "example.I"+i; } ClassGen classGen = new ClassGen(className, superName, fileName, flags, interfaces, constants); // Validate flags Assume.assumeFalse(classGen.isFinal() && (classGen.isAbstract() | classGen.isInterface())); int numFields = geom.sampleWithMean(MEAN_FIELDS_COUNT, r); for (int i = 0; i < numFields; i++) { classGen.addField(generateField(r)); } int numMethods = geom.sampleWithMean(MEAN_METHODS_COUNT, r); for (int i = 0; i < numMethods; i++) { classGen.addMethod(generateMethod(className, r)); } return classGen.getJavaClass(); }