use of jodd.asm5.ClassWriter in project jodd by oblac.
the class ProxettaBuilder method process.
/**
* Reads the target and creates destination class.
*/
protected void process() {
if (targetInputStream == null) {
throw new ProxettaException("Target missing");
}
// create class reader
ClassReader classReader;
try {
classReader = new ClassReader(targetInputStream);
} catch (IOException ioex) {
throw new ProxettaException("Error reading class input stream", ioex);
}
// reads information
TargetClassInfoReader targetClassInfoReader = new TargetClassInfoReader(proxetta.getClassLoader());
classReader.accept(targetClassInfoReader, 0);
this.destClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
// create proxy
if (log.isDebugEnabled()) {
log.debug("processing: " + classReader.getClassName());
}
WorkData wd = process(classReader, targetClassInfoReader);
// store important data
proxyApplied = wd.proxyApplied;
proxyClassName = wd.thisReference.replace('/', '.');
}
use of jodd.asm5.ClassWriter in project tomee by apache.
the class DynamicSubclass method generateBytes.
private static byte[] generateBytes(final Class<?> classToProxy) throws ProxyGenerationException {
final Map<String, MethodVisitor> visitors = new HashMap<>();
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
final String proxyClassFileName = getSubclassName(classToProxy).replace('.', '/');
final String classFileName = classToProxy.getName().replace('.', '/');
cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, proxyClassFileName, null, classFileName, null);
cw.visitSource(classFileName + ".java", null);
// push InvocationHandler field
cw.visitField(ACC_FINAL + ACC_PRIVATE, "this$handler", "Ljava/lang/reflect/InvocationHandler;", null, null).visitEnd();
for (final Constructor<?> constructor : classToProxy.getConstructors()) {
if (!Modifier.isPublic(constructor.getModifiers())) {
continue;
}
final MethodVisitor mv = visitConstructor(cw, proxyClassFileName, classFileName, constructor);
visitors.put("<init>" + Type.getConstructorDescriptor(constructor), mv);
}
final Map<String, List<Method>> methodMap = new HashMap<>();
getNonPrivateMethods(classToProxy, methodMap);
// Iterate over the public methods
for (final Map.Entry<String, List<Method>> entry : methodMap.entrySet()) {
for (final Method method : entry.getValue()) {
if (Modifier.isAbstract(method.getModifiers())) {
final MethodVisitor visitor = LocalBeanProxyFactory.visit(cw, method, proxyClassFileName, "this$handler");
visitors.put(method.getName() + Type.getMethodDescriptor(method), visitor);
}
}
}
copyClassAnnotations(classToProxy, cw);
copyMethodAnnotations(classToProxy, visitors);
// This should never be reached, but just in case
for (final MethodVisitor visitor : visitors.values()) {
visitor.visitEnd();
}
return cw.toByteArray();
}
use of jodd.asm5.ClassWriter in project apex-malhar by apache.
the class BeanClassGenerator method createAndWriteBeanClass.
/**
* Creates a class from given field information and writes it to the output stream. Also returns byte[] of compiled
* class
*
* @param fqcn fully qualified class name
* @param fieldList field list describing the class
* @param outputStream stream to which the class is persisted
* @throws JSONException
* @throws IOException
*/
public static byte[] createAndWriteBeanClass(String fqcn, List<TupleSchemaRegistry.SQLFieldInfo> fieldList, FSDataOutputStream outputStream) throws JSONException, IOException {
ClassNode classNode = new ClassNode();
// generated class will only run on JRE 1.7 or above
classNode.version = Opcodes.V1_7;
classNode.access = Opcodes.ACC_PUBLIC;
classNode.name = fqcn.replace('.', '/');
classNode.superName = "java/lang/Object";
// add default constructor
addDefaultConstructor(classNode);
for (TupleSchemaRegistry.SQLFieldInfo fieldInfo : fieldList) {
String fieldName = fieldInfo.getColumnName();
String fieldType = fieldInfo.getType().getJavaType().getName();
String fieldJavaType = getJavaType(fieldType);
addPrivateField(classNode, fieldName, fieldJavaType);
String fieldNameForMethods = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
if (fieldJavaType.equals(getJavaType("java.util.Date"))) {
addDateFields(classNode, fieldName, fieldNameForMethods, "java/util/Date");
} else {
addGetter(classNode, fieldName, fieldNameForMethods, fieldJavaType);
addSetter(classNode, fieldName, fieldNameForMethods, fieldJavaType);
}
}
addToStringMethod(classNode, fieldList);
addHashCodeMethod(classNode, fieldList);
addEqualsMethod(classNode, fieldList);
// Write the class
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(cw);
cw.visitEnd();
byte[] classBytes = cw.toByteArray();
if (outputStream != null) {
outputStream.write(classBytes);
outputStream.close();
}
return classBytes;
}
use of jodd.asm5.ClassWriter in project component-runtime by Talend.
the class PluginGenerator method createChainPlugin.
public File createChainPlugin(final File dir, final String plugin) {
final File target = new File(dir, plugin);
try (final JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(target))) {
final String packageName = toPackage(target.getParentFile().getParentFile().getName()).replace(".", "/");
final String sourcePackage = "org/talend/test";
final String fromPack = sourcePackage.replace('/', '.');
final String toPack = packageName.replace('.', '/');
final File root = new File(jarLocation(getClass()), sourcePackage);
ofNullable(root.listFiles()).map(Stream::of).orElseGet(Stream::empty).filter(c -> c.getName().endsWith(".class")).forEach(clazz -> {
try (final InputStream is = new FileInputStream(clazz)) {
final ClassReader reader = new ClassReader(is);
final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
reader.accept(new ClassRemapper(writer, new Remapper() {
@Override
public String map(final String key) {
return key.replace(sourcePackage, toPack).replace(fromPack, packageName);
}
}), EXPAND_FRAMES);
outputStream.putNextEntry(new JarEntry(toPack + '/' + clazz.getName()));
outputStream.write(writer.toByteArray());
} catch (final IOException e) {
fail(e.getMessage());
}
});
} catch (final IOException e) {
throw new IllegalStateException(e);
}
return target;
}
use of jodd.asm5.ClassWriter in project component-runtime by Talend.
the class PluginGenerator method createChainPlugin.
public File createChainPlugin(final File dir, final String plugin) {
final File target = new File(dir, plugin);
try (final JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(target))) {
final String packageName = toPackage(target.getParentFile().getParentFile().getName()).replace(".", "/");
final String sourcePackage = "org/talend/test";
final String fromPack = sourcePackage.replace('/', '.');
final String toPack = packageName.replace('.', '/');
final File root = new File(jarLocation(getClass()), sourcePackage);
ofNullable(root.listFiles()).map(Stream::of).orElseGet(Stream::empty).filter(c -> c.getName().endsWith(".class")).forEach(clazz -> {
try (final InputStream is = new FileInputStream(clazz)) {
final ClassReader reader = new ClassReader(is);
final ClassWriter writer = new ClassWriter(COMPUTE_FRAMES);
reader.accept(new ClassRemapper(writer, new Remapper() {
@Override
public String map(final String key) {
return key.replace(sourcePackage, toPack).replace(fromPack, packageName);
}
}), EXPAND_FRAMES);
outputStream.putNextEntry(new JarEntry(toPack + '/' + clazz.getName()));
outputStream.write(writer.toByteArray());
} catch (final IOException e) {
fail(e.getMessage());
}
});
} catch (final IOException e) {
throw new IllegalStateException(e);
}
return target;
}
Aggregations