Java Code Examples for com.badlogic.gdx.utils.reflect.ClassReflection#isAssignableFrom()

The following examples show how to use com.badlogic.gdx.utils.reflect.ClassReflection#isAssignableFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonProcessor.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
static <R> R process(Class<?> wantedType, String jsonString) {
    Json json = new Json();
    json.setIgnoreUnknownFields(true);
    json.setTypeName(null);
    R result = null;
    if (ClassReflection.isAssignableFrom(List.class, wantedType)
            || ClassReflection.isAssignableFrom(Map.class, wantedType)) {
        if (wantedType == List.class) {
            wantedType = ArrayList.class;
        } else if (wantedType == Map.class) {
            wantedType = HashMap.class;
        }
        json.setDefaultSerializer(new JsonListMapDeserializer(HashMap.class));
        result = (R) json.fromJson(wantedType, jsonString);
    } else {
        result = (R) json.fromJson(wantedType, jsonString);
    }
    return result;
}
 
Example 2
Source File: ResolverDataSnapshotList.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static List resolve(DataSnapshot dataSnapshot) {
    if (dataSnapshot.getValue() == null) {
        throw new IllegalStateException();
    }
    List result = new ArrayList<>();
    Iterable<DataSnapshot> dataSnapshots;
    if (ClassReflection.isAssignableFrom(Map.class, dataSnapshot.getValue().getClass())) {
        dataSnapshots = ((Map) dataSnapshot.getValue()).values();
    } else {
        dataSnapshots = dataSnapshot.getChildren();
    }
    for (Object o : dataSnapshots) {
        if (o instanceof DataSnapshot) {
            result.add(((DataSnapshot) o).getValue());
        } else {
            result.add(o);
        }
    }
    return result;
}
 
Example 3
Source File: DefaultTypeRecognizer.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
public static Object getDefaultValue(Class type) {
    if (isFloatingPointNumberType(type)) {
        return 0.;
    } else if (isLongNumberType(type)) {
        return 0L;
    } else if (isBooleanType(type)) {
        return false;
    } else if (isStringType(type)) {
        return "";
    } else if (isCharType(type)) {
        return '\u0000';
    } else if (ClassReflection.isAssignableFrom(Map.class, type)) {
        return new HashMap<>();
    } else if (ClassReflection.isAssignableFrom(List.class, type)) {
        return new ArrayList<>();
    } else {
        try {
            return ClassReflection.newInstance(type);
        } catch (ReflectionException e) {
            GdxFIRLogger.error(CANT_FIND_DEFAULT_VALUE_FOR_GIVEN_TYPE);
            return null;
        }
    }
}
 
Example 4
Source File: MapMitmConverter.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
Object doMitmConversion(Class<?> wantedType, Object data) {
    if (data == null) return null;
    // If client tell us to do map conversion and is not possible - throw exception.
    if (!isConversionPossible(data))
        throw new MapConversionNotPossibleException(CANT_DO_MAP_CONVERSION_FROM_TYPE + data.getClass().getName());
    // Check if Map is covered by data in some way, otherwise throw exception.
    if (ClassReflection.isAssignableFrom(Map.class, data.getClass())) {
        // First case - Map, do simple conversion.
        data = mapConverter.convert((Map) data, wantedType);
    } else if (ClassReflection.isAssignableFrom(List.class, data.getClass())) {
        // Second case - List, go through all elements and convert it to map.
        for (int i = ((List) data).size() - 1; i >= 0; i--) {
            Object element = ((List) data).get(i);
            if (element == null || !ClassReflection.isAssignableFrom(Map.class, element.getClass())) {
                GdxFIRLogger.log("@MapConversion: One of list value are not a Map - value was dropped. Element type: " + (element != null ? element.getClass() : "null"));
                ((List) data).remove(i);
            } else {
                ((List) data).set(i, mapConverter.convert((Map) element, wantedType));
            }
        }
    }
    return data;
}
 
Example 5
Source File: ResolverFIRDataSnapshotList.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static List resolve(FIRDataSnapshot dataSnapshot) {
    List result = new ArrayList<>();
    if (dataSnapshot.value() == null) {
        throw new IllegalStateException();
    }
    NSArray nsArray;
    if (ClassReflection.isAssignableFrom(NSArray.class, dataSnapshot.value().getClass())) {
        nsArray = dataSnapshot.children().allObjects();
    } else if (ClassReflection.isAssignableFrom(NSDictionary.class, dataSnapshot.value().getClass())) {
        nsArray = ((NSDictionary) dataSnapshot.value()).allValues();
    } else {
        throw new IllegalStateException();
    }
    for (Object object : nsArray) {
        if (object instanceof FIRDataSnapshot) {
            result.add(DataProcessor.iosDataToJava(((FIRDataSnapshot) object).value()));
        } else {
            result.add(DataProcessor.iosDataToJava(object));
        }
    }
    return result;
}
 
