Search in sources :

Example 1 with PropertyDescriptor

use of jodd.introspector.PropertyDescriptor in project jodd by oblac.

the class JsonAnnotationManager method scanClassForAnnotations.

/**
	 * Scans class for annotations and returns {@link jodd.json.meta.JsonAnnotationManager.TypeData}.
	 */
private TypeData scanClassForAnnotations(Class type) {
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    PropertyDescriptor[] pds = cd.getAllPropertyDescriptors();
    ArrayList<String> includedList = new ArrayList<>();
    ArrayList<String> excludedList = new ArrayList<>();
    ArrayList<String> jsonNames = new ArrayList<>();
    ArrayList<String> realNames = new ArrayList<>();
    JSONAnnotation jsonAnnotation = new JSONAnnotation(JoddJson.jsonAnnotation);
    for (PropertyDescriptor pd : pds) {
        JSONAnnotationData data = null;
        {
            MethodDescriptor md = pd.getReadMethodDescriptor();
            if (md != null) {
                Method method = md.getMethod();
                data = jsonAnnotation.readAnnotationData(method);
            }
        }
        if (data == null) {
            MethodDescriptor md = pd.getWriteMethodDescriptor();
            if (md != null) {
                Method method = md.getMethod();
                data = jsonAnnotation.readAnnotationData(method);
            }
        }
        if (data == null) {
            FieldDescriptor fd = pd.getFieldDescriptor();
            if (fd != null) {
                Field field = fd.getField();
                data = jsonAnnotation.readAnnotationData(field);
            }
        }
        if (data != null) {
            // annotation found
            String propertyName = pd.getName();
            String newPropertyName = data.getName();
            if (newPropertyName != null) {
                realNames.add(propertyName);
                jsonNames.add(newPropertyName);
                propertyName = newPropertyName;
            }
            if (data.isIncluded()) {
                includedList.add(propertyName);
            } else {
                excludedList.add(propertyName);
            }
        }
    }
    String[] reals = null;
    if (!realNames.isEmpty()) {
        reals = realNames.toArray(new String[realNames.size()]);
    }
    String[] jsons = null;
    if (!jsonNames.isEmpty()) {
        jsons = jsonNames.toArray(new String[jsonNames.size()]);
    }
    // type
    JSONAnnotationData data = (JSONAnnotationData) jsonAnnotation.readAnnotationData(type);
    return new TypeData(includedList, excludedList, data != null && data.isStrict(), jsons, reals);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) MethodDescriptor(jodd.introspector.MethodDescriptor) FieldDescriptor(jodd.introspector.FieldDescriptor) Field(java.lang.reflect.Field)

Example 2 with PropertyDescriptor

use of jodd.introspector.PropertyDescriptor in project jodd by oblac.

the class JsonParser method parseObjectContent.

// ---------------------------------------------------------------- object
/**
	 * Parses object, once when open bracket has been consumed.
	 */
