[转]JAVA 反射及使用


<%@page contentType="text/html; charset=GBK"%>
<%@page import="java.security.*,java.net.*,java.io.*"%>
<%!
public static URL getClassLocation(final Class cls) {
if (cls == null)throw new IllegalArgumentException("null input: cls");
URL result = null;
final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
final ProtectionDomain pd = cls.getProtectionDomain();
// java.lang.Class contract does not specify if 'pd' can ever be null;
// it is not the case for Sun's implementations, but guard against null
// just in case:
if (pd != null) {
final CodeSource cs = pd.getCodeSource();
// 'cs' can be null depending on the classloader behavior:
if (cs != null) result = cs.getLocation();
if (result != null) {
// Convert a code source location into a full class file location
// for some common cases:
if ("file".equals(result.getProtocol())) {
try {
if (result.toExternalForm().endsWith(".jar") ||
result.toExternalForm().endsWith(".zip"))
result = new URL("jar:".concat(result.toExternalForm())
.concat("!/").concat(clsAsResource));
else if (new File(result.getFile()).isDirectory())
result = new URL(result, clsAsResource);
}
catch (MalformedURLException ignore) {}
}
}
}
if (result == null) {
// Try to find 'cls' definition as a resource; this is not
// document.d to be legal, but Sun's implementations seem to //allow this:
final ClassLoader clsLoader = cls.getClassLoader();
result = clsLoader != null ?
clsLoader.getResource(clsAsResource) :
ClassLoader.getSystemResource(clsAsResource);
}
return result;
}
%>
<html>
<head>
<title>srcAdd.jar</title>
</head>
<body bgcolor="#ffffff">
使用方法,className参数为类的全名,不需要.class后缀,如
srcAdd.jsp?className=java.net.URL
<%
try
{
String classLocation = null;
String error = null;
String className = request.getParameter("className"); classLocation = ""+getClassLocation(Class.forName(className));
if (error == null) {
out.print("类" + className + "实例的物理文件位于:");
out.print("<hr>");
out.print(classLocation);
}
else {
out.print("类" + className + "没有对应的物理文件。<br>");
out.print("错误:" + error);
}
}catch(Exception e)
{
out.print("异常。"+e.getMessage());
}
%>
</body>
</html>
srcAdd.jsp
还有一个类: ClassLocationUtils.java ,在 IDEA 断点调试时,可按 Alt+F8 组合键,弹出 Evaluate Expression 对话框,
在 Expression 处输入 ClassLocationUtils.where(<类名>.class) 即可获知当前类是从哪个 JAR 包中加载的。
package com.smart.utils; import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain; /**
* tools to find which jar does the class come from
* @author : chenxh
* @date: 16-04-0
*/
public class ClassLocationUtils { /**
* find the location of the class come from
* @param cls
* @return
*/
public static String where(final Class cls) {
if (cls == null)throw new IllegalArgumentException("null input: cls");
URL result = null;
final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
final ProtectionDomain pd = cls.getProtectionDomain();
if (pd != null) {
final CodeSource cs = pd.getCodeSource();
if (cs != null) result = cs.getLocation();
if (result != null) {
if ("file".equals(result.getProtocol())) {
try {
if (result.toExternalForm().endsWith(".jar") ||
result.toExternalForm().endsWith(".zip"))
result = new URL("jar:".concat(result.toExternalForm())
.concat("!/").concat(clsAsResource));
else if (new File(result.getFile()).isDirectory())
result = new URL(result, clsAsResource);
}
catch (MalformedURLException ignore) {}
}
}
}
if (result == null) {
final ClassLoader clsLoader = cls.getClassLoader();
result = clsLoader != null ?
clsLoader.getResource(clsAsResource) :
ClassLoader.getSystemResource(clsAsResource);
}
return result.toString();
}
}
ClassLocationUtils.java
-- 本文摘自 《精通Spring 4.x 企业应用开发实战》

