Search in sources :

Example 36 with CtField

use of javassist.CtField in project hibernate-orm by hibernate.

the class PersistentAttributesEnhancer method enhance.

public void enhance(CtClass managedCtClass) {
    final IdentityHashMap<String, PersistentAttributeAccessMethods> attrDescriptorMap = new IdentityHashMap<String, PersistentAttributeAccessMethods>();
    for (CtField persistentField : collectPersistentFields(managedCtClass)) {
        attrDescriptorMap.put(persistentField.getName(), enhancePersistentAttribute(managedCtClass, persistentField));
    }
    // find all references to the transformed fields and replace with calls to the added reader/writer methods
    enhanceAttributesAccess(managedCtClass, attrDescriptorMap);
    // same thing for direct access to fields of other entities
    if (this.enhancementContext.doExtendedEnhancement(managedCtClass)) {
        extendedEnhancement(managedCtClass);
    }
}
Also used : CtField(javassist.CtField) IdentityHashMap(java.util.IdentityHashMap)

Example 37 with CtField

use of javassist.CtField in project hibernate-orm by hibernate.

the class PersistentAttributesEnhancer method collectInheritPersistentFields.

private Collection<CtField> collectInheritPersistentFields(CtClass managedCtClass) {
    if (managedCtClass == null || Object.class.getName().equals(managedCtClass.getName())) {
        return Collections.emptyList();
    }
    try {
        CtClass managedCtSuperclass = managedCtClass.getSuperclass();
        if (!enhancementContext.isMappedSuperclassClass(managedCtSuperclass)) {
            return collectInheritPersistentFields(managedCtSuperclass);
        }
        log.debugf("Found @MappedSuperclass %s to collectPersistenceFields", managedCtSuperclass.getName());
        List<CtField> persistentFieldList = new ArrayList<CtField>();
        for (CtField ctField : managedCtSuperclass.getDeclaredFields()) {
            if (ctField.getName().startsWith("$$_hibernate_") || "this$0".equals(ctField.getName())) {
                continue;
            }
            if (!Modifier.isStatic(ctField.getModifiers()) && enhancementContext.isPersistentField(ctField)) {
                persistentFieldList.add(ctField);
            }
        }
        persistentFieldList.addAll(collectInheritPersistentFields(managedCtSuperclass));
        return persistentFieldList;
    } catch (NotFoundException nfe) {
        log.warnf("Could not find the superclass of %s", managedCtClass);
        return Collections.emptyList();
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) ArrayList(java.util.ArrayList) NotFoundException(javassist.NotFoundException)

Example 38 with CtField

use of javassist.CtField in project hibernate-orm by hibernate.

the class PersistentAttributesEnhancer method collectPersistentFields.

private CtField[] collectPersistentFields(CtClass managedCtClass) {
    List<CtField> persistentFieldList = new ArrayList<CtField>();
    for (CtField ctField : managedCtClass.getDeclaredFields()) {
        // skip static fields and skip fields added by enhancement and  outer reference in inner classes
        if (ctField.getName().startsWith("$$_hibernate_") || "this$0".equals(ctField.getName())) {
            continue;
        }
        if (!Modifier.isStatic(ctField.getModifiers()) && enhancementContext.isPersistentField(ctField)) {
            persistentFieldList.add(ctField);
        }
    }
    // HHH-10981 There is no need to do it for @MappedSuperclass
    if (!enhancementContext.isMappedSuperclassClass(managedCtClass)) {
        persistentFieldList.addAll(collectInheritPersistentFields(managedCtClass));
    }
    CtField[] orderedFields = enhancementContext.order(persistentFieldList.toArray(new CtField[0]));
    log.debugf("Persistent fields for entity %s: %s", managedCtClass.getName(), Arrays.toString(orderedFields));
    return orderedFields;
}
Also used : CtField(javassist.CtField) ArrayList(java.util.ArrayList)

Example 39 with CtField

use of javassist.CtField in project atlas by alibaba.

the class CodeInjectByJavassist method addField.

private static void addField(ClassPool pool, CtClass cc, String key, String value) throws NotFoundException, CannotCompileException {
    if (StringUtils.isNotBlank(value)) {
        try {
            cc.removeField(cc.getField(key));
        } catch (NotFoundException notfoundException) {
        }
        logger.info("inject key " + key + "->" + value);
        CtClass ctClass = pool.get("java.lang.String");
        CtField ctField = new CtField(ctClass, key, cc);
        ctField.setModifiers(Modifier.STATIC | Modifier.PUBLIC);
        CtField.Initializer initializer = CtField.Initializer.constant(value);
        cc.addField(ctField, initializer);
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) NotFoundException(javassist.NotFoundException)

Example 40 with CtField

use of javassist.CtField in project atlas by alibaba.

the class CodeInjectByJavassist method inject.

public static synchronized byte[] inject(ClassPool pool, String className, InjectParam injectParam) throws Exception {
    try {
        CtClass cc = pool.get(className);
        cc.defrost();
        if (className.equalsIgnoreCase("android.taobao.atlas.framework.FrameworkProperties") || className.equalsIgnoreCase("android.taobao.atlas.version.VersionKernal")) {
            if (StringUtils.isNotBlank(injectParam.version)) {
                CtClass ctClass = pool.get("java.lang.String");
                CtField ctField = new CtField(ctClass, "version", cc);
                ctField.setModifiers(Modifier.PRIVATE);
                CtMethod ctMethod = CtNewMethod.getter("getVersion", ctField);
                ctMethod.setModifiers(Modifier.PUBLIC);
                CtField.Initializer initializer = CtField.Initializer.constant(injectParam.version);
                cc.addField(ctField, initializer);
                cc.addMethod(ctMethod);
                logger.info("[android.taobao.atlas.framework.FrameworkProperties] inject version " + injectParam.version);
            }
            addField(pool, cc, "bundleInfo", injectParam.bundleInfo);
            addField(pool, cc, "autoStartBundles", injectParam.autoStartBundles);
            addField(pool, cc, "preLaunch", injectParam.preLaunch);
            addField(pool, cc, "group", injectParam.group);
            addField(pool, cc, "outApp", String.valueOf(injectParam.outApp));
        }
        ClazzInjecter clazzInjecter = sInjecterMap.get(className);
        if (null != clazzInjecter) {
            Map<String, String> stringMap = clazzInjecter.getInjectFields();
            if (!stringMap.isEmpty()) {
                for (String key : stringMap.keySet()) {
                    addField(pool, cc, key, stringMap.get(key));
                }
            }
        }
        if (!injectParam.removePreverify) {
            Collection<String> refClasses = cc.getRefClasses();
            if (refClasses.contains("com.taobao.verify.Verifier")) {
                return cc.toBytecode();
            }
            boolean flag = false;
            if (className.equalsIgnoreCase("com.ut.mini.crashhandler.IUTCrashCaughtListner")) {
                flag = true;
            }
            if (cc.isInterface()) {
                final CtClass defClass = pool.get(Class.class.getName());
                CtField defField = new CtField(defClass, "_inject_field__", cc);
                defField.setModifiers(Modifier.STATIC | Modifier.PUBLIC | Modifier.FINAL);
                cc.addField(defField, CtField.Initializer.byExpr("Boolean.TRUE.booleanValue()?java.lang.String.class:com.taobao.verify.Verifier.class;"));
            } else {
                CtConstructor[] ctConstructors = cc.getDeclaredConstructors();
                if (null != ctConstructors && ctConstructors.length > 0) {
                    CtConstructor ctConstructor = ctConstructors[0];
                    ctConstructor.insertBeforeBody("if(Boolean.FALSE.booleanValue()){java.lang.String.valueOf(com.taobao.verify.Verifier.class);}");
                } else {
                    final CtClass defClass = pool.get(Class.class.getName());
                    CtField defField = new CtField(defClass, "_inject_field__", cc);
                    defField.setModifiers(Modifier.STATIC);
                    cc.addField(defField, CtField.Initializer.byExpr("Boolean.TRUE.booleanValue()?java.lang.String.class:com.taobao.verify.Verifier.class;"));
                }
            }
        }
        return cc.toBytecode();
    } catch (Throwable e) {
        throw new Exception("[InjectError]:" + className + ",reason:" + e.getMessage());
    }
}
Also used : NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor) CtClass(javassist.CtClass) CtField(javassist.CtField) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Aggregations

CtField (javassist.CtField)76 CtClass (javassist.CtClass)47 CtMethod (javassist.CtMethod)27 CannotCompileException (javassist.CannotCompileException)24 NotFoundException (javassist.NotFoundException)22 ClassPool (javassist.ClassPool)20 CtConstructor (javassist.CtConstructor)15 Test (org.junit.Test)12 ClassFile (javassist.bytecode.ClassFile)9 IOException (java.io.IOException)7 Method (java.lang.reflect.Method)7 ArrayList (java.util.ArrayList)6 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 ConstPool (javassist.bytecode.ConstPool)4 SMethod (org.bimserver.shared.meta.SMethod)4 SParameter (org.bimserver.shared.meta.SParameter)4 InsertableMethod (com.github.stephanenicolas.afterburner.inserts.InsertableMethod)3 SimpleInsertableMethod (com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3