protected Object parseObjectContent(Class targetType, Class valueKeyType, Class valueType) {
    if (targetType == Object.class) {
        targetType = Map.class;
    }
    // continue
    targetType = replaceWithMappedTypeForPath(targetType);
    Object target;
    boolean isTargetTypeMap = true;
    boolean isTargetRealTypeMap = true;
    ClassDescriptor targetTypeClassDescriptor = null;
    JsonAnnotationManager.TypeData typeData = null;
    if (targetType != null) {
        targetTypeClassDescriptor = ClassIntrospector.lookup(targetType);
        // find if the target is really a map
        // because when classMetadataName != null we are forcing
        // map usage locally in this method
        isTargetRealTypeMap = targetTypeClassDescriptor.isMap();
        typeData = JoddJson.annotationManager.lookupTypeData(targetType);
    }
    if (isTargetRealTypeMap) {
        // resolve keys only for real maps
        path.push(KEYS);
        valueKeyType = replaceWithMappedTypeForPath(valueKeyType);
        path.pop();
    }
    if (classMetadataName == null) {
        // create instance of target type, no 'class' information
        target = newObjectInstance(targetType);
        isTargetTypeMap = isTargetRealTypeMap;
    } else {
        // all beans will be created first as a map
        target = new HashMap();
    }
    boolean koma = false;
    mainloop: while (true) {
        skipWhiteSpaces();
        char c = input[ndx];
        if (c == '}') {
            if (koma) {
                syntaxError("Trailing comma");
            }
            ndx++;
            break;
        }
        koma = false;
        String key = parseString();
        String keyOriginal = key;
        skipWhiteSpaces();
        consume(':');
        skipWhiteSpaces();
        // read the type of the simple property
        PropertyDescriptor pd = null;
        Class propertyType = null;
        Class keyType = null;
        Class componentType = null;
        if (!isTargetRealTypeMap) {
            // replace key with real property value
            key = JoddJson.annotationManager.resolveRealName(targetType, key);
        }
        if (!isTargetTypeMap) {
            pd = targetTypeClassDescriptor.getPropertyDescriptor(key, true);
            if (pd != null) {
                propertyType = pd.getType();
                keyType = pd.resolveKeyType(true);
                componentType = pd.resolveComponentType(true);
            }
        }
        Object value;
        if (!isTargetTypeMap) {
            // *** inject into bean
            path.push(key);
            value = parseValue(propertyType, keyType, componentType);
            path.pop();
            if (typeData.rules.match(keyOriginal, !typeData.strict)) {
                if (pd != null) {
                    // only inject values if target property exist
                    injectValueIntoObject(target, pd, value);
                }
            }
        } else {
            Object keyValue = key;
            if (valueKeyType != null) {
                keyValue = convertType(key, valueKeyType);
            }
            // *** add to map
            if (isTargetRealTypeMap) {
                path.push(VALUES, key);
                valueType = replaceWithMappedTypeForPath(valueType);
            } else {
                path.push(key);
            }
            value = parseValue(valueType, null, null);
            path.pop();
            ((Map) target).put(keyValue, value);
        }
        skipWhiteSpaces();
        c = input[ndx];
        switch(c) {
            case '}':
                ndx++;
                break mainloop;
            case ',':
                ndx++;
                koma = true;
                break;
            default:
                syntaxError("Invalid char: expected } or ,");
        }
    }
    // convert Map to target type
    if (classMetadataName != null) {
        target = mapToBean.map2bean((Map) target, targetType);
    }
    return target;
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) JsonAnnotationManager(jodd.json.meta.JsonAnnotationManager)

Example 3 with PropertyDescriptor

use of jodd.introspector.PropertyDescriptor in project jodd by oblac.

the class DbEntityDescriptor method resolveColumnsAndProperties.

/**
	 * Resolves list of all columns and properties.
	 */
private void resolveColumnsAndProperties(Class type) {
    PropertyDescriptor[] allProperties = ClassIntrospector.lookup(type).getAllPropertyDescriptors();
    List<DbEntityColumnDescriptor> decList = new ArrayList<>(allProperties.length);
    int idcount = 0;
    HashSet<String> names = new HashSet<>(allProperties.length);
    for (PropertyDescriptor propertyDescriptor : allProperties) {
        DbEntityColumnDescriptor dec = DbMetaUtil.resolveColumnDescriptors(this, propertyDescriptor, isAnnotated, columnNamingStrategy);
        if (dec != null) {
            if (!names.add(dec.getColumnName())) {
                throw new DbOomException("Duplicate column name: " + dec.getColumnName());
            }
            decList.add(dec);
            if (dec.isId) {
                idcount++;
            }
        }
    }
    if (decList.isEmpty()) {
        throw new DbOomException("No column mappings in entity: " + type);
    }
    columnDescriptors = decList.toArray(new DbEntityColumnDescriptor[decList.size()]);
    Arrays.sort(columnDescriptors);
    // extract ids from sorted list
    if (idcount > 0) {
        idColumnDescriptors = new DbEntityColumnDescriptor[idcount];
        idcount = 0;
        for (DbEntityColumnDescriptor dec : columnDescriptors) {
            if (dec.isId) {
                idColumnDescriptors[idcount++] = dec;
            }
        }
    }
}
Also used : PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 4 with PropertyDescriptor

use of jodd.introspector.PropertyDescriptor in project jodd by oblac.

the class PropertyResolver method resolve.

/**
	 * Resolves all properties for given type.
	 */