Example 6
Source File: DataSnapshotResolver.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
void resolve(DataSnapshot dataSnapshot) {
    if (ClassReflection.isAssignableFrom(List.class, dataType) && dataSnapshot.getValue() == null) {
        promise.doComplete(Collections.emptyList());
    } else if (ClassReflection.isAssignableFrom(List.class, dataType)
            && ResolverDataSnapshotList.shouldResolveOrderBy(dataType, dataSnapshot)) {
        promise.doComplete(ResolverDataSnapshotList.resolve(dataSnapshot));
    } else {
        promise.doComplete(dataSnapshot.getValue());
    }
}
 
Example 7
Source File: RunTransactionValidator.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
@Override
public void validate(Array<Object> arguments) {
    if (arguments.size < 2)
        throw new IllegalArgumentException(MESSAGE1);
    if (!(arguments.get(0) instanceof Class))
        throw new IllegalArgumentException(MESSAGE2);
    if (!(arguments.get(1) instanceof Function))
        throw new IllegalArgumentException(MESSAGE3);
    if (ClassReflection.isAssignableFrom(Number.class, (Class) arguments.get(0))
            && arguments.get(0) != Double.class
            && arguments.get(0) != Long.class
    ) {
        throw new IllegalArgumentException(NUMBER_TYPE_SHOULD_BE_LONG_OR_DOUBLE);
    }
}
 
Example 8
Source File: FilteringStateEnsurer.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
/**
 * Throws IllegalStateException when filter was applied but type of wanted data is not a list.
 *
 * @param wantedType The type of requested data
 */
public static void checkFilteringState(Array<Filter> filters, OrderByClause orderByClause, Class<?> wantedType) {
    if ((filters.size > 0 || orderByClause != null)
            && (!ClassReflection.isAssignableFrom(List.class, wantedType)
            && !ClassReflection.isAssignableFrom(Map.class, wantedType)))
        throw new IllegalStateException(FILTER_SHOULD_BE_APPLIED_FOR_LIST_OR_MAP);
}
 
Example 9
Source File: ConverterPromise.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public synchronized void doComplete(Object object) {
    try {
        if (canceled) return;
        if (mapConverter == null)
            throw new IllegalStateException();
        if (modifier != null) {
            object = modifier.apply((T) object);
        }
        if (object != null) {
            if (!ClassReflection.isAssignableFrom(List.class, wantedDataType)) {
                if (mapConverter.isPojo(wantedDataType)) {
                    object = mapConverter.doMitmConversion(wantedDataType, object);
                }
            } else {
                if (thenConsumer.isSet()) {
                    MapConversion mapConversionAnnotation = AnnotationFinder.getMethodAnnotation(MapConversion.class, thenConsumer.first());
                    if (mapConversionAnnotation != null) {
                        object = mapConverter.doMitmConversion(mapConversionAnnotation.value(), object);
                        if (object.getClass() == mapConversionAnnotation.value()) {
                            object = Collections.singletonList(object);
                        }
                    } else if (ClassReflection.isAssignableFrom(Map.class, object.getClass())) {
                        object = Collections.singletonList(object);
                    }
                }
            }
        }
        if (object != null &&
                DefaultTypeRecognizer.isLongNumberType(object.getClass()) &&
                DefaultTypeRecognizer.isFloatingPointNumberType(wantedDataType)
        ) {
            object = Double.valueOf(Long.valueOf(object.toString()));
        }
        super.doComplete((R) object);
    } catch (Exception e) {
        doFail(e);
    }
}
 
Example 10
Source File: SnapshotProcessorBlock.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void process(FIRDataSnapshot arg0, String arg1) {
    if (arg0.value() == null) {
        promise.doFail(new Exception(GIVEN_DATABASE_PATH_RETURNED_NULL_VALUE));
    } else {
        Object data = arg0.value();
        try {
            if (ClassReflection.isAssignableFrom(List.class, wantedType)
                    && ResolverFIRDataSnapshotList.shouldResolveList(arg0)) {
                GdxFIRLogger.log("Process list for " +
                        (data != null ? data.getClass() : "") + "" + data);
                data = ResolverFIRDataSnapshotList.resolve(arg0);
            } else {
                GdxFIRLogger.log("Process data for " +
                        (data != null ? data.getClass() : "") + "" + data);
                data = DataProcessor.iosDataToJava(data, wantedType);
            }
            GdxFIRLogger.log("Processed data: " + data);
        } catch (Exception e) {
            promise.doFail(e);
            return;
        }
        if (data == null && ClassReflection.isAssignableFrom(List.class, wantedType)) {
            data = Collections.emptyList();
        }
        promise.doComplete(data);
    }
}
 
