本文整理汇总了Java中com.j256.ormlite.field.DatabaseField类的典型用法代码示例。如果您正苦于以下问题:Java DatabaseField类的具体用法?Java DatabaseField怎么用?Java DatabaseField使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DatabaseField类属于com.j256.ormlite.field包,在下文中一共展示了DatabaseField类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findIdField
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
/**
* Find a Field with a DatabaseField annotation that defines it as being an id column.
*
* @param annotationRetriever the annotation retriever that caches the annotations
* @param modelClass the class to find the ID field in
* @return the Field or null
*/
@Nullable
public static Field findIdField(AnnotationRetriever annotationRetriever, Class<?> modelClass) {
for (Field field : modelClass.getDeclaredFields()) {
DatabaseField database_field = annotationRetriever.getAnnotation(field, DatabaseField.class);
if (database_field == null) {
continue;
}
if (database_field.generatedId() || database_field.id()) {
return field;
}
}
if (modelClass.getSuperclass() == null) {
return null;
}
// Recursively check superclass
return findIdField(annotationRetriever, modelClass.getSuperclass());
}
示例2: findForeignField
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
/**
* Find a Field with a DatabaseField annotation that defines it as foreign.
*
* @param annotationRetriever the annotation retriever that caches the annotations
* @param parentClass the class to search for the Field
* @param findClass the field class to search for
* @return a Field or null
*/
@Nullable
public static Field findForeignField(AnnotationRetriever annotationRetriever, Class<?> parentClass, Class<?> findClass) {
for (Field field : parentClass.getDeclaredFields()) {
DatabaseField database_field = annotationRetriever.getAnnotation(field, DatabaseField.class);
if (database_field != null
&& isForeign(database_field)
&& findClass.isAssignableFrom(field.getType())) {
return field;
}
}
if (parentClass.getSuperclass() == null) {
return null;
}
// Recursively check superclass
return findForeignField(annotationRetriever, parentClass.getSuperclass(), findClass);
}
示例3: getFieldsIfValueNotNull
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
/**
* 获取对象DatabaseField注解的属性值不为空的属性名称和属性值
*
* @param obj 数据实体对象
* @return 属性名称和属性值键值对集合
*/
private Map<String, Object> getFieldsIfValueNotNull(Object obj) {
Map<String, Object> map = new HashMap<>();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (field.getAnnotation(DatabaseField.class) == null) {
continue;
}
Object valueObj = null;
try {
valueObj = field.get(obj);
} catch (IllegalAccessException e) {
LogUtils.e(e);
}
if (valueObj != null) {
map.put(field.getName(), valueObj);
}
}
return map;
}
示例4: buildConfig
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
private static DatabaseFieldConfig buildConfig(DatabaseField paramDatabaseField, String paramString, Field paramField)
{
InvocationHandler localInvocationHandler = Proxy.getInvocationHandler(paramDatabaseField);
if (localInvocationHandler.getClass() != annotationFactoryClazz)
return null;
Object localObject1 = elementsField.get(localInvocationHandler);
if (localObject1 == null)
return null;
DatabaseFieldConfig localDatabaseFieldConfig = new DatabaseFieldConfig(paramField.getName());
Object[] arrayOfObject = (Object[])localObject1;
for (int i = 0; i < configFieldNums.length; i++)
{
Object localObject2 = valueField.get(arrayOfObject[i]);
if (localObject2 != null)
assignConfigField(configFieldNums[i], localDatabaseFieldConfig, paramField, localObject2);
}
return localDatabaseFieldConfig;
}
示例5: configFromField
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
private static DatabaseFieldConfig configFromField(DatabaseType paramDatabaseType, String paramString, Field paramField)
{
if (configFieldNums == null)
return DatabaseFieldConfig.fromField(paramDatabaseType, paramString, paramField);
DatabaseField localDatabaseField = (DatabaseField)paramField.getAnnotation(DatabaseField.class);
DatabaseFieldConfig localDatabaseFieldConfig = null;
if (localDatabaseField != null);
try
{
localDatabaseFieldConfig = buildConfig(localDatabaseField, paramString, paramField);
}
catch (Exception localException)
{
}
if (localDatabaseFieldConfig == null)
return DatabaseFieldConfig.fromField(paramDatabaseType, paramString, paramField);
workedC = 1 + workedC;
return localDatabaseFieldConfig;
}
示例6: extractFieldTypes
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
private static <T> FieldType[] extractFieldTypes(ConnectionSource paramConnectionSource, Class<T> paramClass, String paramString)
{
ArrayList localArrayList = new ArrayList();
for (Object localObject = paramClass; localObject != null; localObject = ((Class)localObject).getSuperclass())
{
Field[] arrayOfField = ((Class)localObject).getDeclaredFields();
int i = arrayOfField.length;
for (int j = 0; j < i; j++)
{
FieldType localFieldType = FieldType.createFieldType(paramConnectionSource, paramString, arrayOfField[j], paramClass);
if (localFieldType != null)
localArrayList.add(localFieldType);
}
}
if (localArrayList.isEmpty())
throw new IllegalArgumentException("No fields have a " + DatabaseField.class.getSimpleName() + " annotation in " + paramClass);
return (FieldType[])localArrayList.toArray(new FieldType[localArrayList.size()]);
}
示例7: getForeignObjects
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
private List<Object> getForeignObjects(final Object object) {
List<Object> results = new ArrayList<>();
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
DatabaseField annotation = field.getAnnotation(DatabaseField.class);
if (annotation != null) {
if (annotation.foreign()) {
try {
field.setAccessible(true);
results.add(field.get(object));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
}
return results;
}
示例8: buildConfig
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
/**
* 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;
}
示例9: extractFieldTypes
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
private static <T> FieldType[] extractFieldTypes(ConnectionSource connectionSource, Class<T> clazz, String tableName)
throws SQLException {
List<FieldType> fieldTypes = new ArrayList<FieldType>();
for (Class<?> classWalk = clazz; classWalk != null; classWalk = classWalk.getSuperclass()) {
for (Field field : classWalk.getDeclaredFields()) {
FieldType fieldType = FieldType.createFieldType(connectionSource, tableName, field, clazz);
if (fieldType != null) {
fieldTypes.add(fieldType);
}
}
}
if (fieldTypes.isEmpty()) {
throw new IllegalArgumentException("No fields have a " + DatabaseField.class.getSimpleName()
+ " annotation in " + clazz);
}
return fieldTypes.toArray(new FieldType[fieldTypes.size()]);
}
示例10: getFieldName
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
/**
* Get SQLite column name for a given Field.
*
* @param annotationRetriever the annotation retriever that caches the annotations
* @param field the model's field
* @return the SQLite column name
*/
public static String getFieldName(AnnotationRetriever annotationRetriever, Field field) {
DatabaseField database_field = annotationRetriever.getAnnotation(field, DatabaseField.class);
if (database_field == null) {
throw new RuntimeException("DatabaseField annotation not found in " + field.getDeclaringClass().getName() + " for " + field.getName());
}
return getFieldName(field, database_field);
}
示例11: processIdField
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
/**
* Process an ID field giving JSON input and serialization information.
* If no object is found in the database, a new one is inserted and its ID is returned.
*
* @param databaseField the Ormlite annotation
* @param field the field that is annotated by databaseField
* @param jsonObject the object that is being mapped
* @param jsonKey the key where the value of the id field can be found within the jsonObject
* @param tableName the table to insert a new row in case the ID is not found in the database
* @return the ID field value of this object (never null)
* @throws JSONException when the ID field value cannot be determined
*/
private Object processIdField(DatabaseField databaseField, Field field, JSONObject jsonObject, String jsonKey, String tableName) throws JSONException {
String db_field_name = OrmliteReflection.getFieldName(field, databaseField);
Object object_id = JsonUtils.getValue(jsonObject, jsonKey, field.getType());
if (object_id == null) {
throw new RuntimeException(String.format("failed to get a value from JSON with key %s and type %s", jsonKey, field.getType().getName()));
}
String sql = String.format("SELECT * FROM '%s' WHERE %s = ? LIMIT 1", tableName, db_field_name);
String[] selection_args = new String[]{object_id.toString()};
Cursor cursor = database.rawQuery(sql, selection_args);
boolean object_exists = (cursor.getCount() > 0);
cursor.close();
if (object_exists) {
// return existing object id
return object_id;
} else { // create object
ContentValues values = new ContentValues(1);
if (!JsonUtils.copyValue(object_id, db_field_name, values)) {
throw new JSONException(String.format("failed to process id field %s for table %s and jsonKey %s", field.getName(), tableName, jsonKey));
}
long inserted_id = database.insert("'" + tableName + "'", null, values);
if (inserted_id == -1) {
throw new SQLiteException(String.format("failed to insert %s with id %s=%s", field.getType().getName(), db_field_name, object_id.toString()));
}
Log.i(getClass().getName(), String.format("prepared %s row (id=%s/%s)", tableName, object_id.toString(), Long.toString(inserted_id)));
return object_id; // don't return inserted_id, because it's always long (while the target type might be int or another type)
}
}
示例12: setObjectValueIfNotNull
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
/**
* 过滤数据实体中DatabaseField注解的字段值为null的字段
*
* @param t 过滤后不包含null字段的数据
* @param obj 被过滤的数据
* @throws IllegalAccessException
* @throws NoSuchFieldException
*/
private void setObjectValueIfNotNull(T t, Object obj) throws IllegalAccessException, NoSuchFieldException {
Field[] fields = obj.getClass().getDeclaredFields();
Class<?> cls = t.getClass();
for (Field field : fields) {
field.setAccessible(true);
if (field.getAnnotation(DatabaseField.class) == null || "id".equals(field.getName())) {
continue;
}
Object valueObj = null;
try {
valueObj = field.get(obj);
} catch (IllegalAccessException e) {
LogUtils.e(e);
}
if (valueObj != null) {
Field f = cls.getDeclaredField(field.getName());
if (f != null) {
f.setAccessible(true);
f.set(t, valueObj);
} else {
LogUtils.e("no this field:" + field.getName());
}
}
}
}
示例13: classHasAnnotations
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
private static boolean classHasAnnotations(Class<?> paramClass)
{
while (paramClass != null)
{
if (paramClass.getAnnotation(DatabaseTable.class) != null)
return true;
Field[] arrayOfField;
try
{
arrayOfField = paramClass.getDeclaredFields();
}
catch (Throwable localThrowable1)
{
System.err.println("Could not load get delcared fields from: " + paramClass);
System.err.println(" " + localThrowable1);
return false;
}
int i = arrayOfField.length;
for (int j = 0; j < i; j++)
{
Field localField = arrayOfField[j];
if ((localField.getAnnotation(DatabaseField.class) != null) || (localField.getAnnotation(ForeignCollectionField.class) != null))
return true;
}
try
{
paramClass = paramClass.getSuperclass();
}
catch (Throwable localThrowable2)
{
System.err.println("Could not get super class for: " + paramClass);
System.err.println(" " + localThrowable2);
return false;
}
}
return false;
}
示例14: getColumnName
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
private String getColumnName(Field field) {
DatabaseField dbField = field.getAnnotation(DatabaseField.class);
if (dbField == null)
throw new IllegalArgumentException();
if (dbField.columnName() == null)
return field.getName();
return dbField.columnName();
}
示例15: configFromField
import com.j256.ormlite.field.DatabaseField; //导入依赖的package包/类
/**
* 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;
}
}