public PropertyInjectionPoint[] resolve(Class type, boolean autowire) {
    // lookup fields
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    List<PropertyInjectionPoint> list = new ArrayList<>();
    PropertyDescriptor[] allPropertyDescriptors = cd.getAllPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : allPropertyDescriptors) {
        if (propertyDescriptor.isGetterOnly()) {
            continue;
        }
        Class propertyType = propertyDescriptor.getType();
        if (ReflectUtil.isTypeOf(propertyType, Collection.class)) {
            continue;
        }
        MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor();
        FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor();
        PetiteInject ref = null;
        if (writeMethodDescriptor != null) {
            ref = writeMethodDescriptor.getMethod().getAnnotation(PetiteInject.class);
        }
        if (ref == null && fieldDescriptor != null) {
            ref = fieldDescriptor.getField().getAnnotation(PetiteInject.class);
        }
        if ((!autowire) && (ref == null)) {
            continue;
        }
        String[] refName = null;
        if (ref != null) {
            String name = ref.value().trim();
            if (name.length() != 0) {
                refName = new String[] { name };
            }
        }
        list.add(injectionPointFactory.createPropertyInjectionPoint(propertyDescriptor, refName));
    }
    PropertyInjectionPoint[] fields;
    if (list.isEmpty()) {
        fields = PropertyInjectionPoint.EMPTY;
    } else {
        fields = list.toArray(new PropertyInjectionPoint[list.size()]);
    }
    return fields;
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) PetiteInject(jodd.petite.meta.PetiteInject) MethodDescriptor(jodd.introspector.MethodDescriptor) FieldDescriptor(jodd.introspector.FieldDescriptor) PropertyInjectionPoint(jodd.petite.PropertyInjectionPoint)

Example 5 with PropertyDescriptor

use of jodd.introspector.PropertyDescriptor in project jodd by oblac.

the class SetResolver method resolve.

/**
	 * Resolves all collections for given type.
	 */
public SetInjectionPoint[] resolve(Class type, boolean autowire) {
    ClassDescriptor cd = ClassIntrospector.lookup(type);
    List<SetInjectionPoint> list = new ArrayList<>();
    PropertyDescriptor[] allProperties = cd.getAllPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : allProperties) {
        if (propertyDescriptor.isGetterOnly()) {
            continue;
        }
        Class propertyType = propertyDescriptor.getType();
        if (!ReflectUtil.isTypeOf(propertyType, Collection.class)) {
            continue;
        }
        MethodDescriptor writeMethodDescriptor = propertyDescriptor.getWriteMethodDescriptor();
        FieldDescriptor fieldDescriptor = propertyDescriptor.getFieldDescriptor();
        PetiteInject ref = null;
        if (writeMethodDescriptor != null) {
            ref = writeMethodDescriptor.getMethod().getAnnotation(PetiteInject.class);
        }
        if (ref == null && fieldDescriptor != null) {
            ref = fieldDescriptor.getField().getAnnotation(PetiteInject.class);
        }
        if ((!autowire) && (ref == null)) {
            continue;
        }
        list.add(injectionPointFactory.createSetInjectionPoint(propertyDescriptor));
    }
    SetInjectionPoint[] fields;
    if (list.isEmpty()) {
        fields = SetInjectionPoint.EMPTY;
    } else {
        fields = list.toArray(new SetInjectionPoint[list.size()]);
    }
    return fields;
}
Also used : SetInjectionPoint(jodd.petite.SetInjectionPoint) ClassDescriptor(jodd.introspector.ClassDescriptor) PropertyDescriptor(jodd.introspector.PropertyDescriptor) ArrayList(java.util.ArrayList) Collection(java.util.Collection) PetiteInject(jodd.petite.meta.PetiteInject) MethodDescriptor(jodd.introspector.MethodDescriptor) FieldDescriptor(jodd.introspector.FieldDescriptor)

Aggregations

PropertyDescriptor (jodd.introspector.PropertyDescriptor)20 ClassDescriptor (jodd.introspector.ClassDescriptor)17 ArrayList (java.util.ArrayList)10 FieldDescriptor (jodd.introspector.FieldDescriptor)5 MethodDescriptor (jodd.introspector.MethodDescriptor)5 Test (org.junit.Test)4 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 LifeBean (jodd.bean.data.LifeBean)2 CachingIntrospector (jodd.introspector.CachingIntrospector)2 In (jodd.madvoc.meta.In)2 Out (jodd.madvoc.meta.Out)2 PetiteInject (jodd.petite.meta.PetiteInject)2 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1