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);
}
}
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();
}
}
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;
}
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);
}
}
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());
}
}
Aggregations