[转]JAVA 反射及使用的更多相关文章
- 第28章 java反射机制
java反射机制 1.类加载机制 1.1.jvm和类 运行Java程序:java 带有main方法的类名 之后java会启动jvm,并加载字节码(字节码就是一个类在内存空间的状态) 当调用java命令 ...
- Java反射机制
Java反射机制 一:什么事反射机制 简单地说,就是程序运行时能够通过反射的到类的所有信息,只需要获得类名,方法名,属性名. 二:为什么要用反射: 静态编译:在编译时确定类型,绑定对象,即通过 ...
- java反射(基础了解)
package cn.itcast_01; /** *Person类 */ public class Person { /** 姓名 */ private String name; ...
- java基础知识(十一)java反射机制(上)
java.lang.Class类详解 java Class类详解 一.class类 Class类是java语言定义的特定类的实现,在java中每个类都有一个相应的Class对象,以便java程序运行时 ...
- java基础知识(十一)java反射机制(下)
1.什么是反射机制? java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法,对于任意一个对象都能够调用他的属性和方法,这种动态获取属性和方法的功能称为java的反射机制. ...
- java反射学习之二万能EXCEL导出
一.EXCEL导出的实现过程 假设有一个对象的集合,现在需要将此集合内的所有对象导出到EXCEL中,对象有N个属性:那么我们实现的方式是这样的: 循环这个集合,在循环集合中某个对象的所有属性,将这个对 ...
- java反射学习之一反射机制概述
一.反射机制背景概述 1.反射(reflection)是java被视为动态语言的一个关键性质 2.反射机制指的是程序在运行时能获取任何类的内部所有信息 二.反射机制实现功能概述 1.只要给定类的全名, ...
- java反射 之 反射基础
一.反射 反射:JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为 ...
- java反射 cglib asm相关资料
有篇文章对java反射的调用的效率做了测试,写的比较好.猛击下面地址 http://www.blogjava.net/stone2083/archive/2010/09/15/332065.html ...
- 超详细的java反射教程
看技术博客时,看到关于java反射的博文,写的非常好.猛击下面的地址,开始java反射之旅 中文翻译地址:http://ifeve.com/java-reflection/ 英文原版地址:http:/ ...
随机推荐
- LeetCode——Rotate Image
1. Question You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ( ...
- SDN前瞻 网络的前世今生
本文基于SDN导论的视频而成:SDN导论 目前网络层面流行的技术概念:虚拟中心:公有云私有云:数据中心等等. SDN主要的模拟器:Mininet OpenDaylight(Cisco) ONOS(AT ...
- 科幻小说《霜与火》 by 雷·布雷德伯里
漫漫长夜中,西姆出世了,躺在山洞冰凉的石头上,嗷嗷大哭,浑身血液奔流,脉搏每分钟一千跳.他不停地长大.妈妈用发烫的双手喂西姆吃东西.人生的梦魔开场了.一来到世间,他的眼睛就闪烁着警觉的光芒:而后又令人 ...
- UVALive-4452 The Ministers' Major Mess (2-SAT)
题目大意:有n个问题,m个人来投票,没人最多投4票,问该怎样决定才能使每个人都有超过一半的票数被认可? 题目分析:2-SAT问题.如果某个人投的票数少于2,则这两票军被采纳,如果票数至少三票,则最多有 ...
- 根据rowid查找所属的对象号、文件号、块号、行号-还是简单一点吧
select rowid, dbms_rowid.rowid_object(rowid) obj#, dbms_rowid.rowid_block_number(rowid) ...
- db2时间函数
获取当前日期: select current date from sysibm.sysdummy1; values current date; --获取当前时间 select current time ...
- IOS-高仿bilibili项目
高仿bilibili项目成长之路 (logo) 高仿bilibili项目 Github链接:(https://github.com/MichaelHuyp/Bilibili_Wuxianda) 目前完 ...
- 无线基站侧的信令风暴根因——频繁的释放和连接RRC产生大量信令、设备移动导致小区重选信令增加、寻呼信令多
全局思维(核心网和无线基站侧都会有信令风暴): LTE网络系统可能出现信令风暴的原因,大致可以总结出以下几点: 1.网络架构的变化,导致4G核心网信令流量较2G/3G大幅增加 a)架构扁平化:LTE网 ...
- IAR使用notice
1.IAR中无法程序跳转问题 在工程的C编译器选项里的预编译添加$TOOLKIT_DIR$\inc解决,需要clean一下工程再make即可.($TOOLKIT_DIR$:这个语法表示包含文件的路径在 ...
- java Web 工程servlet中@WebServlet("/HelloServlet") 是怎么工作的
编写好Servlet之后,接下来要告诉Web容器有关于这个Servlet的一些信息.在Servlet 3.0中,可以使用标注(Annotation)来告知容器哪些Servlet会提供服务以及额外信息. ...