Search in sources :

Example 1 with ClassFieldInspector

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();
    }
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) HashMap(java.util.HashMap) Position(org.kie.api.definition.type.Position) FieldDefinition(org.drools.core.factmodel.FieldDefinition) Method(java.lang.reflect.Method) IOException(java.io.IOException) Field(java.lang.reflect.Field) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector) HashSet(java.util.HashSet)

Example 2 with ClassFieldInspector

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);
            }
        }
    }
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) Resource(org.kie.api.io.Resource) IOException(java.io.IOException) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector)

Example 3 with ClassFieldInspector

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;
}
Also used : IOException(java.io.IOException) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector)

Example 4 with ClassFieldInspector

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;
}
Also used : ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector)

Example 5 with ClassFieldInspector

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);
    }
}
Also used : ByteArrayClassLoader(org.drools.core.util.ByteArrayClassLoader) Method(java.lang.reflect.Method) IOException(java.io.IOException) SelfReferenceClassFieldReader(org.drools.core.base.extractors.SelfReferenceClassFieldReader) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector)

Aggregations

ClassFieldInspector (org.drools.core.util.asm.ClassFieldInspector)11 IOException (java.io.IOException)7 Method (java.lang.reflect.Method)5 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)3 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)2 FieldDefinition (org.drools.core.factmodel.FieldDefinition)2 ByteArrayClassLoader (org.drools.core.util.ByteArrayClassLoader)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)1 ClassFieldAccessor (org.drools.core.base.ClassFieldAccessor)1 ClassFieldAccessorStore (org.drools.core.base.ClassFieldAccessorStore)1 SelfReferenceClassFieldReader (org.drools.core.base.extractors.SelfReferenceClassFieldReader)1 ClassDefinition (org.drools.core.factmodel.ClassDefinition)1