package com.oneliang.frame.reflect;

import java.lang.reflect.Modifier;

public class ReflectClass<C> extends ThrowableChain {

	private Class<C> clazz = null;

	/**
	 * constructor
	 * 
	 * @param clazz
	 */
	public ReflectClass(Class<C> clazz) {
		this.clazz = clazz;
	}

	/**
	 * constructor
	 * 
	 * @param className
	 */
	@SuppressWarnings("unchecked")
	public ReflectClass(String className) {
		try {
			this.clazz = (Class<C>) Class.forName(className);
		} catch (ClassNotFoundException e) {
			this.addThrowable(e);
		}
	}

	/**
	 * get static field
	 * 
	 * @param <FieldClass>
	 * @param fieldName
	 * @return ReflectField<C,FieldClass>
	 */
	public <FieldClass> ReflectField<C, FieldClass> getStaticField(String fieldName) {
		return this.getField(fieldName, Modifier.STATIC);
	}

	/**
	 * get declared field
	 * 
	 * @param <FieldClass>
	 * @param fieldName
	 * @return ReflectField<C,FieldClass>
	 */
	public <FieldClass> ReflectField<C, FieldClass> getDeclaredField(String fieldName) {
		return this.getField(fieldName, 0);
	}

	/**
	 * get field
	 * 
	 * @param <FieldClass>
	 * @param fieldName
	 * @return ReflectField<C,FieldClass>
	 */
	private <FieldClass> ReflectField<C, FieldClass> getField(String fieldName, int modifier) {
		return new ReflectField<C, FieldClass>(this.clazz, fieldName, modifier);
	}

	/**
	 * get static method
	 * 
	 * @param <ReturnType>
	 * @param methodName
	 * @param methodParameterType
	 * @return ReflectMethod<C,ReturnType>
	 */
	public <ReturnType> ReflectMethod<C, ReturnType> getStaticMethod(String methodName, Class<?>[] methodParameterType) {
		return this.getMethod(methodName, methodParameterType, Modifier.STATIC);
	}

	/**
	 * get declared method
	 * 
	 * @param <ReturnType>
	 * @param methodName
	 * @param methodParameterType
	 * @return ReflectMethod<C,ReturnType>
	 */
	public <ReturnType> ReflectMethod<C, ReturnType> getDeclaredMethod(String methodName, Class<?>[] methodParameterType) {
		return this.getMethod(methodName, methodParameterType, 0);
	}

	/**
	 * get method
	 * 
	 * @param <ReturnType>
	 * @param methodName
	 * @param methodParameterType
	 * @param modifier
	 * @return ReflectMethod<C,ReturnType>
	 */
	private <ReturnType> ReflectMethod<C, ReturnType> getMethod(String methodName, Class<?>[] methodParameterType, int modifier) {
		return new ReflectMethod<C, ReturnType>(this.clazz, methodName, methodParameterType, modifier);
	}

	/**
	 * @return the clazz
	 */
	public Class<C> getClazz() {
		return clazz;
	}
}

@@ -565,37 +565,36 @@
  * @param %3CField
-Class
+Type
 %3E%0A%09 * @param fie
@@ -624,37 +624,36 @@
 ectField%3CC,Field
-Class
+Type
 %3E%0A%09 */%0A%09public %3C
@@ -649,37 +649,36 @@
 /%0A%09public %3CField
-Class
+Type
 %3E ReflectField%3CC
@@ -680,29 +680,28 @@
 eld%3CC, Field
-Class
+Type
 %3E getStaticF
@@ -823,37 +823,36 @@
  * @param %3CField
-Class
+Type
 %3E%0A%09 * @param fie
@@ -882,37 +882,36 @@
 ectField%3CC,Field
-Class
+Type
 %3E%0A%09 */%0A%09public %3C
@@ -911,29 +911,28 @@
 ublic %3CField
-Class
+Type
 %3E ReflectFie
@@ -938,29 +938,28 @@
 eld%3CC, Field
-Class
+Type
 %3E getDeclare
@@ -1064,29 +1064,28 @@
 param %3CField
-Class
+Type
 %3E%0A%09 * @param
@@ -1127,21 +1127,20 @@
 %3CC,Field
-Class
+Type
 %3E%0A%09 */%0A%09
@@ -1153,21 +1153,20 @@
 e %3CField
-Class
+Type
 %3E Reflec
@@ -1180,21 +1180,20 @@
 C, Field
-Class
+Type
 %3E getFie
@@ -1263,21 +1263,20 @@
 C, Field
-Class
+Type
 %3E(this.c

optimize some code