Search in sources :

Example 91 with ClassPool

use of javassist.ClassPool in project fakereplace by fakereplace.

the class MainTransformer method transform.

@Override
public byte[] transform(final ClassLoader loader, final String className, final Class<?> classBeingRedefined, final ProtectionDomain protectionDomain, final byte[] classfileBuffer) throws IllegalClassFormatException {
    if (className == null) {
        // TODO: deal with lambdas
        return classfileBuffer;
    }
    boolean replaceable = Fakereplace.isClassReplaceable(className, loader);
    if (classBeingRedefined != null) {
        retransformationStarted = true;
        if (logClassRetransformation && replaceable) {
            log.info("Fakereplace is replacing class " + className);
        }
    }
    ChangedClassImpl changedClass = null;
    if (classBeingRedefined != null) {
        changedClass = new ChangedClassImpl(classBeingRedefined);
    }
    boolean changed = false;
    if (!replaceable && UnmodifiedFileIndex.isClassUnmodified(className)) {
        return null;
    }
    Set<Class<?>> classesToRetransform = new HashSet<>();
    final ClassFile file;
    try {
        Set<MethodInfo> modifiedMethods = new HashSet<>();
        file = new ClassFile(new DataInputStream(new ByteArrayInputStream(classfileBuffer)));
        for (final FakereplaceTransformer transformer : transformers) {
            if (transformer.transform(loader, className, classBeingRedefined, protectionDomain, file, classesToRetransform, changedClass, modifiedMethods)) {
                changed = true;
            }
        }
        if (!changed) {
            UnmodifiedFileIndex.markClassUnmodified(className);
            return null;
        } else {
            try {
                if (!modifiedMethods.isEmpty()) {
                    ClassPool classPool = new ClassPool();
                    if (loader == null) {
                        classPool.appendClassPath(new LoaderClassPath(ClassLoader.getSystemClassLoader()));
                    } else {
                        classPool.appendClassPath(new LoaderClassPath(loader));
                    }
                    classPool.appendSystemPath();
                    for (MethodInfo method : modifiedMethods) {
                        if (method.getCodeAttribute() != null) {
                            method.getCodeAttribute().computeMaxStack();
                            try {
                                method.rebuildStackMap(classPool);
                            } catch (BadBytecode e) {
                                Throwable root = e;
                                while (!(root instanceof NotFoundException) && root != null && root.getCause() != root) {
                                    root = root.getCause();
                                }
                                if (root instanceof NotFoundException) {
                                    NotFoundException cause = (NotFoundException) root;
                                    Bytecode bytecode = new Bytecode(file.getConstPool());
                                    bytecode.addNew(NoClassDefFoundError.class.getName());
                                    bytecode.add(Opcode.DUP);
                                    bytecode.addLdc(cause.getMessage());
                                    bytecode.addInvokespecial(NoClassDefFoundError.class.getName(), "<init>", "(Ljava/lang/String;)V");
                                    bytecode.add(Opcode.ATHROW);
                                    method.setCodeAttribute(bytecode.toCodeAttribute());
                                    method.getCodeAttribute().computeMaxStack();
                                    method.getCodeAttribute().setMaxLocals(DescriptorUtils.maxLocalsFromParameters(method.getDescriptor()) + 1);
                                    method.rebuildStackMap(classPool);
                                } else {
                                    throw e;
                                }
                            }
                        }
                    }
                }
            } catch (BadBytecode e) {
                throw new RuntimeException(e);
            }
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            file.write(new DataOutputStream(bs));
            // dump the class for debugging purposes
            final String dumpDir = AgentOptions.getOption(AgentOption.DUMP_DIR);
            if (dumpDir != null) {
                try {
                    File dump = new File(dumpDir + '/' + file.getName() + ".class");
                    dump.getParentFile().mkdirs();
                    FileOutputStream s = new FileOutputStream(dump);
                    DataOutputStream dos = new DataOutputStream(s);
                    file.write(dos);
                    s.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (!classesToRetransform.isEmpty()) {
                synchronized (this) {
                    retransformationOutstandingCount++;
                }
                Thread t = new Thread(() -> {
                    try {
                        Fakereplace.getInstrumentation().retransformClasses(classesToRetransform.toArray(new Class[classesToRetransform.size()]));
                    } catch (UnmodifiableClassException e) {
                        log.error("Failed to retransform classes", e);
                    } finally {
                        synchronized (MainTransformer.this) {
                            retransformationOutstandingCount--;
                            notifyAll();
                        }
                    }
                });
                t.setDaemon(true);
                t.start();
            }
            if (classBeingRedefined != null) {
                changedClasses.add(changedClass);
                queueIntegration();
            }
            return bs.toByteArray();
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalClassFormatException(e.getMessage());
    } catch (Throwable e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) BadBytecode(javassist.bytecode.BadBytecode) Bytecode(javassist.bytecode.Bytecode) HashSet(java.util.HashSet) ClassFile(javassist.bytecode.ClassFile) UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) ChangedClassImpl(org.fakereplace.replacement.notification.ChangedClassImpl) LoaderClassPath(javassist.LoaderClassPath) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) BadBytecode(javassist.bytecode.BadBytecode) NotFoundException(javassist.NotFoundException) IllegalClassFormatException(java.lang.instrument.IllegalClassFormatException) IOException(java.io.IOException) UnmodifiableClassException(java.lang.instrument.UnmodifiableClassException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ChangedClass(org.fakereplace.api.ChangedClass) MethodInfo(javassist.bytecode.MethodInfo) ClassFile(javassist.bytecode.ClassFile) File(java.io.File)

Example 92 with ClassPool

use of javassist.ClassPool in project motech by motech.

the class EntityMetadataBuilderTest method shouldAddBaseEntityMetadata.

@Test
public void shouldAddBaseEntityMetadata() throws Exception {
    CtField ctField = mock(CtField.class);
    CtClass ctClass = mock(CtClass.class);
    CtClass superClass = mock(CtClass.class);
    ClassData classData = mock(ClassData.class);
    ClassPool pool = mock(ClassPool.class);
    PowerMockito.mockStatic(MotechClassPool.class);
    PowerMockito.when(MotechClassPool.getDefault()).thenReturn(pool);
    when(classData.getClassName()).thenReturn(CLASS_NAME);
    when(classData.getModule()).thenReturn(MODULE);
    when(classData.getNamespace()).thenReturn(NAMESPACE);
    when(entity.getTableName()).thenReturn(TABLE_NAME);
    when(pool.getOrNull(CLASS_NAME)).thenReturn(ctClass);
    when(ctClass.getField("id")).thenReturn(ctField);
    when(ctClass.getSuperclass()).thenReturn(superClass);
    when(superClass.getName()).thenReturn(Object.class.getName());
    when(jdoMetadata.newPackageMetadata(PACKAGE)).thenReturn(packageMetadata);
    when(packageMetadata.newClassMetadata(ENTITY_NAME)).thenReturn(classMetadata);
    entityMetadataBuilder.addBaseMetadata(jdoMetadata, classData, EntityType.STANDARD, Sample.class);
    verify(jdoMetadata).newPackageMetadata(PACKAGE);
    verify(packageMetadata).newClassMetadata(ENTITY_NAME);
    verify(classMetadata).setTable(TABLE_NAME_3);
    verifyCommonClassMetadata();
}
Also used : CtClass(javassist.CtClass) ClassData(org.motechproject.mds.domain.ClassData) CtField(javassist.CtField) ClassPool(javassist.ClassPool) MotechClassPool(org.motechproject.mds.javassist.MotechClassPool) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 93 with ClassPool

use of javassist.ClassPool in project ignite by apache.

the class IgniteExamplesMLTestSuite method makeTestClass.

/**
 * Creates test class for given example.
 *
 * @param exampleCls Class of the example to be tested.
 * @return Test class.
 */
private static Class<?> makeTestClass(Class<?> exampleCls) {
    ClassPool cp = ClassPool.getDefault();
    cp.insertClassPath(new ClassClassPath(IgniteExamplesMLTestSuite.class));
    CtClass cl = cp.makeClass(basePkgForTests + "." + exampleCls.getSimpleName() + "SelfTest");
    try {
        cl.setSuperclass(cp.get(GridAbstractExamplesTest.class.getName()));
        CtMethod mtd = CtNewMethod.make("public void testExample() { " + exampleCls.getCanonicalName() + ".main(" + MLExamplesCommonArgs.class.getName() + ".EMPTY_ARGS_ML); }", cl);
        // Create and add annotation.
        ClassFile ccFile = cl.getClassFile();
        ConstPool constpool = ccFile.getConstPool();
        AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
        Annotation annot = new Annotation("org.junit.Test", constpool);
        attr.addAnnotation(annot);
        mtd.getMethodInfo().addAttribute(attr);
        cl.addMethod(mtd);
        return cl.toClass();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : CtClass(javassist.CtClass) ConstPool(javassist.bytecode.ConstPool) ClassFile(javassist.bytecode.ClassFile) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ClassPool(javassist.ClassPool) MLExamplesCommonArgs(org.apache.ignite.examples.ml.util.MLExamplesCommonArgs) ClassClassPath(javassist.ClassClassPath) CtMethod(javassist.CtMethod) Annotation(javassist.bytecode.annotation.Annotation) IOException(java.io.IOException)

Example 94 with ClassPool

use of javassist.ClassPool in project dubbo by alibaba.

the class JValidator method generateMethodParameterClass.

/**
 * try to generate methodParameterClass.
 *
 * @param clazz interface class
 * @param method invoke method
 * @param parameterClassName generated parameterClassName
 * @return Class<?> generated methodParameterClass
 * @throws Exception
 */
private static Class<?> generateMethodParameterClass(Class<?> clazz, Method method, String parameterClassName) throws Exception {
    ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader());
    synchronized (parameterClassName.intern()) {
        CtClass ctClass = null;
        try {
            ctClass = pool.getCtClass(parameterClassName);
        } catch (NotFoundException ignore) {
        }
        if (null == ctClass) {
            ctClass = pool.makeClass(parameterClassName);
            ClassFile classFile = ctClass.getClassFile();
            classFile.setVersionToJava5();
            ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName)));
            // parameter fields
            Class<?>[] parameterTypes = method.getParameterTypes();
            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
            for (int i = 0; i < parameterTypes.length; i++) {
                Class<?> type = parameterTypes[i];
                Annotation[] annotations = parameterAnnotations[i];
                AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag);
                for (Annotation annotation : annotations) {
                    if (annotation.annotationType().isAnnotationPresent(Constraint.class)) {
                        javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation(classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName()));
                        Method[] members = annotation.annotationType().getMethods();
                        for (Method member : members) {
                            if (Modifier.isPublic(member.getModifiers()) && member.getParameterTypes().length == 0 && member.getDeclaringClass() == annotation.annotationType()) {
                                Object value = member.invoke(annotation);
                                if (null != value) {
                                    MemberValue memberValue = createMemberValue(classFile.getConstPool(), pool.get(member.getReturnType().getName()), value);
                                    ja.addMemberValue(member.getName(), memberValue);
                                }
                            }
                        }
                        attribute.addAnnotation(ja);
                    }
                }
                String fieldName = method.getName() + "Argument" + i;
                CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName));
                ctField.getFieldInfo().addAttribute(attribute);
                ctClass.addField(ctField);
            }
            return ctClass.toClass(clazz.getClassLoader(), null);
        } else {
            return Class.forName(parameterClassName, true, clazz.getClassLoader());
        }
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) Method(java.lang.reflect.Method) Constraint(javax.validation.Constraint) Annotation(java.lang.annotation.Annotation) CtClass(javassist.CtClass) MemberValue(javassist.bytecode.annotation.MemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue) CharMemberValue(javassist.bytecode.annotation.CharMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) FloatMemberValue(javassist.bytecode.annotation.FloatMemberValue) DoubleMemberValue(javassist.bytecode.annotation.DoubleMemberValue) ShortMemberValue(javassist.bytecode.annotation.ShortMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) LongMemberValue(javassist.bytecode.annotation.LongMemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) ByteMemberValue(javassist.bytecode.annotation.ByteMemberValue) CtField(javassist.CtField) CtClass(javassist.CtClass)

