Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2015 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.kie.workbench.common.services.datamodel.backend.server.builder.projects;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.drools.workbench.models.datamodel.oracle.FieldAccessorsAndMutators;
 import  org.drools.workbench.models.datamodel.oracle.ModelField;
Find information for all "fields" on a class. A "field" is either a public property or a non-public property for which there is a "getter" and/or a "setter"
 
 public class ClassFieldInspector {
 
     private final Map<StringFieldInfofieldTypesFieldInfo = new HashMap<StringFieldInfo>();
 
     public ClassFieldInspectorfinal Class<?> clazz ) throws IOException {
         //Handle fields
         final List<Fieldfields = new ArrayList<Field>( getAllFieldsclazz ).values() );
         final List<FielddeclaredFields = Arrays.asListclazz.getDeclaredFields() );
         for ( Field field : fields ) {
             if ( BlackLists.isClassMethodBlackListedclazz,
                                                       field.getName() ) ) {
                 continue;
             }
             if ( BlackLists.isTypeBlackListedfield.getType() ) ) {
                 continue;
             }
 
             if ( Modifier.isPublicfield.getModifiers() ) && !Modifier.isStaticfield.getModifiers() ) ) {
                 this..putfield.getName(),
                                               new FieldInfo( FieldAccessorsAndMutators.BOTH,
                                                              field.getGenericType(),
                                                              field.getType(),
                                                              declaredFields.containsfield ) ? ModelField.FIELD_ORIGIN.DECLARED : ModelField.FIELD_ORIGIN.INHERITED ) );
             }
         }
 
         //Handle methods
         final List<Methodmethods = new ArrayList<Method>( getAllMethodsclazz ).values() );
         for ( Method method : methods ) {
             final int modifiers = method.getModifiers();
             if ( Modifier.isPublicmodifiers ) && !Modifier.isStaticmethod.getModifiers() ) ) {
                 String methodName = null;
                 FieldAccessorsAndMutators accessorsAndMutators = null;
                 if ( isGettermethod ) ) {
                     methodName = method.getName().substring( 3 );
                     methodName = Introspector.decapitalizemethodName );
                     accessorsAndMutators = FieldAccessorsAndMutators.ACCESSOR;
 
                 } else if ( isBooleanGettermethod ) ) {
                     methodName = method.getName().substring( 2 );
                     methodName = Introspector.decapitalizemethodName );
                     accessorsAndMutators = FieldAccessorsAndMutators.ACCESSOR;
 
                 } else if ( isSettermethod ) ) {
                     methodName = method.getName().substring( 3 );
                     methodName = Introspector.decapitalizemethodName );
                     accessorsAndMutators = FieldAccessorsAndMutators.MUTATOR;
                 }
 
                 if ( methodName != null ) {
                     if ( BlackLists.isClassMethodBlackListedclazz,
                                                               methodName ) ) {
                         continue;
                     }
                     if ( BlackLists.isTypeBlackListedmethod.getReturnType() ) ) {
                         continue;
                     }
 
                     //Correct accessor information, if a Field has already been discovered
                     if ( this..containsKeymethodName ) ) {
                         final FieldInfo info = this..getmethodName );
                         if ( accessorsAndMutators == FieldAccessorsAndMutators.ACCESSOR ) {
                            if ( info.accessorAndMutator == FieldAccessorsAndMutators.MUTATOR ) {
                                info.accessorAndMutator = FieldAccessorsAndMutators.BOTH;
                            }
                            info.genericType = method.getGenericReturnType();
                            info.returnType = method.getReturnType();
                        } else if ( accessorsAndMutators == FieldAccessorsAndMutators.MUTATOR ) {
                            if ( info.accessorAndMutator == FieldAccessorsAndMutators.ACCESSOR ) {
                                info.accessorAndMutator = FieldAccessorsAndMutators.BOTH;
                            }
                        }
                    } else {
                        final ModelField.FIELD_ORIGIN origin = getOriginmethodName,
                                                                          getNamesfields ),
                                                                          getNamesdeclaredFields ) );
                        this..putmethodName,
                                                      new FieldInfoaccessorsAndMutators,
                                                                     method.getGenericReturnType(),
                                                                     method.getReturnType(),
                                                                     origin ) );
                    }
                }
            }
        }
    }
    private boolean isGetterfinal Method m ) {
        String name = m.getName();
        int parameterCount = m.getParameterTypes().length;
        if ( parameterCount != 0 ) {
            return false;
        }
        return ( name.length() > 3 && name.startsWith"get" ) );
    }
    private boolean isBooleanGetterfinal Method m ) {
        String name = m.getName();
        int parameterCount = m.getParameterTypes().length;
        if ( parameterCount != 0 ) {
            return false;
        }
        return ( name.length() > 2 && name.startsWith"is" ) && ( Boolean.class.isAssignableFromm.getReturnType() ) || . == m.getReturnType() ) );
    }
    private boolean isSetterfinal Method m ) {
        String name = m.getName();
        int parameterCount = m.getParameterTypes().length;
        if ( parameterCount != 1 ) {
            return false;
        }
        return ( name.length() > 3 && name.startsWith"set" ) );
    }
    private List<StringgetNamesfinal List<Fieldfields ) {
        final List<Stringnames = new ArrayList<String>();
        for ( Field field : fields ) {
            names.addfield.getName() );
        }
        return names;
    }
    private ModelField.FIELD_ORIGIN getOrigin( final String methodName,
                                               final List<Stringfields,
                                               final List<StringdeclaredFields ) {
        if ( declaredFields.containsmethodName ) ) {
            return ModelField.FIELD_ORIGIN.DECLARED;
        }
        if ( fields.containsmethodName ) ) {
            return ModelField.FIELD_ORIGIN.INHERITED;
        }
        return ModelField.FIELD_ORIGIN.DELEGATED;
    }
    public Set<StringgetFieldNames() {
        return this..keySet();
    }
        return this.;
    }
    //class.getDeclaredField(String) doesn't walk the inheritance tree; this does
    private Map<StringFieldgetAllFieldsClass<?> type ) {
        Map<StringFieldfields = new HashMap<StringField>();
        for ( Class<?> c = typec != nullc = c.getSuperclass() ) {
            for ( Field f : c.getDeclaredFields() ) {
                fields.putf.getName(), f );
            }
        }
        return fields;
    }
    //class.getDeclaredMethods() doesn't walk the inheritance tree; this does
    private Map<StringMethodgetAllMethodsClass<?> type ) {
        Map<StringMethodmethods = new HashMap<StringMethod>();
        for ( Class<?> c = typec != nullc = c.getSuperclass() ) {
            for ( Method m : c.getDeclaredMethods() ) {
                methods.putm.getName(), m );
            }
        }
        return methods;
    }
    public static class FieldInfo {
        private FieldAccessorsAndMutators accessorAndMutator;
        private Type genericType;
        private Class<?> returnType;
        private ModelField.FIELD_ORIGIN origin;
        private FieldInfo( final FieldAccessorsAndMutators accessorAndMutator,
                           final Type genericType,
                           final Class<?> returnType,
                           final ModelField.FIELD_ORIGIN origin ) {
            this. = accessorAndMutator;
            this. = genericType;
            this. = returnType;
            this. = origin;
        }
        public FieldAccessorsAndMutators getAccessorAndMutator() {
            return ;
        }
        public Type getGenericType() {
            return ;
        }
        public Class<?> getReturnType() {
            return ;
        }
        public ModelField.FIELD_ORIGIN getOrigin() {
            return origin;
        }
    }
New to GrepCode? Check out our FAQ X