Search in sources :

Example 1 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
   * {@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;
}
Also used : Type(org.objectweb.asm.Type) ClassDefinition(co.cask.cdap.internal.asm.ClassDefinition) ClassWriter(org.objectweb.asm.ClassWriter)

Example 2 with 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));
    }
}
Also used : AuthEnforceRewriter(co.cask.cdap.common.security.AuthEnforceRewriter) InputStream(java.io.InputStream) ClassDefinition(co.cask.cdap.internal.asm.ClassDefinition) URL(java.net.URL)

Example 3 with ClassDefinition

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

Example 4 with 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;
}
Also used : ClassDefinition(co.cask.cdap.internal.asm.ClassDefinition) ClassWriter(org.objectweb.asm.ClassWriter)

Example 5 with 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));
    }
}
Also used : InputStream(java.io.InputStream) ClassDefinition(co.cask.cdap.internal.asm.ClassDefinition) DatasetClassRewriter(co.cask.cdap.common.dataset.DatasetClassRewriter) URL(java.net.URL)

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