Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kie.workbench.common.services.datamodel.backend.server.builder.projects;
  
  import java.util.*;
 
 import  org.drools.core.util.asm.ClassFieldInspector;
 import  org.guvnor.common.services.project.builder.model.TypeSource;
 import  org.kie.workbench.common.services.datamodel.model.Annotation;
 import  org.kie.workbench.common.services.datamodel.model.ClassToGenericClassConverter;
 import  org.kie.workbench.common.services.datamodel.model.FieldAccessorsAndMutators;
 import  org.kie.workbench.common.services.datamodel.model.MethodInfo;
 import  org.kie.workbench.common.services.datamodel.model.ModelField;
 import  org.kie.workbench.common.services.datamodel.oracle.ProjectDataModelOracleImpl;

Builder for Fact Types originating from a .class
 
 public class ClassFactBuilder extends BaseFactBuilder {
 
     private final ClassToGenericClassConverter typeSystemConverter = new JavaTypeSystemTranslator();
 
     private final Map<StringList<MethodInfo>> methodInformation = new HashMap<StringList<MethodInfo>>();
     private final Map<StringStringfieldParametersType = new HashMap<StringString>();
 
     private final String superType;
     private final Set<Annotation> annotations = new LinkedHashSet<Annotation>();
     private final Map<StringSet<Annotation>> fieldAnnotations = new HashMap<StringSet<Annotation>>();
 
     public ClassFactBuilderfinal ProjectDataModelOracleBuilder builder,
                              final Class<?> clazz,
                              final boolean isEvent,
                              final TypeSource typeSource ) throws IOException {
         superbuilder,
                clazz,
                isEvent,
                typeSource );
         this. = getSuperTypeclazz );
         this..addAll( getAnnotationsclazz ) );
         this..putAll( getFieldsAnnotationsclazz ) );
         loadClassFieldsclazz );
     }
 
     @Override
     public void buildfinal ProjectDataModelOracleImpl oracle ) {
         super.buildoracle );
         oracle.addMethodInformation(  );
         oracle.addFieldParametersType(  );
         oracle.addSuperTypes( buildSuperTypes() );
         oracle.addTypeAnnotations( buildTypeAnnotations() );
         oracle.addTypeFieldsAnnotations( buildTypeFieldsAnnotations() );
     }
 
     protected String getSuperTypefinal Class<?> clazz ) {
         final Class<?> superType = clazz.getSuperclass();
         return ( superType == null || Object.class.equalssuperType ) ? null : superType.getName() );
     }
 
     protected Set<Annotation> getAnnotationsfinal Class<?> clazz ) {
         final Set<Annotation> dmoAnnotations = new LinkedHashSet<Annotation>();
         final java.lang.annotation.Annotation annotations[] = clazz.getAnnotations();
         for ( java.lang.annotation.Annotation a : annotations ) {
             final Annotation dmoa = new Annotation( a.annotationType().getName() );
             for ( Method m : a.annotationType().getDeclaredMethods() ) {
                 final String methodName = m.getName();
                 dmoa.addAttribute( methodName,
                                    AnnotationUtils.getAnnotationAttributeValuea,
                                                                                 methodName ) );
             }
             dmoAnnotations.add( dmoa );
         }
         return dmoAnnotations;
     }
 
     private Map<StringSet<Annotation>> getFieldsAnnotationsfinal Class<?> clazz ) {
         final Field[] fields = clazz.getDeclaredFields();
         final Map<StringSet<Annotation>> fieldsAnnotations = new HashMap<StringSet<Annotation>>();
         for ( Field field : fields ) {
             final String fieldName = field.getName();
             final Set<Annotation> fieldAnnotations = getFieldAnnotationsfield );
             if ( fieldAnnotations.size() > 0 ) {
                 fieldsAnnotations.put( fieldName,
                                        fieldAnnotations );
             }
         }
         return fieldsAnnotations;
     }
 
     private Set<Annotation> getFieldAnnotationsfinal Field field ) {
         final java.lang.annotation.Annotation[] annotations = field.getDeclaredAnnotations();
         final Set<Annotation> fieldAnnotations = new LinkedHashSet<Annotation>();
         for ( java.lang.annotation.Annotation a : annotations ) {
             final Annotation fieldAnnotation = new Annotation( a.annotationType().getName() );
             for ( Method m : a.annotationType().getDeclaredMethods() ) {
                final String methodName = m.getName();
                fieldAnnotation.addAttribute( methodName,
                                              AnnotationUtils.getAnnotationAttributeValuea,
                                                                                           methodName ) );
            }
            fieldAnnotations.add( fieldAnnotation );
        }
        return fieldAnnotations;
    }
    private void loadClassFieldsfinal Class<?> clazz ) throws IOException {
        if ( clazz == null ) {
            return;
        }
        final String factType = getTypeclazz );
        //Get all getters and setters for the class. This does not handle delegated properties
        final ClassFieldInspector inspector = new ClassFieldInspector( clazz );
        final Set<StringfieldNamesSet = new TreeSet<String>( inspector.getFieldNames().keySet() );
        final List<StringfieldNames = removeIrrelevantFieldsfieldNamesSet );
        //Consolidate methods into those with getters or setters
        final Method[] methods = clazz.getMethods();
        final Map<StringMethodSignaturemethodSignatures = removeIrrelevantMethodsgetMethodSignaturesfactType,
                                                                                                            methods ) );
        //Add Fields from ClassFieldInspector which provides a list of "reasonable" methods
        for ( final String fieldName : fieldNames ) {
            final String qualifiedName = factType + "." + fieldName;
            final Field f = inspector.getFieldTypesField().get( fieldName );
            if ( f == null ) {
                //If a Field cannot be found is is really a delegated property so use the Method return type
                if ( methodSignatures.containsKeyqualifiedName ) ) {
                    final MethodSignature m = methodSignatures.getqualifiedName );
                    addParametricTypeForFieldfactType,
                                               fieldName,
                                               m.genericType );
                    final Class<?> returnType = m.returnType;
                    final String genericReturnType = .translateClassToGenericType( returnType );
                    addFieldnew ModelField( fieldName,
                                              returnType.getName(),
                                              ModelField.FIELD_CLASS_TYPE.REGULAR_CLASS,
                                              ModelField.FIELD_ORIGIN.DELEGATED,
                                              methodSignatures.getqualifiedName ).,
                                              genericReturnType ) );
                    addEnumsForFieldfactType,
                                      fieldName,
                                      returnType );
                }
            } else {
                //Otherwise we can use the results of ClassFieldInspector
                final Field field = inspector.getFieldTypesField().get( fieldName );
                addParametricTypeForFieldfactType,
                                           fieldName,
                                           field.getGenericType() );
                Field[] _declaredClassFields = clazz.getDeclaredFields();
                Collection declaredClassFields = _declaredClassFields != null ? Arrays.asList(_declaredClassFields) : .;
                final Class<?> returnType = field.getType();
                final String genericReturnType = .translateClassToGenericType( returnType );
                addFieldnew ModelField( fieldName,
                                          returnType.getName(),
                                          ModelField.FIELD_CLASS_TYPE.REGULAR_CLASS,
                                          declaredClassFields.contains(field) ? ModelField.FIELD_ORIGIN.DECLARED : ModelField.FIELD_ORIGIN.INHERITED,
                                          methodSignatures.getqualifiedName ).,
                                          genericReturnType ) );
                addEnumsForFieldfactType,
                                  fieldName,
                                  returnType );
            }
        }
        //Methods for use in ActionCallMethod's
        ClassMethodInspector methodInspector = new ClassMethodInspectorclazz,
                                                                          );
        final List<MethodInfo> methodInformation = methodInspector.getMethodInfos();
        for ( final MethodInfo mi : methodInformation ) {
            final String genericType = mi.getParametricReturnType();
            if ( genericType != null ) {
                final String qualifiedFactFieldName = factType + "#" + mi.getNameWithParameters();
                this..putqualifiedFactFieldName,
                                              genericType );
            }
        }
        this..put( factType,
                                    methodInformation );
    }
    // Remove the unneeded "fields" that come from java.lang.Object
    private List<StringremoveIrrelevantFieldsfinal Collection<Stringfields ) {
        final List<Stringresult = new ArrayList<String>();
        for ( String field : fields ) {
            //clone, empty, iterator, listIterator, size, toArray
            if ( !( field.equals"class" ) || field.equals"hashCode" ) || field.equals"toString" ) ) ) {
                result.addfield );
            }
        }
        return result;
    }
    // Remove the unneeded "methods" that come from java.lang.Object
    private Map<StringMethodSignatureremoveIrrelevantMethodsfinal Map<StringMethodSignaturemethods ) {
        final Map<StringMethodSignatureresult = new HashMap<StringMethodSignature>();
        for ( Map.Entry<StringMethodSignaturemethodSignature : methods.entrySet() ) {
            String methodName = methodSignature.getKey();
            methodName = methodName.substringmethodName.lastIndexOf"." ) + 1 );
            if ( !methodName.equals"class" ) ) {
                result.putmethodSignature.getKey(),
                            methodSignature.getValue() );
            }
        }
        return result;
    }
    private Map<StringMethodSignaturegetMethodSignaturesfinal String factType,
                                                              final Method[] methods ) {
        Map<StringMethodSignaturemethodSignatures = new HashMap<StringMethodSignature>();
        //Determine accessors for methods
        for ( Method method : methods ) {
            String name = method.getName();
            if ( method.getParameterTypes().length > 0 ) {
                //Strip bare mutator name
                if ( name.startsWith"set" ) ) {
                    name = Introspector.decapitalizename.substring( 3 ) );
                } else {
                    name = Introspector.decapitalizename );
                }
                final String factField = factType + "." + name;
                if ( !methodSignatures.containsKeyfactField ) ) {
                    methodSignatures.putfactField,
                                          new MethodSignature( FieldAccessorsAndMutators.MUTATOR,
                                                               void.class.getGenericSuperclass(),
                                                               void.class ) );
                } else if ( methodSignatures.getfactField ). == FieldAccessorsAndMutators.ACCESSOR ) {
                    MethodSignature signature = methodSignatures.getfactField );
                    signature.accessorAndMutator = FieldAccessorsAndMutators.BOTH;
                }
            } else if ( !method.getReturnType().getName().equals"void" ) ) {
                //Strip bare accessor name
                if ( name.startsWith"get" ) ) {
                    name = Introspector.decapitalizename.substring( 3 ) );
                } else if ( name.startsWith"is" ) ) {
                    name = Introspector.decapitalizename.substring( 2 ) );
                } else {
                    name = Introspector.decapitalizename );
                }
                final String factField = factType + "." + name;
                if ( !methodSignatures.containsKeyfactField ) ) {
                    methodSignatures.putfactField,
                                          new MethodSignature( FieldAccessorsAndMutators.ACCESSOR,
                                                               method.getGenericReturnType(),
                                                               method.getReturnType() ) );
                } else if ( methodSignatures.getfactField ). == FieldAccessorsAndMutators.MUTATOR ) {
                    MethodSignature signature = methodSignatures.getfactField );
                    signature.accessorAndMutator = FieldAccessorsAndMutators.BOTH;
                    signature.genericType = method.getGenericReturnType();
                    signature.returnType = method.getReturnType();
                }
            }
        }
        return methodSignatures;
    }
    private static class MethodSignature {
        private MethodSignaturefinal FieldAccessorsAndMutators accessorAndMutator,
                                 final Type genericType,
                                 final Class<?> returnType ) {
            this. = accessorAndMutator;
            this. = genericType;
            this. = returnType;
        }
        private FieldAccessorsAndMutators accessorAndMutator;
        private Type genericType;
        private Class<?> returnType;
    }
    private void addEnumsForFieldfinal String className,
                                   final String fieldName,
                                   final Class<?> fieldClazz ) {
        if ( fieldClazz.isEnum() ) {
            final Field[] enumFields = fieldClazz.getDeclaredFields();
            final List<StringenumValues = new ArrayList<String>();
            for ( final Field enumField : enumFields ) {
                if ( enumField.isEnumConstant() ) {
                    final String shortName = fieldClazz.getName().substringfieldClazz.getName().lastIndexOf"." ) + 1 ) + "." + enumField.getName();
                    enumValues.addshortName + "=" + shortName );
                }
            }
            final String a[] = new StringenumValues.size() ];
            enumValues.toArraya );
            getDataModelBuilder().addEnumclassName,
                                           fieldName,
                                           a );
        }
    }
    private void addParametricTypeForFieldfinal String className,
                                            final String fieldName,
                                            final Type type ) {
        final String qualifiedFactFieldName = className + "#" + fieldName;
        final String parametricType = getParametricTypetype );
        if ( parametricType != null ) {
            .putqualifiedFactFieldName,
                                     parametricType );
        }
    }
    private String getParametricTypefinal Type type ) {
        if ( type instanceof ParameterizedType ) {
            final ParameterizedType pt = (ParameterizedTypetype;
            Type parameter = null;
            for ( final Type t : pt.getActualTypeArguments() ) {
                parameter = t;
            }
            if ( parameter != null ) {
                if ( parameter instanceof Class<?> ) {
                    return ( (Class<?>) parameter ).getName();
                }
                return null;
            } else {
                return null;
            }
        }
        return null;
    }
    private Map<StringStringbuildSuperTypes() {
        final Map<StringStringloadableSuperTypes = new HashMap<StringString>();
        loadableSuperTypes.putgetType(),
                                 );
        return loadableSuperTypes;
    }
    private Map<StringSet<Annotation>> buildTypeAnnotations() {
        final Map<StringSet<Annotation>> loadableTypeAnnotations = new HashMap<StringSet<Annotation>>();
        loadableTypeAnnotations.put( getType(),
                                      );
        return loadableTypeAnnotations;
    }
    private Map<StringMap<StringSet<Annotation>>> buildTypeFieldsAnnotations() {
        final Map<StringMap<StringSet<Annotation>>> loadableTypeFieldsAnnotations = new HashMap<StringMap<StringSet<Annotation>>>();
        loadableTypeFieldsAnnotations.put( getType(),
                                            );
        return loadableTypeFieldsAnnotations;
    }
New to GrepCode? Check out our FAQ X