All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.g2forge.alexandria.reflection.object.HReflection Maven / Gradle / Ivy

Go to download

Generic type safe reflection library, which adds significant usability over the standard Java runtime.

There is a newer version: 0.0.18
Show newest version
package com.g2forge.alexandria.reflection.object;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;

import com.g2forge.alexandria.adt.collection.strategy.ICollectionStrategy;
import com.g2forge.alexandria.adt.collection.strategy.implementations.CollectionStrategy;
import com.g2forge.alexandria.generic.type.environment.ITypeEnvironment;
import com.g2forge.alexandria.generic.type.environment.implementations.EmptyTypeEnvironment;
import com.g2forge.alexandria.generic.type.java.IJavaUntype;
import com.g2forge.alexandria.generic.type.java.type.implementations.JavaBoundType;
import com.g2forge.alexandria.java.core.marker.Helpers;
import com.g2forge.alexandria.reflection.object.implementations.JavaConcreteReflection;
import com.g2forge.alexandria.reflection.object.implementations.JavaTypeReflection;
import com.g2forge.alexandria.reflection.typed.IReflectionGenericTyped;

import lombok.experimental.UtilityClass;

@Helpers
@UtilityClass
public class HReflection extends com.g2forge.alexandria.java.reflect.HReflection {
	@SuppressWarnings("unchecked")
	public static , T> ICollectionStrategy create(IReflectionGenericTyped typed) {
		if (Collection.class.equals(typed.getType().erase().getType().getJavaType())) { return (ICollectionStrategy) new CollectionStrategy(); }
		throw new IllegalArgumentException("Collection type \"" + typed.getType() + "\" is not recognized, so we can't help you here (sorry)!");
	}

	public static  IJavaConcreteReflection toReflection(final Class type) {
		return toReflection(type, EmptyTypeEnvironment.create());
	}

	public static  IJavaConcreteReflection toReflection(final Class type, final ITypeEnvironment environment) {
		return new JavaConcreteReflection<>(type, environment);
	}

	public static  IJavaTypeReflection toReflection(IJavaUntype type) {
		return toReflection(type.getJavaType(), EmptyTypeEnvironment.create());
	}

	@SuppressWarnings("unchecked")
	public static  IJavaConcreteReflection toReflection(T value) {
		return (IJavaConcreteReflection) toReflection(value.getClass(), EmptyTypeEnvironment.create());
	}

	public static  IJavaTypeReflection toReflection(final Type type) {
		return toReflection(type, EmptyTypeEnvironment.create());
	}

	public static  IJavaTypeReflection toReflection(final Type type, final ITypeEnvironment environment) {
		if (type == null) return null;
		if (type instanceof Class) {
			@SuppressWarnings("unchecked")
			final Class klass = (Class) type;
			return toReflection(klass, environment);
		}
		if (type instanceof ParameterizedType) {
			final ParameterizedType parameterized = (ParameterizedType) type;
			return new JavaConcreteReflection<>(new JavaBoundType(parameterized, environment));
		}
		return new JavaTypeReflection(type, environment);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy