- 浏览: 178152 次
- 性别:
- 来自: 济南
文章分类
- 全部博客 (101)
- DataBase (8)
- Java (37)
- Software (14)
- JavaScript (8)
- XML (4)
- WebService (5)
- SharePoint (6)
- ESB (4)
- Algorithm&Data Structure (5)
- CloudFoundry (1)
- liferay (1)
- portal (1)
- System Analysis (1)
- Storm (2)
- linux (3)
- Server (1)
- elasticsearch (1)
- angular (1)
- spark (1)
- python (2)
- ELK (1)
- Resin (1)
- Flink (1)
- python AI (1)
最新评论
-
yuekinger:
若果 64bit 下正常 32位系统下不正常运行的 排除dll ...
Java 调用 C# DLL -
yuekinger:
Win764 系统下成功! 实现 java 调用 c++ 链接 ...
Java 调用 C# DLL -
sdasds:
sdasds 写道pp1314 写道x931100537 写道 ...
Java 调用 C# DLL -
sdasds:
pp1314 写道x931100537 写道Exception ...
Java 调用 C# DLL -
pp1314:
x931100537 写道Exception in threa ...
Java 调用 C# DLL
1, 类引导,反射机制下的对象创建
类引导最终会字节流传给native 的java.lang.ClassLoader.defineClass1方法。
Class的 newInstance 方法 创建对象实例的时候,会调用无参的构造函数,如果类中没有 无参数的构造函数,JVM 会抛出java.lang.InstantiationException异常,无法进行实例化。
针对有参数的构造函数的类,可以调用class的getDeclaredMethod和Constructor 来构造这一个class的对象。
如果无参的构造函数是private类型,JVM调用该类时,会报private权限的错误。将无参的构造函数设为public类型可以解决该问题。
备注:
Java in a Nutshell这本书的第12章介绍了与class的反射机制有关的基本方法。
类引导最终会字节流传给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(";"); } }
发表评论
-
实时推送
2018-12-20 15:47 732初探和实现websocket心跳重连 http://www ... -
Resin 维护
2018-06-13 09:42 599<!--[if gte mso 9]>< ... -
自动化测试
2016-12-13 17:16 6021、linux下 执行JMeter测试脚本 在机器的 ... -
Spark 记录
2016-07-15 09:47 6881、java.lang.StackOverflowError ... -
Java 日常操作
2016-03-15 09:57 589Eclipse 闪退 1、增加内存设置 -vmargs ... -
maven技术分享(一) 标准maven项目的搭建
2014-08-27 14:19 0其中的nexus 的maven 私服搭建,参考网上的资料。 ... -
Java 调用 C# DLL
2014-04-11 16:57 15662Java无法直接调用C# dll,需要通过经过桥接的方式, ... -
Apache 负载均衡的环境搭建 (使用Tomcat作为web server)
2012-03-02 17:21 2843前言 使用tomcat作为web server,搭建Apach ... -
知识归纳索引
2012-02-23 09:57 1128本片文章记录了读过的书籍和文章,并将原始资料作为附件上传。 ... -
acegi 开发笔记
2011-12-26 17:36 7996. 编译acegi-security-1.0.7-s ... -
Mule 开发问题记录
2011-11-30 16:54 17031. A Mule runtime distribution ... -
ActiveMQ问题解决记录
2011-11-09 14:24 12596问题4:Networks of Brokers 在每个acti ... -
Log4j 学习笔记
2011-09-14 12:00 916为了彻底掌握log4j的使用,有必要去了解log4j的实现结构 ... -
ESB 研究
2011-08-24 15:08 1267Introduction to ESB 一些ESB的名词 E ... -
struts 开发笔记
2011-08-24 10:44 13251,struts默认根据http request请求流中的键值 ... -
JMS,ActiveMQ 内部实现的研究
2011-08-23 10:28 10674问题1,AMQ 4的openWireFormat与AMQ 5的 ... -
设计模式
2011-08-04 11:38 10091, 设计模式在JDK中的体现 参见stackoverflow ... -
ActiveMQ 开发
2011-07-29 13:28 26541, 配置activemq.xml时,修改<broker ... -
Java 中的 中文编码
2011-05-26 13:56 53431. java.net.URLEncoder.encode j ... -
Spring + Hibernate 例子(使用petclinic, 利用ant)
2011-03-03 16:30 1005Spring + Hibernate 例子 1. 使用Spr ...
相关推荐
java学习笔记java反射机制
Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记
java学习笔记 [Java学习笔记doc]之反射机制
Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习...
Java学习笔记:JavaSe、Spring、JVM、MySQL、MyBatis...
在学习java过程中的笔记。
java注解和反射的个人学习笔记
Java 虚拟机学习笔记: Java 内存区域, 垃圾收集, 内存分配与回收策略, JVM 调优, 文件结构, 类加载机制, Java 程序 Java是一种面向对象的编程语言,由Sun Microsystems于1995年推出。它是一种跨平台的语言,...
Java I/O学习笔记: 磁盘操作 字节操作 字符操作 对象操作 网络操作 NIO & AIO Java I/O Java是一种面向对象的编程语言,由Sun Microsystems于1995年推出。它是一种跨平台的语言,意味着可以在不同的操作系统上运行...
java学习笔记java学习笔记.zipjava学习笔记.zipjava学习笔记.zipjava学习笔记.zipjava学习笔记.zipjava学习笔记.zipjava学习笔记.zipjava学习笔记.zipjava学习笔记.zipjava学习笔记.zipjava学习笔记.zipjava学习笔记...
这是一年多以来学习java和在项目中遇到问题之后总结出的一系列资料。包含:java 反射机制 流 内存管理等信息。
java学习笔记,java基础知识,只用于个人学习。
JAVA学习笔记JAVA学习笔记JAVA学习笔记JAVA学习笔记JAVA学习笔记JAVA学习笔记JAVA学习笔记JAVA学习笔记JAVA学习笔记
java反射机制
java学习笔记.doc java学习笔记 java学习 学习笔记 java
Java项目学习笔记: SSM实战项目——Java高并发秒杀API,详细流程+学习笔记
Java基础 java学习笔记 Java Java基础Markdown学习笔记,可转换成PDF、Word等格式
Contiki学习笔记:创建两个交互进程 Contiki 主函数剖析: Contiki学习笔记:main函数剖析 Contiki学习笔记:启动一个进程process_start Contiki学习笔记:系统进程etimer_process Contiki学习笔记:深入理解process...
Java 基础学习笔记,主要包括: 10_Java常见对象.md 10_Java常见对象_2.md 1_数据类型.md 3_运算.md 4_Object通用方法.md 5_关键字.md 6_反射.md 8_泛型.md JDK8新特性.md 正则表达式.md Java是一种面向对象的编程...
Java反射机制笔记,简单易懂,轻松搞定Java反射机制