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
* user service handler class. Calls to user service handler methods are transactional unless
* the method is annotated with {@link TransactionPolicy(TransactionControl)}.
*
* @param delegateType type of the user service handler
* @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<?> 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_8, 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;
}
Aggregations