use of org.drools.core.util.asm.ClassFieldInspector in project drools by kiegroup.
the class ClassDefinitionFactory method populateDefinitionFromClass.
public static void populateDefinitionFromClass(ClassDefinition def, Resource resource, Class<?> concrete, boolean asTrait) {
try {
def.setClassName(concrete.getName());
if (concrete.getSuperclass() != null) {
def.setSuperClass(concrete.getSuperclass().getName());
}
ClassFieldInspector inspector = new ClassFieldInspector(concrete);
Map<String, Method> methods = inspector.getGetterMethods();
Map<String, Method> setters = inspector.getSetterMethods();
int j = 0;
Map<String, TypeFieldDescr> fields = new HashMap<String, TypeFieldDescr>();
for (String fieldName : methods.keySet()) {
if (asTrait && ("core".equals(fieldName) || "fields".equals(fieldName))) {
continue;
}
if (!inspector.isNonGetter(fieldName) && setters.keySet().contains(fieldName)) {
Position position = null;
if (!concrete.isInterface()) {
try {
Field fld = concrete.getDeclaredField(fieldName);
position = fld.getAnnotation(Position.class);
} catch (NoSuchFieldException nsfe) {
// @Position can only annotate fields. This x means that a getter/setter pair was found with no field
}
}
Class ret = methods.get(fieldName).getReturnType();
TypeFieldDescr field = new TypeFieldDescr();
field.setResource(resource);
field.setFieldName(fieldName);
field.setPattern(new PatternDescr(ret.getName()));
field.setIndex(position != null ? position.value() : -1);
fields.put(fieldName, field);
}
}
if (!fields.isEmpty()) {
List<FieldDefinition> fieldDefs = sortFields(fields, null, null);
int i = 0;
for (FieldDefinition fieldDef : fieldDefs) {
fieldDef.setIndex(i++);
def.addField(fieldDef);
}
}
Set<String> interfaces = new HashSet<String>();
Collections.addAll(interfaces, def.getInterfaces());
for (Class iKlass : ClassUtils.getAllImplementedInterfaceNames(concrete)) {
interfaces.add(iKlass.getName());
}
def.setInterfaces(interfaces.toArray(new String[interfaces.size()]));
def.setDefinedClass(concrete);
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.drools.core.util.asm.ClassFieldInspector in project drools by kiegroup.
the class ClassHierarchyManager method buildDescrsFromFields.
private static void buildDescrsFromFields(Class klass, TypeDeclarationDescr typeDescr, PackageRegistry pkgRegistry, Map<String, TypeFieldDescr> fieldMap) {
ClassFieldInspector inspector = null;
try {
inspector = new ClassFieldInspector(klass);
} catch (IOException e) {
throw new RuntimeException(e);
}
for (String name : inspector.getGetterMethods().keySet()) {
// classFieldAccessor requires both getter and setter
if (inspector.getSetterMethods().containsKey(name)) {
if (!inspector.isNonGetter(name) && !"class".equals(name)) {
Resource resource = typeDescr.getResource();
PatternDescr patternDescr = new PatternDescr(inspector.getFieldType(name).getName());
patternDescr.setResource(resource);
TypeFieldDescr inheritedFlDescr = new TypeFieldDescr(name, patternDescr);
inheritedFlDescr.setResource(resource);
inheritedFlDescr.setInherited(!Modifier.isAbstract(inspector.getGetterMethods().get(name).getModifiers()));
if (!fieldMap.containsKey(inheritedFlDescr.getFieldName()))
fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
}
}
}
}
use of org.drools.core.util.asm.ClassFieldInspector in project drools by kiegroup.
the class ClassFieldAccessorFactory method getFieldType.
public static Class<?> getFieldType(Class<?> clazz, String fieldName, CacheEntry cache) {
ClassFieldInspector inspector;
try {
inspector = getClassFieldInspector(clazz, cache);
} catch (IOException e) {
throw new RuntimeException(e);
}
Class<?> fieldType = inspector.getFieldType(fieldName);
if (fieldType == null && fieldName.length() > 1 && Character.isLowerCase(fieldName.charAt(0)) && Character.isUpperCase(fieldName.charAt(1))) {
String altFieldName = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
fieldType = inspector.getFieldType(altFieldName);
}
return fieldType;
}
use of org.drools.core.util.asm.ClassFieldInspector in project drools by kiegroup.
the class ClassFieldAccessorFactory method getClassFieldInspector.
private static ClassFieldInspector getClassFieldInspector(Class<?> clazz, CacheEntry cache) throws IOException {
Map<Class<?>, ClassFieldInspector> inspectors = cache.getInspectors();
ClassFieldInspector inspector = inspectors.get(clazz);
if (inspector == null) {
inspector = new ClassFieldInspector(clazz);
inspectors.put(clazz, inspector);
}
return inspector;
}
use of org.drools.core.util.asm.ClassFieldInspector in project drools by kiegroup.
the class ClassFieldAccessorFactory method getClassFieldReader.
public static BaseClassFieldReader getClassFieldReader(Class<?> clazz, String fieldName, CacheEntry cache) {
try {
// if it is a self reference
if (SELF_REFERENCE_FIELD.equals(fieldName)) {
// then just create an instance of the special class field extractor
return new SelfReferenceClassFieldReader(clazz);
} else {
// otherwise, bytecode generate a specific extractor
ClassFieldInspector inspector = getClassFieldInspector(clazz, cache);
Method getterMethod = inspector.getGetterMethods().get(fieldName);
Integer index = inspector.getFieldNames().get(fieldName);
Class<?> fieldType = inspector.getFieldType(fieldName);
if (fieldType == null && fieldName.length() > 1 && Character.isLowerCase(fieldName.charAt(0)) && Character.isUpperCase(fieldName.charAt(1))) {
// it might be that odd case of javabeans naming conventions that does not use lower case first letters if the second is uppercase
String altFieldName = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
fieldType = inspector.getFieldType(altFieldName);
if (fieldType != null) {
// it seems it is the corner case indeed.
getterMethod = inspector.getGetterMethods().get(altFieldName);
index = inspector.getFieldNames().get(altFieldName);
}
}
if (fieldType != null && getterMethod != null) {
final String className = ClassFieldAccessorFactory.BASE_PACKAGE + "/" + Type.getInternalName(clazz) + Math.abs(System.identityHashCode(clazz)) + "$" + getterMethod.getName();
// generating byte array to create target class
final byte[] bytes = dumpReader(clazz, className, getterMethod, fieldType);
// use bytes to get a class
ByteArrayClassLoader byteArrayClassLoader = cache.getByteArrayClassLoader();
final Class<?> newClass = byteArrayClassLoader.defineClass(className.replace('/', '.'), bytes, PROTECTION_DOMAIN);
// instantiating target class
final ValueType valueType = ValueType.determineValueType(fieldType);
final Object[] params = { index, fieldType, valueType };
return (BaseClassFieldReader) newClass.getConstructors()[0].newInstance(params);
} else {
// must be a public field
return null;
}
}
} catch (final RuntimeException e) {
throw e;
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Aggregations