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

com.github.andyshao.util.Reflects Maven / Gradle / Ivy

There is a newer version: 4.0
Show newest version
package com.github.andyshao.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Set;

/**
 * 
 * Title:the tool of java.lang.reflect
* Descript:
* With a easy way to use java.lang.reflect.*; For this class could avoid some * exception which is * the subclass of {@link Exception}.
* Convert the {@link Exception} or the subclass of {@link Exception} to * {@link RuntimeException}. * For example:
*
* *
 * try {
 *     return (Class<T>) Class.forName(className);
 * } catch (ClassNotFoundException e) {
 *     throw new RuntimeException(e);
 * }
 * 
* *

* Any necessary about take the original exception, you could use this way:
*
* *
 * try {
 *     Class<String> clazz = Reflects.forName("java.lang.String");
 * } catch (RuntimeException e) {
 *     Throwable throwable = e.getCause();
 *     if (throwable instanceof ClassNotFoundException) {
 *         // Do something
 *     }
 *     // For other things:
 *     throw e;
 * }
 * 
* *
*

* Copyright: Copryright(c) Mar 7, 2014
* Encoding:UNIX UTF-8 * * @author Andy.Shao * */ public final class Reflects { /** * * @param className the name of class * @param the type of class * @return the type of class */ @SuppressWarnings("unchecked") public static Class forName(String className) { try { return (Class) Class.forName(className); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } /** * * @param clazz the type of class * @param parameterTypes the type of parameters of constructor of class * @param the type of class * @return the constructor of class */ public static Constructor getConstructor(Class clazz , Class... parameterTypes) { try { return clazz.getConstructor(parameterTypes); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } } /** * * @param clazz the type of class * @param parameterTypes the typ of parameters of constructor of class * @param the type of class * @return the constructor of class */ public static Constructor getDeclaredConstructor(Class clazz , Class... parameterTypes) { try { return clazz.getDeclaredConstructor(parameterTypes); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } } /** * if the clazz doesn't include the mtheod, it will check the super class. * * @param clazz the type of class * @param field_name the name of field * @return the field of class */ public static Field superGetDeclaredField(Class clazz , String field_name) { try { return clazz.getDeclaredField(field_name); } catch (NoSuchFieldException e) { if (clazz.getSuperclass() != null) { return Reflects.superGetDeclaredField(clazz.getSuperclass() , field_name); } throw new RuntimeException(e); } catch (SecurityException e) { throw new RuntimeException(e); } } /** * * @param clazz the type of class * @param field_name the name of field * @return the field of class */ public static Field getDeclaredField(Class clazz , String field_name){ try { return clazz.getDeclaredField(field_name); } catch (NoSuchFieldException | SecurityException e) { throw new RuntimeException(e); } } /** * * @param clazz the type of class * @return the fields of class */ public static Field[] superGetDeclaredFields(Class clazz){ Field[] result = new Field[0]; if(clazz.getSuperclass() != null){ Field[] fields = superGetDeclaredFields(clazz.getSuperclass()); result = ArrayTools.mergeArray(Field[].class, result, fields); } Field[] fields = clazz.getDeclaredFields(); result = ArrayTools.mergeArray(Field[].class, result, fields); return result; } /** * if the clazz doesn't include the mtheod, it will check the super class. * * @param clazz the type of class * @param method_name the name of method * @param parameterTypes the type of parameters * @return the method of class */ public static Method superGetDeclaredMethod(Class clazz , String method_name , Class... parameterTypes) { try { return clazz.getDeclaredMethod(method_name , parameterTypes); } catch (NoSuchMethodException e) { if (clazz.getSuperclass() != null) { return Reflects.superGetDeclaredMethod(clazz.getSuperclass() , method_name , parameterTypes); } throw new RuntimeException(e); } catch (SecurityException e) { throw new RuntimeException(e); } } /** * * @param clazz the type of class * @param method_name the name of method * @param parameterTypes the type of parameters * @return the method of class */ public static Method getDeclaredMethod(Class clazz , String method_name , Class... parameterTypes){ try { return clazz.getDeclaredMethod(method_name , parameterTypes); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } } /** * * @param clazz the type of class * @param field_name the name of field * @return the fields of class */ public static Field getField(Class clazz , String field_name) { try { return clazz.getField(field_name); } catch (NoSuchFieldException | SecurityException e) { throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public static T getFieldValue(Object target , Field field) { try { return (T) field.get(target); } catch (IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException(e); } } public static void getInterfaces(Class clazz , Set> set) { set.addAll(Arrays.asList(clazz.getInterfaces())); if (clazz.getSuperclass() != null) { Reflects.getInterfaces(clazz.getSuperclass() , set); } } public static Method getMethod(Class clazz , String method_name , Class... parameterTypes) { try { return clazz.getMethod(method_name , parameterTypes); } catch (NoSuchMethodException | SecurityException e) { throw new RuntimeException(e); } } @SuppressWarnings("unchecked") public static T invoked(Object target , Method method , Object... values) { try { return (T) method.invoke(target , values); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } } public static T newInstance(Constructor constructor , Object... values) { try { return constructor.newInstance(values); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } } public static void setFieldValue(Object target , Field field , Object value) { try { field.set(target , value); } catch (IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException(e); } } public static T superGetAnnotation(Class target , Class clazz) { T annotation = target.getAnnotation(clazz); if (annotation != null) { return annotation; } if (target.isInterface()) { Class[] interfaces = target.getInterfaces(); for (Class _interface : interfaces) { annotation = Reflects.superGetAnnotation(_interface , clazz); if (annotation != null) { return annotation; } } } else { Class superClass = target.getSuperclass(); if (superClass != null) { annotation = Reflects.superGetAnnotation(superClass , clazz); if (annotation != null) { return annotation; } } Class[] interfaces = target.getInterfaces(); for (Class _interface : interfaces) { annotation = Reflects.superGetAnnotation(_interface , clazz); if (annotation != null) { return annotation; } } } return annotation; } private Reflects() { throw new AssertionError("No support instance " + Reflects.class.getName()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy