use of com.j256.ormlite.field.DatabaseField in project ormlite-android by j256.
the class DatabaseTableConfigUtil method buildConfig.
/**
* Instead of calling the annotation methods directly, we peer inside the proxy and investigate the array of
* AnnotationMember objects stored by the AnnotationFactory.
*/
private static DatabaseFieldConfig buildConfig(DatabaseField databaseField, String tableName, Field field) throws Exception {
InvocationHandler proxy = Proxy.getInvocationHandler(databaseField);
if (proxy.getClass() != annotationFactoryClazz) {
return null;
}
// this should be an array of AnnotationMember objects
Object elementsObject = elementsField.get(proxy);
if (elementsObject == null) {
return null;
}
DatabaseFieldConfig config = new DatabaseFieldConfig(field.getName());
Object[] objs = (Object[]) elementsObject;
for (int i = 0; i < configFieldNums.length; i++) {
Object value = valueField.get(objs[i]);
if (value != null) {
assignConfigField(configFieldNums[i], config, field, value);
}
}
return config;
}
use of com.j256.ormlite.field.DatabaseField in project ormlite-android by j256.
the class DatabaseTableConfigUtil method configFromField.
/**
* Extract our configuration information from the field by looking for a {@link DatabaseField} annotation.
*/
private static DatabaseFieldConfig configFromField(DatabaseType databaseType, String tableName, Field field) throws SQLException {
if (configFieldNums == null) {
return DatabaseFieldConfig.fromField(databaseType, tableName, field);
}
/*
* This, unfortunately, we can't get around. This creates a AnnotationFactory, an array of AnnotationMember
* fields, and possibly another array of AnnotationMember values. This creates a lot of GC'd objects.
*/
DatabaseField databaseField = field.getAnnotation(DatabaseField.class);
DatabaseFieldConfig config = null;
try {
if (databaseField != null) {
config = buildConfig(databaseField, tableName, field);
}
} catch (Exception e) {
// ignored so we will configure normally below
}
if (config == null) {
/*
* We configure this the old way because we might be using javax annotations, have a ForeignCollectionField,
* or may still be using the deprecated annotations. At this point we know that there isn't a @DatabaseField
* or we can't do our reflection hacks for some reason.
*/
return DatabaseFieldConfig.fromField(databaseType, tableName, field);
} else {
workedC++;
return config;
}
}
use of com.j256.ormlite.field.DatabaseField in project ormlite-android by j256.
the class DatabaseTableConfigUtil method lookupClasses.
/**
* This does all of the class reflection fu to find our classes, find the order of field names, and construct our
* array of ConfigField entries the correspond to the AnnotationMember array.
*/
private static int[] lookupClasses() {
Class<?> annotationMemberArrayClazz;
try {
annotationFactoryClazz = Class.forName("org.apache.harmony.lang.annotation.AnnotationFactory");
annotationMemberClazz = Class.forName("org.apache.harmony.lang.annotation.AnnotationMember");
annotationMemberArrayClazz = Class.forName("[Lorg.apache.harmony.lang.annotation.AnnotationMember;");
} catch (ClassNotFoundException e) {
return null;
}
Field fieldField;
try {
elementsField = annotationFactoryClazz.getDeclaredField("elements");
elementsField.setAccessible(true);
nameField = annotationMemberClazz.getDeclaredField("name");
nameField.setAccessible(true);
valueField = annotationMemberClazz.getDeclaredField("value");
valueField.setAccessible(true);
fieldField = DatabaseFieldSample.class.getDeclaredField("field");
} catch (SecurityException e) {
return null;
} catch (NoSuchFieldException e) {
return null;
}
DatabaseField databaseField = fieldField.getAnnotation(DatabaseField.class);
InvocationHandler proxy = Proxy.getInvocationHandler(databaseField);
if (proxy.getClass() != annotationFactoryClazz) {
return null;
}
try {
// this should be an array of AnnotationMember objects
Object elements = elementsField.get(proxy);
if (elements == null || elements.getClass() != annotationMemberArrayClazz) {
return null;
}
Object[] elementArray = (Object[]) elements;
int[] configNums = new int[elementArray.length];
// build our array of field-numbers that match the AnnotationMember array
for (int i = 0; i < elementArray.length; i++) {
String name = (String) nameField.get(elementArray[i]);
configNums[i] = configFieldNameToNum(name);
}
return configNums;
} catch (IllegalAccessException e) {
return null;
}
}
Aggregations