Search in sources :

Example 6 with ClassDefinition

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;
}
Also used : Type(org.objectweb.asm.Type) MethodType(java.lang.invoke.MethodType) ClassDefinition(co.cask.cdap.internal.asm.ClassDefinition) ClassWriter(org.objectweb.asm.ClassWriter)

Aggregations

ClassDefinition (co.cask.cdap.internal.asm.ClassDefinition)6 ClassWriter (org.objectweb.asm.ClassWriter)4 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Type (org.objectweb.asm.Type)2 DatasetClassRewriter (co.cask.cdap.common.dataset.DatasetClassRewriter)1 AuthEnforceRewriter (co.cask.cdap.common.security.AuthEnforceRewriter)1 MethodType (java.lang.invoke.MethodType)1