- 浏览: 332442 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (212)
- spring (21)
- design pattern(java) (12)
- linux-shell (28)
- java-thread (20)
- java-collection (6)
- java-reflect (9)
- mysql (11)
- java-io (7)
- java-util&lang&io (3)
- algorithm (3)
- interview (2)
- tools-eclipse (2)
- tools-maven (1)
- web-script (1)
- java组建 (13)
- 博客收藏 (1)
- 架构设计与实践 (10)
- active-mq (6)
- java-jvm&性能&原理 (27)
- tomcat (2)
- flume (1)
- serialization (2)
- git (1)
- cache&redis (8)
- guava (1)
- zookeeper (3)
- socket&tcp&udp&http (6)
- test (1)
最新评论
-
bbls:
有用有用有用
java-jvm-jstack-(监视器和锁的概念) -
王新春:
小侠有点帅哦 写道此流怎么关闭新春这个实现 可以不关闭的,哈哈 ...
源码剖析之java.io.ByteArrayOutputStream -
小侠有点帅哦:
此流怎么关闭新春
源码剖析之java.io.ByteArrayOutputStream -
cumt168:
写的很好为什么初始化参数,年轻代-Xmn10M def new ...
jvm之内存申请过程分析 -
ronin47:
应该是跟共享域名思路差不多,根据cookie的key作判断
跨域:一种通过服务端解决跨域的实现
java提供了对运行时class文件信息获取功能的支持,对编写通用组件和底层框架有着重大意义。
Jdk提供此功能的支持类:java.lang.Class
知识提醒:
1、每个Class对象代表了一个class文件信息。所以一个类加载器里只会有一个Class对象。
2、Class对象本身也是Object对象。
3、这个类算是java提供反射功能的一部分,虽然没有在java.lang.reflect包里。
下面这个例子,算是对此类各个方法功能的一个探索和测试,希望对大家有用~
Jdk提供此功能的支持类:java.lang.Class
知识提醒:
1、每个Class对象代表了一个class文件信息。所以一个类加载器里只会有一个Class对象。
2、Class对象本身也是Object对象。
3、这个类算是java提供反射功能的一部分,虽然没有在java.lang.reflect包里。
下面这个例子,算是对此类各个方法功能的一个探索和测试,希望对大家有用~
package com.horizon.reflect; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.sql.SQLException; import java.util.Arrays; import java.util.List; @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,ElementType.TYPE,ElementType.CONSTRUCTOR }) @interface Note { public boolean require() default true; public String note() default ""; } @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,ElementType.TYPE,ElementType.CONSTRUCTOR }) @interface Type { public boolean require() default true; public String type() default ""; } @Inherited //标记这个注解是否可以被其注解的接口的子类获取 @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,ElementType.TYPE,ElementType.CONSTRUCTOR }) @interface Face { public boolean require() default true; public String face() default ""; } interface IModel<T>{ @Face(face="method->face") public boolean loginUser(String username,String password) throws RuntimeException,SQLException; } @Face abstract class AbastractUser{ public String rowid; protected Integer Id; } @Note(note= "calss->B",require = false) public class LoginModel<T> extends AbastractUser implements IModel<T>{ /** * */ private static final long serialVersionUID = -2537216038308998595L; @Note(note="field->note") @Type(type="field->type") public List<String> note = Arrays.asList("defaultValue"); private String username; private String password; public LoginModel(){ } public LoginModel(String username,String password,Integer id){ } @Note(note="CONSTRUCTOR->B") public LoginModel(@Note(note = "p->note") @Type(type = "p->type") String username,String password){ this.username = username; this.password = password; } @Note(note = "method ->login()",require = true) public void login(@Note(note = "p->username") String username, @Note(note = "p->password")String password) { } @Note(note = "method ->login()",require = true) @Type(type = "p->type->username",require = false) public boolean loginUser(@Note(note = "p->note->username") @Type(type = "p->type->username") String username, @Type(type = "p->password")String password) throws RuntimeException,SQLException{ return true; } public <N,P> N getUserInfo(){ return null; } public List<String> getNote() { return note; } public void setNote(List<String> note) { this.note = note; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.horizon.reflect; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import org.junit.Test; /** * @author create by [url=xinchunwang@aliyun.com]新春.王[/url] <br> * date :Apr 20, 2013 10:34:04 AM * * Class反射功能测试 * 1、继承关系相关的信息和转化功能 * 2、成员、方法、构造方法 相关的功能 * 3、Class 基本属性相关的功能。eg:类型(接口,数组,枚举) * 4、资源相关的功能 * 5、注解相关的功能 * 6、类加载相关的功能 */ public class ClassReflect { private Class<?> targetClass = LoginModel.class; public static void main(String[] args) { } /** * 与资源相关,从当前class的相对路径或者绝对路径找 targetClass.getResourceAsStream(name) */ @Test public void testgetResource(){ //URL url = targetClass.getResource("..\\nio\\src.txt"); URL url = targetClass.getResource(""); System.out.println(url); try { InputStream input = url.openStream(); BufferedInputStream bis = new BufferedInputStream(input); byte[] b = new byte[1024]; while(bis.read(b) != -1){ System.out.println(new String(b)); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ } } /** * 强制转化类型 */ @Test public void testCast(){ Class<?> superClass = targetClass.getSuperclass(); LoginModel model = new LoginModel(); AbastractUser obj = (AbastractUser)superClass.cast(model); System.out.println(obj == model); } /** * 获取父类 */ @Test public void testgetSuperclass(){ Class<?> superClass = targetClass.getSuperclass(); System.out.println(superClass); System.out.println(superClass.getSuperclass()); } /** * 测试 实例 和实例Class之间的关系 功能类似于 instanceof operator */ @Test public void testIsInstance(){ LoginModel model = new LoginModel(); System.out.println(targetClass.isInstance(model)); //true System.out.println(IModel.class.isInstance(model));//true System.out.println(model instanceof IModel); System.out.println((Integer)1 instanceof Integer); } /** * 测试一个接口是否是接口、数组、枚举、注解、基本类型、 */ @Test public void testIsType() { System.out.println(targetClass.isInterface()); //false Class<Serializable> cs = Serializable.class; System.out.println("cs.isInterface():"+cs.isInterface()); //true Class<?> abstractClass = AbastractUser.class; System.out.println("Type.class.isAnnotation():"+Type.class.isAnnotation()); System.out.println("Integer.class.isPrimitive():"+Integer.class.isPrimitive()); System.out.println("int.class.isPrimitive():"+int.class.isPrimitive()); int[] intArr = new int[100]; System.out.println("intArr.getClass().isArray():"+intArr.getClass().isArray()); System.out.println("Integer.TYPE.isPrimitive():"+Integer.TYPE.isPrimitive()); System.out.println("int.class == Integer.TYPE:"+(int.class == Integer.TYPE)); } /** * LoginModel.class 生成实例 note: * 必须保证有默认构造函数(如果调用方法:newInstance)或者通过Constructor来生成实例 * * @throws InstantiationException * @throws IllegalAccessException */ @Test public void testNewInstance() throws InstantiationException, IllegalAccessException { Class<LoginModel> loginModel = LoginModel.class; LoginModel model = loginModel.newInstance(); model.setNote(new ArrayList<String>()); model.setUsername("xinchun.wang"); System.out.println(model.getUsername()); } /** * 测试获取LoginModel实现的所有接口 */ @Test public void testGetInterfaces() { Class<?> bClass = LoginModel.class; Class[] interfaces = bClass.getInterfaces(); System.out.println(Arrays.toString(interfaces)); java.lang.reflect.Type[] genericType = bClass.getGenericInterfaces(); System.out.println(Arrays.toString(genericType)); for (java.lang.reflect.Type item : genericType) { if (item instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) item; // 获取原始类型 System.out.println(type.getRawType()); // 获取真实的参数类型 System.out.println(Arrays.toString(type.getActualTypeArguments())); // 意义不大 System.out.println(type.getOwnerType()); } } } /** * 获取LoginModel.class 构造函数 * * @throws Exception * @throws SecurityException */ @Test public void testGetConstructors() throws SecurityException, Exception { Class<?> bClass = LoginModel.class; // 获取所有构造函数的注解 Constructor[] cons = bClass.getConstructors(); System.out.println(Arrays.toString(cons)); // 获取特定的构造函数, note:获取不到将抛出异常 Constructor con = bClass.getConstructor(String.class, Integer.class); System.out.println(con); } /** * 获取LoginModel.class 类级别的注解 */ @Test public void testGetAnnotations() { Class<?> bClass = LoginModel.class; // 获取LoginModel.class的所有注解。包括可继承的注解。 Annotation[] arrAnno = bClass.getAnnotations(); System.out.println(Arrays.toString(arrAnno)); // 获取LoginModel.class特定的注解 Note note = bClass.getAnnotation(Note.class); System.out.println(note); // 获取在LoginModel.class 上直接声明的注解 Annotation[] dAnnoArr = bClass.getDeclaredAnnotations(); System.out.println(Arrays.toString(dAnnoArr)); } /** * 获取数组类型的Class,根据getComponentType 获取数组中的元素类型 */ @Test public void testArrayGetComponentType() { int[] intArr = new int[100]; Class<?> arrClass = intArr.getClass().getComponentType(); System.out.println(arrClass); Integer[] integerArr = new Integer[100]; Class<?> integerArrClass = integerArr.getClass().getComponentType(); System.out.println(integerArrClass); } /** * 测试Class的基本功能 */ @Test public void testClassBasic() { Class<?> bClass = LoginModel.class; // 简单名字 String simpleName = bClass.getSimpleName(); System.out.println(simpleName); // 包含包的类名字 System.out.println(bClass.getName()); System.out.println(bClass.getCanonicalName()); System.out.println(bClass.getClass()); System.out.println(Arrays.toString(bClass.getClasses())); System.out.println(Arrays.toString(bClass.getDeclaredClasses())); } /** * 获取LoginModel.class 所有可以访问的成员 note:公共访问、包括父类 * * @throws Exception * @throws SecurityException */ @Test public void testGetFields() throws SecurityException, Exception { Class<?> bClass = LoginModel.class; Field[] fields = bClass.getFields(); System.out.println(Arrays.toString(fields)); // 可以访问父类的成员 Field field = bClass.getField("rowid"); System.out.println(field); } /** * 获取LoginModel.class 所有的成员(继承的不返回) note: 无论field 是否是私有都可以获取 */ @Test public void testGetDeclaredFields() { Class<?> bClass = LoginModel.class; Field[] fArr = bClass.getDeclaredFields(); for (Field field : fArr) { System.out.println("field_name: " + field.getName()); } } }
发表评论
-
java-Annotation 反射
2016-08-23 15:33 740java中的注解也可以反射 切面注解: @Aspect ... -
java-InvocationHandler 动态代理
2016-07-28 16:15 1080以例子来说明动态代理: package com.gym; ... -
java.lang.annotation
2013-04-22 10:28 839java注解篇。 注解和注 ... -
lang.reflect ->java.lang.reflect.Proxy
2013-04-20 18:46 1432java 反射的另外一个主 ... -
java reflect 接口概述
2013-04-20 14:01 975java提供了反射功能。保证了运行时获取java 对象、方法、 ... -
lang.reflect->java.lang.reflect.Method
2013-04-19 16:46 1831java中反射使用几率最多的就是方法的反射。 功能支持的类库: ... -
lang.reflect->java.lang.Array
2013-04-19 15:10 1237数组反射功能依赖类java.lang.reflect.Arra ... -
lang.reflect->java.lang.reflect.Field
2013-04-19 14:43 1804java 反射功能强大,也是很多基础框架的核心技术。结合jdk ...
相关推荐
编写basedao的时候报错:java.lang.Class cannot be cast to java.lang.reflect.Parameterized
import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class ReflectionTest { public static void main(String[] args) { Class c = null; try { ...
java 反射实战demo,Java的反射(reflection)机制是指在程序的运行...我理解的Java反射的原理就是获取Class对象然后使用java.lang.reflect里提供的方法操作Class对象,Class与java.lang.reflect构成了java的反射技术。
Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.Access...
用以执行和JDK反射API中java.lang.Class,,java.lang.reflect.Method,, java.lang.reflect.Method .Field相同的操作。这些类可以使你在目标类被加载前,轻松的获得它的结构,函数,以及属性。此外,不仅仅是在功能...
at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable....
import java.lang.reflect.Method; 4. 5. import org.springframework.aop.framework.MethodBeforeAdvice; 6. 7. public class LogBeforeAdvice implements MethodAdvice { 8. public void before(Method ...
包含以下java源文件: com.google.gson.DefaultDateTypeAdapter.class com.google.gson.ExclusionStrategy.class com.google.gson.FieldAttributes.class com.google.gson.FieldNamingPolicy.class ...
at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run...
at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414) 2010-11-11 ...
3.2.4 使用反射机制的步骤 导入Jjava.lang.relfect 包需 遵循以下3三个步骤: 第一步是获得你想操作的类的 java.lang.Class 对象; 第二步是调用诸如 getDeclaredMethods 的方法; 第三步使用 反射API 来...
import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; import com.org.entity.User; /** */ public class Test3 { /** * @param args */ public static void main(String[] args) ...
import java.lang.reflect.Method; public class DumpMethods { public static void main(String args[]) throws Exception { // 加载并初始化命令行参数指定的类 Class<?> classType = Class.forName(args[0])...
例如Class<?> classType = Class.forName(java.lang.String); 例子: a..使用?通配符可以引用其他各种参数化的类型,但不能调用与参数化有关的方法; Collection<?> c = new Vector<String>(); c.add("abc");//报错...
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet....
at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) Caused by: java...
java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance...
at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable....
java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /* * JDK 动态代理 * */ public class dynamicProxyUsingJDK { public static void main(String[] args) ...
1.Configuration.xml 是 mybatis 用来建立 sessionFactory 用的,里面主要包含了数据库连接相关东西,还有 java 类所对应的别名,比如 <typeAlias alias="User" type="com.yihaomen.mybatis.model.User"/> 这个别名...