Java Reflection-classes

Java Reflection

http://tutorials.jenkov.com/java-reflection/classes.html

Why and where Reflectioin?

Reflection allows a program to work with code that may not be present and do so in a reliable way.

"Normal code" have snippets like URLConnection c = null which by its sheer presence cause the class loader to load the URLConnection class as part of loading this class, throwing a ClassNotFound exception and exiting.

Reflection allow you to load classes based on their names in string form and test them for various properties (useful for multiple versions outside your control) before launching actual classes that depend on them. A typical example is the OS X specific code used to make Java programs look native under OS X, which are not present on other platforms.



java.lang.class
java.lang.reflect

Classes

Using Java Reflection you can inspect Java classes at runtime. Inspecting classes is often the first thing you do when using Reflection. From the classes you can obtain information about

plus a lot more information related to Java classes. For a full list you should consult theJavaDoc for java.lang.Class. This text will briefly touch upon all accessing of the above mentioned information. Some of the topics will also be examined in greater detail in separate texts. For instance, this text will show you how to obtain all methods or a specific method, but a separate text will show you how to invoke that method, how to find the method matching a given set of arguments if more than one method exists with the same name, what exceptions are thrown from method invocation via reflection, how to spot a getter/setter etc. The purpose of this text is primarily to introduce the Class object and the information you can obtain from it.


Get Class Object

 If you know the name of the class at compile time you can obtain a  Class  object like this:
Class myClass=String.class;
System.out.println(myClass.toString());

result: class java.lang.String

If you don't know the name at compile time, but have the class name as a string at runtime, you can do like this:
				String s="Hello world";//an Object at runtime
				String className=s.getClass().getName();//obtain class name as string at runtime
				Class myClass=Class.forName(className);
				//forname static method support only fully qualified name
				
				System.out.println("fully qualified name: "+className);
				System.out.println("get class as string: "+myClass.toString());

get result
fully qualified name: java.lang.String
get class as string: class java.lang.String

Get Class Name

				String s="Hello world";//an Object at runtime
				String className=s.getClass().getName();//fully qualified name
				String classSimpleName=s.getClass().getSimpleName();
				
				System.out.println("fully qualified name: "+className);
				System.out.println("simple name: : "+classSimpleName);

fully qualified name: java.lang.String
simple name: : String

Modifier

You can access the modifiers of a class via the Class object. The class modifiers are the keywords "public", "private", "static" etc. You obtain the class modifiers like this:

  				String s="Hello world";
				Class myClass=s.getClass();
				int modifiers=myClass.getModifiers();
				
				System.out.println(Modifier.toString(modifiers));
				//output the modifiers of class string 


http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Modifier.html

Following this you can obtain other informations on the class by utilizing relevant methods.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值