Java 学习笔记:1,类引导,反射机制下的对象创建

1, 类引导,反射机制下的对象创建
类引导最终会字节流传给native 的java.lang.ClassLoader.defineClass1方法。
Class的 newInstance 方法 创建对象实例的时候,会调用无参的构造函数,如果类中没有 无参数的构造函数,JVM 会抛出java.lang.InstantiationException异常,无法进行实例化。
针对有参数的构造函数的类,可以调用class的getDeclaredMethod和Constructor 来构造这一个class的对象。

如果无参的构造函数是private类型,JVM调用该类时,会报private权限的错误。将无参的构造函数设为public类型可以解决该问题。


URL url1 = new URL("file:/E:/Server/vsmessages_fortest.jar");
URLClassLoader myClassLoader = new URLClassLoader( new URL[] { url1 } );
Class myClass = myClassLoader.loadClass("cn.com.*.messageagent.vo.MessageVO");
Method m01 = myClass.getDeclaredMethod("setClientId", String.class);
Method m02 = myClass.getDeclaredMethod("getClientId");


Constructor ctor = myClass.getConstructor(String.class);

ctor.setAccessible(true);
Object targetObj1 = ctor.newInstance("1234567");

// m01.invoke(targetObj1, "123456");
String clientId = (String) m02.invoke(targetObj1);
log.info("clientId:" + clientId);



备注:
Java in a Nutshell这本书的第12章介绍了与class的反射机制有关的基本方法。

Example 12.1: Obtaining Class and Member Information with the Reflection API

import java.lang.reflect.*;
/** A program that displays a class synopsis for the named class. */
public class ShowClass {
/** The main method. Print info about the named class. */
public static void main(String[] args) throws ClassNotFoundException {
Class c = Class.forName(args[0]);
print_class(c);
}
/** Display the modifiers, name, superclass, and interfaces of a class
* or interface. Then go and list all constructors, fields, and methods. */
public static void print_class(Class c)
{
// Print modifiers, type (class or interface), name, and superclass.
if (c.isInterface()) {
// The modifiers will include the "interface" keyword here...
System.out.print(Modifier.toString(c.getModifiers()) + c.getName());
}
else
System.out.print(Modifier.toString(c.getModifiers()) + " class " +
c.getName() +
" extends " + c.getSuperclass().getName());
// Print interfaces or super-interfaces of the class or interface.
Class[] interfaces = c.getInterfaces();
if ((interfaces != null) && (interfaces.length > 0)) {
if (c.isInterface()) System.out.println(" extends ");
else System.out.print(" implements ");
for(int i = 0; i < interfaces.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print(interfaces[i].getName());
}
}
System.out.println(" {"); // Begin class member listing.
// Now look up and display the members of the class.
System.out.println(" // Constructors");
Constructor[] constructors = c.getDeclaredConstructors();
for(int i = 0; i < constructors.length; i++) // Display constructors.
print_method_or_constructor(constructors[i]);
System.out.println(" // Fields");
Field[] fields = c.getDeclaredFields(); // Look up fields.
for(int i = 0; i < fields.length; i++) // Display them.
print_field(fields[i]);
System.out.println(" // Methods");
Method[] methods = c.getDeclaredMethods(); // Look up methods.
for(int i = 0; i < methods.length; i++) // Display them.
print_method_or_constructor(methods[i]);
System.out.println("}"); // End class member listing.
}
/** Return the name of an interface or primitive type, handling arrays. */
public static String typename(Class t) {
String brackets = "";
while(t.isArray()) {
brackets += "[]";
t = t.getComponentType();
}
return t.getName() + brackets;
}
/** Return a string version of modifiers, handling spaces nicely. */
public static String modifiers(int m) {
if (m == 0) return "";
else return Modifier.toString(m) + " ";
}
/** Print the modifiers, type, and name of a field. */
public static void print_field(Field f) {
System.out.println(" " +
modifiers(f.getModifiers()) +
typename(f.getType()) + " " + f.getName() + ";");
}
/** Print the modifiers, return type, name, parameter types, and exception
* type of a method or constructor. Note the use of the Member interface
* to allow this method to work with both Method and Constructor objects. */
public static void print_method_or_constructor(Member member) {
Class returntype=null, parameters[], exceptions[];
if (member instanceof Method) {
Method m = (Method) member;
returntype = m.getReturnType();
parameters = m.getParameterTypes();
exceptions = m.getExceptionTypes();
} else {
Constructor c = (Constructor) member;
parameters = c.getParameterTypes();
exceptions = c.getExceptionTypes();
}
System.out.print(" " + modifiers(member.getModifiers()) +
((returntype!=null)? typename(returntype)+" " : "") +
member.getName() + "(");
for(int i = 0; i < parameters.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print(typename(parameters[i]));
}
System.out.print(")");
if (exceptions.length > 0) System.out.print(" throws ");
for(int i = 0; i < exceptions.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print(typename(exceptions[i]));
}
System.out.println(";");
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值