Example 95 with ClassPool

use of javassist.ClassPool in project dubbo by alibaba.

the class CtClassBuilder method build.

/**
 * build CtClass object
 */
public CtClass build(ClassLoader classLoader) throws NotFoundException, CannotCompileException {
    ClassPool pool = new ClassPool(true);
    pool.appendClassPath(new LoaderClassPath(classLoader));
    // create class
    CtClass ctClass = pool.makeClass(className, pool.get(superClassName));
    // add imported packages
    imports.forEach(pool::importPackage);
    // add implemented interfaces
    for (String iface : ifaces) {
        ctClass.addInterface(pool.get(iface));
    }
    // add constructors
    for (String constructor : constructors) {
        ctClass.addConstructor(CtNewConstructor.make(constructor, ctClass));
    }
    // add fields
    for (String field : fields) {
        ctClass.addField(CtField.make(field, ctClass));
    }
    // add methods
    for (String method : methods) {
        ctClass.addMethod(CtNewMethod.make(method, ctClass));
    }
    return ctClass;
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath)

Aggregations

ClassPool (javassist.ClassPool)120 CtClass (javassist.CtClass)93 CtMethod (javassist.CtMethod)48 NotFoundException (javassist.NotFoundException)40 CannotCompileException (javassist.CannotCompileException)28 IOException (java.io.IOException)23 LoaderClassPath (javassist.LoaderClassPath)21 CtField (javassist.CtField)20 CtConstructor (javassist.CtConstructor)17 Test (org.junit.Test)17 ClassFile (javassist.bytecode.ClassFile)15 File (java.io.File)13 Method (java.lang.reflect.Method)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ConstPool (javassist.bytecode.ConstPool)12 FileNotFoundException (java.io.FileNotFoundException)11 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)9 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)9 ClassClassPath (javassist.ClassClassPath)7 MethodInfo (javassist.bytecode.MethodInfo)7