use of co.cask.cdap.internal.asm.ClassDefinition in project cdap by caskdata.
the class HttpHandlerGenerator method generate.
/**
* Generates a new class that implements {@link HttpHandler} by copying methods signatures from the given
* {@link HttpServiceHandler} class. Calls to {@link HttpServiceHandler} methods are transactional unless
* the method is annotated with {@link TransactionPolicy(TransactionControl)}.
*
* @param delegateType type of the {@link HttpServiceHandler}
* @param pathPrefix prefix for all {@code @PATH} annotation
* @return A {@link ClassDefinition} containing information of the newly generated class.
* @throws IOException if failed to generate the class.
*/
ClassDefinition generate(TypeToken<? extends HttpServiceHandler> delegateType, String pathPrefix) throws IOException {
Class<?> rawType = delegateType.getRawType();
List<Class<?>> preservedClasses = Lists.newArrayList();
preservedClasses.add(rawType);
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
String internalName = Type.getInternalName(rawType);
String className = internalName + Hashing.md5().hashString(internalName);
// Generate the class
Type classType = Type.getObjectType(className);
classWriter.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className, getClassSignature(delegateType), Type.getInternalName(AbstractHttpHandlerDelegator.class), null);
// Inspect the delegate class hierarchy to generate public handler methods.
for (TypeToken<?> type : delegateType.getTypes().classes()) {
if (!Object.class.equals(type.getRawType())) {
inspectHandler(delegateType, type, pathPrefix, classType, classWriter, preservedClasses);
}
}
generateConstructor(delegateType, classWriter);
generateLogger(classType, classWriter);
ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className, preservedClasses);
// End DEBUG block
return classDefinition;
}
use of co.cask.cdap.internal.asm.ClassDefinition in project cdap by caskdata.
the class AuthEnforceRewriterTest method rewrite.
private ClassDefinition rewrite(Class cls) throws Exception {
AuthEnforceRewriter rewriter = new AuthEnforceRewriter();
URL url = cls.getClassLoader().getResource(cls.getName().replace('.', '/') + ".class");
Assert.assertNotNull(url);
try (InputStream is = url.openStream()) {
return new ClassDefinition(rewriter.rewriteClass(cls.getName(), is), Type.getInternalName(cls));
}
}
use of co.cask.cdap.internal.asm.ClassDefinition in project cdap by caskdata.
the class DatumWriterGenerator method generate.
/**
* Generates a {@link DatumWriter} class for encoding data of the given output type with the given schema.
* @param outputType Type information of the output data type.
* @param schema Schema of the output data type.
* @return A {@link co.cask.cdap.internal.asm.ClassDefinition} that contains generated class information.
*/
ClassDefinition generate(TypeToken<?> outputType, Schema schema) {
classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
preservedClasses = Lists.newArrayList();
TypeToken<?> interfaceType = getInterfaceType(outputType);
// Generate the class
String className = getClassName(interfaceType, schema);
classType = Type.getObjectType(className);
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className, Signatures.getClassSignature(interfaceType), Type.getInternalName(Object.class), new String[] { Type.getInternalName(interfaceType.getRawType()) });
// Static schema hash field, for verification
classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "SCHEMA_HASH", Type.getDescriptor(String.class), null, schema.getSchemaHash().toString()).visitEnd();
// Schema field
classWriter.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "schema", Type.getDescriptor(Schema.class), null, null).visitEnd();
// Encode method
generateEncode(outputType, schema);
// Constructor
generateConstructor();
ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className, preservedClasses);
// End DEBUG block
return classDefinition;
}
use of co.cask.cdap.internal.asm.ClassDefinition in project cdap by caskdata.
the class FieldAccessorGenerator method generate.
ClassDefinition generate(Class<?> classType, Field field, boolean publicOnly) {
String name = String.format("%s$GeneratedAccessor%s", classType.getName(), field.getName());
if (name.startsWith("java.") || name.startsWith("javax.")) {
name = "co.cask.cdap." + name;
publicOnly = true;
}
this.className = name.replace('.', '/');
if (publicOnly) {
isPrivate = !Modifier.isPublic(field.getModifiers()) || !Modifier.isPublic(field.getDeclaringClass().getModifiers());
} else {
isPrivate = Modifier.isPrivate(field.getModifiers()) || Modifier.isPrivate(field.getDeclaringClass().getModifiers());
}
// Generate the class
classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className, null, Type.getInternalName(AbstractFieldAccessor.class), new String[0]);
generateConstructor(field);
generateGetter(field);
generateSetter(field);
classWriter.visitEnd();
ClassDefinition classDefinition = new ClassDefinition(classWriter.toByteArray(), className);
// End DEBUG block
return classDefinition;
}
use of co.cask.cdap.internal.asm.ClassDefinition in project cdap by caskdata.
the class DatasetClassRewriterTest method rewrite.
private ClassDefinition rewrite(Class<? extends Dataset> dataset) throws Exception {
DatasetClassRewriter rewriter = new DatasetClassRewriter();
URL url = dataset.getClassLoader().getResource(dataset.getName().replace('.', '/') + ".class");
Assert.assertNotNull(url);
try (InputStream is = url.openStream()) {
return new ClassDefinition(rewriter.rewriteClass(dataset.getName(), is), Type.getInternalName(dataset));
}
}
Aggregations