Search in sources :

Example 6 with ClassFieldInspector

use of org.drools.core.base.ClassFieldInspector in project drools by kiegroup.

the class ClassFieldAccessorFactory method getClassFieldWriter.

@Override
public BaseClassFieldWriter getClassFieldWriter(Class<?> clazz, String fieldName, CacheEntry cache) {
    ByteArrayClassLoader byteArrayClassLoader = cache.getByteArrayClassLoader();
    Map<Class<?>, ClassFieldInspector> inspectors = cache.getInspectors();
    try {
        // otherwise, bytecode generate a specific extractor
        ClassFieldInspector inspector = inspectors.get(clazz);
        if (inspector == null) {
            inspector = new ClassFieldInspectorImpl(clazz);
            inspectors.put(clazz, inspector);
        }
        Method setterMethod = inspector.getSetterMethods().get(fieldName);
        Integer index = inspector.getFieldNames().get(fieldName);
        if (setterMethod == 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);
            setterMethod = inspector.getSetterMethods().get(altFieldName);
            index = inspector.getFieldNames().get(altFieldName);
        }
        if (setterMethod != null) {
            final Class<?> fieldType = setterMethod.getParameterTypes()[0];
            final String className = ClassFieldAccessorFactory.BASE_PACKAGE + "/" + Type.getInternalName(clazz) + Math.abs((long) System.identityHashCode(clazz)) + "$" + setterMethod.getName();
            // generating byte array to create target class
            final byte[] bytes = dumpWriter(clazz, className, setterMethod, fieldType);
            // use bytes to get a class
            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 (BaseClassFieldWriter) newClass.getConstructors()[0].newInstance(params);
        } else {
            if (inspector.getFieldNames().containsKey(fieldName)) {
                return null;
            } else {
                throw new RuntimeException("Field/method '" + fieldName + "' not found for class '" + clazz.getName() + "'");
            }
        }
    } catch (final RuntimeException e) {
        throw e;
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ValueType(org.drools.core.base.ValueType) ByteArrayClassLoader(org.drools.wiring.api.util.ByteArrayClassLoader) Method(java.lang.reflect.Method) BaseClassFieldWriter(org.drools.core.base.BaseClassFieldWriter) ClassFieldAccessorStore.getClassFieldInspector(org.drools.mvel.accessors.ClassFieldAccessorStore.getClassFieldInspector) ClassFieldInspector(org.drools.core.base.ClassFieldInspector)

Example 7 with ClassFieldInspector

use of org.drools.core.base.ClassFieldInspector in project drools by kiegroup.

the class ClassFieldAccessorStore method getFieldType.

public Class<?> getFieldType(Class<?> clazz, String fieldName) {
    ClassFieldAccessorCache.CacheEntry cache = this.cache.getCacheEntry(clazz);
    ClassFieldInspector inspector;
    try {
        inspector = getClassFieldInspector(clazz, cache);
    } catch (IOException e) {
        throw new UncheckedIOException(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 : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ClassFieldInspector(org.drools.core.base.ClassFieldInspector)

Example 8 with ClassFieldInspector

use of org.drools.core.base.ClassFieldInspector in project drools by kiegroup.

the class ClassFieldAccessorStore method getClassFieldInspector.

public static ClassFieldInspector getClassFieldInspector(final Class<?> clazz, ClassFieldAccessorCache.CacheEntry cache) throws IOException {
    Map<Class<?>, ClassFieldInspector> inspectors = cache.getInspectors();
    ClassFieldInspector inspector = inspectors.get(clazz);
    if (inspector == null) {
        inspector = CoreComponentsBuilder.get().createClassFieldInspector(clazz);
        inspectors.put(clazz, inspector);
    }
    return inspector;
}
Also used : ClassFieldInspector(org.drools.core.base.ClassFieldInspector)

Aggregations

ClassFieldInspector (org.drools.core.base.ClassFieldInspector)8 IOException (java.io.IOException)5 Method (java.lang.reflect.Method)3 TypeFieldDescr (org.drools.drl.ast.descr.TypeFieldDescr)3 ValueType (org.drools.core.base.ValueType)2 PatternDescr (org.drools.drl.ast.descr.PatternDescr)2 ClassFieldAccessorStore.getClassFieldInspector (org.drools.mvel.accessors.ClassFieldAccessorStore.getClassFieldInspector)2 ByteArrayClassLoader (org.drools.wiring.api.util.ByteArrayClassLoader)2 UncheckedIOException (java.io.UncheckedIOException)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)1 BaseClassFieldReader (org.drools.core.base.BaseClassFieldReader)1 BaseClassFieldWriter (org.drools.core.base.BaseClassFieldWriter)1 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)1 SelfReferenceClassFieldReader (org.drools.core.base.extractors.SelfReferenceClassFieldReader)1 FieldDefinition (org.drools.core.factmodel.FieldDefinition)1 GeneratedFact (org.drools.core.factmodel.GeneratedFact)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1