Example 11
Source File: ResolverFIRDataSnapshotList.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
static boolean shouldResolveList(FIRDataSnapshot dataSnapshot) {
    GdxFIRLogger.log("Should resolve list, "
            + "children: " + dataSnapshot.childrenCount());
    return (ClassReflection.isAssignableFrom(NSArray.class, dataSnapshot.value().getClass())
            || ClassReflection.isAssignableFrom(NSDictionary.class, dataSnapshot.value().getClass()))
            && dataSnapshot.childrenCount() > 0;
}
 
Example 12
Source File: ResolverDataSnapshotList.java    From gdx-fireapp with Apache License 2.0 4 votes vote down vote up
static boolean shouldResolveOrderBy(Class<?> dataType, DataSnapshot dataSnapshot) {
    return (ClassReflection.isAssignableFrom(List.class, dataType) || ClassReflection.isAssignableFrom(Map.class, dataType))
            && dataSnapshot.getChildrenCount() > 0;
}
 
Example 13
Source File: MapMitmConverter.java    From gdx-fireapp with Apache License 2.0 4 votes vote down vote up
boolean isConversionPossible(Object data) {
    return ClassReflection.isAssignableFrom(Map.class, data.getClass()) || ClassReflection.isAssignableFrom(List.class, data.getClass());
}
 
Example 14
Source File: MapMitmConverter.java    From gdx-fireapp with Apache License 2.0 4 votes vote down vote up
boolean isPojo(Class<?> type) {
    return !JavaCoreClassDetector.isJavaCoreClass(type)
            && !ClassReflection.isAssignableFrom(List.class, type)
            && !ClassReflection.isAssignableFrom(Map.class, type);
}
 
Example 15
Source File: GDXDialogsSystem.java    From gdx-dialogs with Apache License 2.0 4 votes vote down vote up
private void installAndroidGDXDialogs() {

		if (Gdx.app.getType() != ApplicationType.Android) {
			showDebugSkipInstall(ApplicationType.Android.name());
			return;
		}

		if (Gdx.app.getType() == ApplicationType.Android) {
			try {

				Class<?> activityClazz = ClassReflection.forName("android.app.Activity");

				Class<?> dialogManagerClazz = ClassReflection.forName("de.tomgrill.gdxdialogs.android.AndroidGDXDialogs");

				Object activity = null;

				if (ClassReflection.isAssignableFrom(activityClazz, gdxAppObject.getClass())) {

					activity = gdxAppObject;
				} else {

					Class<?> supportFragmentClass = findClass("android.support.v4.app.Fragment");
					// {
					if (supportFragmentClass != null && ClassReflection.isAssignableFrom(supportFragmentClass, gdxAppObject.getClass())) {

						activity = ClassReflection.getMethod(supportFragmentClass, "getActivity").invoke(gdxAppObject);
					} else {
						Class<?> fragmentClass = findClass("android.app.Fragment");
						if (fragmentClass != null && ClassReflection.isAssignableFrom(fragmentClass, gdxAppObject.getClass())) {
							activity = ClassReflection.getMethod(fragmentClass, "getActivity").invoke(gdxAppObject);
						}
					}

				}

				if (activity == null) {
					throw new RuntimeException("Can't find your gdx activity to instantiate gdx-dialogs. " + "Looks like you have implemented AndroidApplication without using "
							+ "Activity or Fragment classes or Activity is not available at the moment");
				}
				Object dialogManager = ClassReflection.getConstructor(dialogManagerClazz, activityClazz).newInstance(activity);

				this.gdxDialogs = (GDXDialogs) dialogManager;
				showDebugInstallSuccessful(ApplicationType.Android.name());

			} catch (Exception e) {
				showErrorInstall(ApplicationType.Android.name(), "android");
				e.printStackTrace();
			}
		}
	}
 
Example 16
Source File: GdxReflection.java    From Cardshifter with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isEnum(Class<?> aClass) {
    return ClassReflection.isAssignableFrom(Enum.class, aClass);
}