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

nkist88.object2source.0.0.1.source-code.common-methods.txt Maven / Gradle / Ivy

Go to download

A library for generating the source code that creates an instance of the object that is submitted to the input.

There is a newer version: 0.0.10
Show newest version
-----------------
getCalendarInstance(, )
-----------------
public static java.util.GregorianCalendar getCalendarInstance(String tzId, long mTime) {
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTimeZone(java.util.TimeZone.getTimeZone(tzId));
cal.setTimeInMillis(mTime);
return (java.util.GregorianCalendar) cal;
}
-----------------
getClassWithMethodsHierarchy()
-----------------
public static java.util.List getClassWithMethodsHierarchy(Class clazz){
java.util.List classesWithParent = new java.util.ArrayList<>();
Class currentClass = clazz;
while (currentClass.getDeclaredFields().length > 0) {
classesWithParent.add(currentClass);
currentClass = currentClass.getSuperclass();
}
return classesWithParent;
}
-----------------
getAllFieldsOfObject()
-----------------
public static java.util.List getAllFieldsOfObject(Object obj) {
java.util.List allFields = new java.util.ArrayList<>();
for(Class c : getClassWithMethodsHierarchy(obj.getClass())) {
allFields.addAll(java.util.Arrays.asList(c.getDeclaredFields()));
}
return allFields;
}
-----------------
notPublicAssignment(, , )
-----------------
public static void notPublicAssignment(Object obj, String fieldName, Object value) throws IllegalAccessException {
for(java.lang.reflect.Field f : getAllFieldsOfObject(obj)) {
if(f.getName().equals(fieldName)) {
boolean currentAccessible = f.isAccessible();
f.setAccessible(true);
f.set(obj, value);
f.setAccessible(currentAccessible);
}
}
}
-----------------
newInstanceHard()
-----------------
public static Object newInstanceHard(Class type) {
try {
java.lang.reflect.Constructor mungedConstructor = newConstructorForSerialization(type, getJavaLangObjectConstructor());
mungedConstructor.setAccessible(true);
return mungedConstructor.newInstance((Object[]) null);
} catch(Exception e) {
throw new IllegalStateException(e);
}
}
-----------------
getJavaLangObjectConstructor()
-----------------
public static java.lang.reflect.Constructor getJavaLangObjectConstructor() {
try {
return Object.class.getConstructor((Class[]) null);
} catch(NoSuchMethodException e) {
throw new IllegalStateException(e);
}
}
-----------------
getJavaLangObjectConstructor(, )
-----------------
public static java.lang.reflect.Constructor newConstructorForSerialization(Class type, java.lang.reflect.Constructor constructor) {
Class reflectionFactoryClass = getReflectionFactoryClass();
Object reflectionFactory = createReflectionFactory(reflectionFactoryClass);
java.lang.reflect.Method newConstructorForSerializationMethod = getNewConstructorForSerializationMethod(reflectionFactoryClass);
try {
return (java.lang.reflect.Constructor) newConstructorForSerializationMethod.invoke(reflectionFactory, type, constructor);
} catch(Exception e) {
throw new IllegalStateException(e);
}
}
-----------------
getReflectionFactoryClass()
-----------------
public static Class getReflectionFactoryClass() {
try {
return Class.forName("sun.reflect.ReflectionFactory");
} catch(ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
-----------------
createReflectionFactory()
-----------------
public static Object createReflectionFactory(Class reflectionFactoryClass) {
try {
java.lang.reflect.Method method = reflectionFactoryClass.getDeclaredMethod("getReflectionFactory");
return method.invoke(null);
} catch(Exception e) {
throw new IllegalStateException(e);
}
}
-----------------
getNewConstructorForSerializationMethod()
-----------------
public static java.lang.reflect.Method getNewConstructorForSerializationMethod(Class reflectionFactoryClass) {
try {
return reflectionFactoryClass.getDeclaredMethod("newConstructorForSerialization", Class.class, java.lang.reflect.Constructor.class);
} catch(NoSuchMethodException e) {
throw new IllegalStateException(e);
}
}
-----------------
callConstructorReflection()
-----------------
public static Object callConstructorReflection(Class type) throws IllegalAccessException, java.lang.reflect.InvocationTargetException, InstantiationException {
java.lang.reflect.Constructor constructor = null;
for (java.lang.reflect.Constructor c : type.getDeclaredConstructors()) {
if (c.getParameterTypes().length == 0) {
constructor = c;
break;
}
}
if (constructor != null) {
constructor.setAccessible(true);
return constructor.newInstance((Object[]) null);
} else {
return null;
}
}
-----------------
createInstance()
-----------------
public static  T createInstance(Class clazz) {
try {
return (T) newInstanceHard(Class.forName(clazz.getName()));
} catch (ClassNotFoundException cnf) {
throw new IllegalStateException(cnf);
}
}
-----------------