聊一聊Java反射

这次提到的Java反射涉及的代码比较多。因为工作中经常用到反射,对代码做了很多抽象以及过滤器。虽然代码量很多,但是简单易用,过滤插件也易修改。

下面介绍下工作中哪些地方比较容易用到反射。比如插件或者过滤器,如果抽象的子类比较少,配置成XML等结构也是可以达到同样的效果。如果希望灵活一些,添加了插件或者过滤器代码子类后希望可以直接使用。可能反射会比较好点,通过扫描所有class或者jar文件,得到所有继承的子类。如果每次调用都扫描所有的文件会比较影响性能。所以在实现里面加入反射缓存,对所要获取反射子类时涉及的所有参数作为一个key缓存所有的反射结果。下次如果是同样的key,就不在重新扫描。

代码示例如下:

public static void main(String[] args) {
 //设置扫描范围,可以是class文件所在位置例如bin下或者是mysql开头或者mysql结尾的jar,
 //设置为""为全部都扫描,这种比较耗时
 ReflectUtils.createSharedReflections("classes", "bin", "mysql");
 try {
  //调试阶段可以设置每次都全扫描
  //Beans.setDesignTime(true);
  final Collection<String> subTypes = ReflectUtils.listSubClass(IA.class);//
  for (final String subType : subTypes) {
  //这里获取的是所有继承IA的子类
  System.out.println(subType);
  final IA impl = ReflectUtils.initClass(subType, IA.class);
  if (null == impl)
   continue;
  //通过该方式,可以统一做操作,
  impl.print();
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 }

代码执行结果:

//缓存文件,避免每次调用反射都重新扫描
//如果删除该文件,再次调用反射时,会重新扫描,一般会在代码里面有添加子类的时候会删除该文件
XmlUtils.readXml failure:.\configuration.REF (系统找不到指定的文件。)
net.simple.reflect.test.B
net.simple.reflect.test.B
net.simple.reflect.test.D
net.simple.reflect.test.V

具体的类里面如何实现的大家就看下源码吧,这里贴出两个核心类的代码。源码地址:https://git.oschina.net/eliyanfei/api_tools

package net.simple.reflect;

import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import net.simple.reflect.filter.IPathURLFilter;
import net.simple.reflect.filter.ISubTypeFilter;
import net.simple.reflect.filter.ITypeFilter;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 * @author 李岩飞
 * @email eliyanfei@126.com
 * 2016年11月2日 下午3:23:49
 *
 */
public final class Reflections {
 private final Collection<URL> pathUrls;
 private final Collection<IPathURLFilter> pathURLfilters;
 private final Collection<ITypeFilter> typeFilters;
 private ISubTypeFilter subTypeFilter;

 public Reflections() {
 typeFilters = new ArrayList<ITypeFilter>();
 pathURLfilters = new ArrayList<IPathURLFilter>();
 this.pathUrls = ClasspathHelper.getUrlsForCurrentClasspath();
 }

 public Reflections(final Collection<URL> pathUrls) {
 this.pathUrls = pathUrls;
 typeFilters = new ArrayList<ITypeFilter>();
 pathURLfilters = new ArrayList<IPathURLFilter>();
 }

 /**
 * @param subTypeFilter
 *      the subTypeFilter to set
 */
 public void setSubTypeFilter(final ISubTypeFilter subTypeFilter) {
 this.subTypeFilter = subTypeFilter;
 }

 /**
 * @return the subTypeFilter
 */
 public ISubTypeFilter getSubTypeFilter() {
 return subTypeFilter;
 }

 public Reflections addPathURLFilter(final IPathURLFilter pathURLFilter) {
 if (null == pathURLFilter)
  return this;
 if (!this.pathURLfilters.contains(pathURLFilter))
  this.pathURLfilters.add(pathURLFilter);
 return this;
 }

 public Reflections addTypeFilter(final ITypeFilter typeFilter) {
 if (null == typeFilter)
  return this;
 if (!this.typeFilters.contains(typeFilter))
  this.typeFilters.add(typeFilter);
 return this;
 }

 private static final String histFile = "./configuration.REF";
 private Document histDom;

 public Collection<String> getSubTypesFast(final Class<?> baseType) {//, final String... typeNames
 //首先过滤出当前允许扫描的路径
 final StringBuilder bufPathsId = new StringBuilder(32);
 final Map<File, URL> fileUrls = new LinkedHashMap<File, URL>(8);
 for (final URL pathUrl : pathUrls) {
  if (!acceptPathUrl(pathUrl))
  continue;
  File file = null;
  try {
  file = new File(URLDecoder.decode(pathUrl.getFile(), "UTF-8"));
  } catch (final Exception e) {
  file = new File(pathUrl.getFile());
  }
  fileUrls.put(file, pathUrl);
  if (!file.exists())//is url file?ignore
  continue;
  bufPathsId.append(file.getName()).append(file.lastModified());
 }
 final String domId = MD5.getHashString(bufPathsId.toString());
 if (null == histDom)
  histDom = W3cUtils.readXml(histFile);
 if (null == histDom)
  histDom = W3cUtils.newDom("R");
 Element rootEle = histDom.getDocumentElement();
 if (null == rootEle)
  histDom.appendChild(rootEle = histDom.createElement("R"));
 if (!domId.equals(rootEle.getAttribute("id"))) {
  rootEle.getParentNode().removeChild(rootEle);
  histDom.appendChild(rootEle = histDom.createElement("R"));
  rootEle.setAttribute("id", domId);
 }
 final String baseTypeId = MD5.getHashString(baseType.getName());
 Element refEle = W3cUtils.firstChildElement(rootEle, "E", "id", baseTypeId);
 if (null != refEle) {
  final List<Element> valueEles = W3cUtils.childElementList(refEle, "F");
  final Collection<String> result = new ArrayList<String>(valueEles.size());
  for (final Element valueEle : valueEles) {
  result.add(new String(Base64.decodeFast(valueEle.getAttribute("id"))));
  }
  return result;
 }
 final ThreadPool<ListSubTypes> pool = new ThreadPool<ListSubTypes>();
 for (final File fileKey : fileUrls.keySet()) {
  pool.execute(new ListSubTypes(baseType, fileKey, fileUrls.get(fileKey)));
 }
 try {
  pool.shutdown(3, TimeUnit.MINUTES);
 } catch (final InterruptedException e) {
  e.printStackTrace();//for debug
 }
 final Collection<String> result = new ArrayList<String>();
 for (final ListSubTypes task : pool.getThreadRunables()) {
  result.addAll(task.result);
 }
 refEle = W3cUtils.addEle(rootEle, "E");
 refEle.setAttribute("id", baseTypeId);
 for (final String itm : result) {
  W3cUtils.addEle(refEle, "F").setAttribute("id", Base64.encodeToString(itm.getBytes(), false));
 }
 try {
  W3cUtils.writeXmlDocument(histFile, histDom);
 } catch (final Exception e) {
 }
 return result;
 }

 /**
 * @see {@link ReflectUtils#createSharedReflections(String...)}
 * @see {@link ReflectUtils#setSharedReflections(Reflections)}
 * @see {@link ReflectUtils#listSubClass(Class)}
 * @param baseType
 * @return
 */
 public Collection<String> getSubTypes(final Class<?> baseType, final String... typeNames) {//
 final ThreadPool<ListSubTypes> pool = new ThreadPool<ListSubTypes>();
 for (final URL pathUrl : pathUrls) {
  if (!acceptPathUrl(pathUrl))
  continue;
  File file = null;
  try {
  file = new File(URLDecoder.decode(pathUrl.getFile(), "UTF-8"));
  } catch (final Exception e) {
  file = new File(pathUrl.getFile());
  }
  pool.execute(new ListSubTypes(baseType, file, pathUrl, typeNames));
 }
 try {
  pool.shutdown(3, TimeUnit.MINUTES);
 } catch (final InterruptedException e) {
  e.printStackTrace();//for debug
 }
 final Collection<String> result = new ArrayList<String>();
 for (final ListSubTypes task : pool.getThreadRunables()) {
  result.addAll(task.result);
 }
 return result;
 }

 class ListSubTypes implements Runnable {
 final File file;
 final Class<?> baseType;
 final URL pathUrl;
 final String[] typeNames;

 public ListSubTypes(final Class<?> baseType, final File file, final URL pathUrl, final String... typeNames) {
  this.baseType = baseType;
  this.file = file;
  this.pathUrl = pathUrl;
  this.typeNames = typeNames;
 }

 Collection<String> result = new ArrayList<String>(4);

 @Override
 public void run() {
  if (file.isDirectory()) {
  listSubTypesFromDirectory(file, baseType, pathUrl, file, result, typeNames);
  } else
  listSubTypesFromJar(baseType, pathUrl, result, typeNames);
 }
 }

 /**
 * @param baseType
 * @param pathUrl
 * @param result
 */
 public void listSubTypesFromDirectory(final File baseDirectory, final Class<?> baseType, final URL pathUrl, final File directory,
  final Collection<String> result, final String... typeNames) {
 File[] files = directory.listFiles();
 if (null == files)
  files = new File[] {};
 String clazzPath;
 final int baseDirLen = baseDirectory.getAbsolutePath().length() + 1;
 for (final File file : files) {
  if (file.isDirectory()) {
  listSubTypesFromDirectory(baseDirectory, baseType, pathUrl, file, result, typeNames);
  } else {
  clazzPath = file.getAbsolutePath().substring(baseDirLen);
  clazzPath = clazzPath.replace('\\', '/');
  doTypesFilter(baseType, pathUrl, result, clazzPath, typeNames);
  }
 }
 }

 /**
 * @param baseType
 * @param pathUrl
 * @param result
 */
 public void listSubTypesFromJar(final Class<?> baseType, URL pathUrl, final Collection<String> result, final String... typeNames) {
 try {
  // It does not work with the filesystem: we must
  // be in the case of a package contained in a jar file.
  JarFile jarFile = null;
  try {
  if ("file".equals(pathUrl.getProtocol()))
   pathUrl = new URL("jar:" + pathUrl.toExternalForm() + "!/");
  jarFile = ((JarURLConnection) pathUrl.openConnection()).getJarFile();
  } catch (final Exception e) {
  final String filePath = pathUrl.getFile();
  // if on win platform
  if (filePath.indexOf(':') != -1) {
   if (pathUrl.getFile().charAt(0) == '/')
   jarFile = new JarFile(filePath.substring(1));
  }
  if (null == jarFile)
   jarFile = new JarFile(filePath);
  }
  final Enumeration<JarEntry> e = jarFile.entries();
  ZipEntry entry;
  while (e.hasMoreElements()) {
  entry = e.nextElement();
  doTypesFilter(baseType, pathUrl, result, entry.getName(), typeNames);
  }
 } catch (final IOException ioex) {
 }
 }

 private void doTypesFilter(final Class<?> baseType, final URL pathUrl, final Collection<String> result, final String clazzPath,
  final String... typeNames) {
 if (!clazzPath.endsWith(".class"))
  return;
 final int lastDotIdx = clazzPath.lastIndexOf('.');
 if (-1 == lastDotIdx)
  return;
 final String typeDef = clazzPath.substring(0, lastDotIdx).replace('/', '.');
 if (null != typeNames && typeNames.length > 0) {
  final int lastDot = typeDef.lastIndexOf('.');
  if (lastDot == -1)
  return;
  final String typeName = typeDef.substring(lastDot + 1);
  boolean withLiked = false;
  for (final String tmpTypeName : typeNames) {
  if (!typeName.contains(tmpTypeName))
   continue;
  withLiked = true;
  break;
  }
  if (withLiked == false)
  return;
 }
 if (this.typeFilters.isEmpty()) {
  if (null == this.subTypeFilter || this.subTypeFilter.accept(baseType, pathUrl, clazzPath))
  result.add(typeDef);
 } else {
  for (final ITypeFilter typeFilter : this.typeFilters) {
  if (!typeFilter.accept(clazzPath))
   continue;
  if (null == this.subTypeFilter || this.subTypeFilter.accept(baseType, pathUrl, clazzPath))
   result.add(typeDef);
  }
 }
 }

 /**
 * @param pathUrl
 * @return
 */
 private boolean acceptPathUrl(final URL pathUrl) {
 if (this.pathURLfilters.isEmpty())
  return true;
 for (final IPathURLFilter pathURLFilter : this.pathURLfilters) {
  if (pathURLFilter.accept(pathUrl))
  return true;
 }
 return false;
 }
}
package net.simple.reflect;

import java.beans.Beans;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import net.simple.reflect.filter.PathURLFilter;
import net.simple.reflect.filter.SampleSubInstanceFilter;
import net.simple.reflect.filter.TypeFilter;

/**
 *
 * @author 李岩飞
 * @email eliyanfei@126.com
 * 2016年11月2日 下午3:24:02
 *
 */
public final class ReflectUtils {
 public static final String VAR_START_FLAG = "${";
 public static final String VAR_END_FLAG = "}";

 private static Reflections sharedReflections;
 static final Collection<String> EMP_COLL = Collections.emptyList();

 public static final void createSharedReflections(final String... filterExts) {
 final Reflections refs = new Reflections();
 refs.addPathURLFilter(new PathURLFilter(filterExts));//
 refs.addTypeFilter(TypeFilter.DEFAULT);
 refs.setSubTypeFilter(SampleSubInstanceFilter.DEFAULT);
 ReflectUtils.setSharedReflections(refs);
 }

 /**
 * 此方法用于绑定一个通用的共享类型遍列工具.
 * @param sharedReflections
 */
 public static final void setSharedReflections(final Reflections sharedReflections) {
 ReflectUtils.sharedReflections = sharedReflections;
 }

 /**
 * 调用此方法之前必须先设置共享的类型遍列工具,参考:{@link #setSharedReflections(Reflections)},
 * 此方法主要使更方便的遍列给定类的实现,
 */
 public static final Collection<String> listSubClass(final Class<?> baseType, final String... typeNames) {//
 if (null == sharedReflections)
  return EMP_COLL;
 //调用阶段由于可能增加新的子类实现,需要每次都重新扫描,只有在发布的产品时使用保存记录的方法以提高启动速度.
 return Beans.isDesignTime() ? sharedReflections.getSubTypes(baseType, typeNames) : sharedReflections.getSubTypesFast(baseType);
 }

 public static List<Class<?>> listClassOfPackage(final Class<?> cType, final String extenion) {
 final List<Class<?>> result = new ArrayList<Class<?>>();
 final List<String> cPath = ReflectUtils.listClassCanonicalNameOfPackage(cType, extenion);
 for (final String path : cPath) {
  try {
  result.add(Class.forName(path, false, Thread.currentThread().getContextClassLoader()));
  } catch (final Exception e) {
  // ignore
  }
 }
 return result;
 }

 public static List<String> listClassCanonicalNameOfPackage(final Class<?> clazz, final String extenion) {
 return ReflectUtils.listNameOfPackage(clazz, extenion, true);
 }

 public static List<String> listClassNameOfPackage(final Class<?> clazz, final String extenion) {
 return ReflectUtils.listNameOfPackage(clazz, extenion, false);
 }

 public static List<String> listNameOfPackage(final Class<?> clazz, final String extenion, final boolean fullPkgName) {
 return ReflectUtils.listNameOfPackage(clazz.getName().replace('.', '/') + ".class", extenion, fullPkgName);
 }

 public static List<String> listNameOfPackage(final String clazzPkg, final String extenion, final boolean fullPkgName) {
 final List<String> result = new ArrayList<String>();

 final StringBuffer pkgBuf = new StringBuffer(clazzPkg);

 if (pkgBuf.charAt(0) != '/')
  pkgBuf.insert(0, '/');

 final URL urlPath = ReflectUtils.class.getResource(pkgBuf.toString());

 if (null == urlPath)
  return result;

 String checkedExtenion = extenion;
 if (!extenion.endsWith(".class"))
  checkedExtenion = extenion + ".class";

 if (pkgBuf.toString().endsWith(".class"))
  pkgBuf.delete(pkgBuf.lastIndexOf("/"), pkgBuf.length());

 pkgBuf.deleteCharAt(0);

 final StringBuffer fileUrl = new StringBuffer();
 try {
  fileUrl.append(URLDecoder.decode(urlPath.toExternalForm(), "UTF-8"));
 } catch (final UnsupportedEncodingException e1) {
  fileUrl.append(urlPath.toExternalForm());
 }

 if (fileUrl.toString().startsWith("file:")) {
  fileUrl.delete(0, 5);// delete file: flag
  if (fileUrl.indexOf(":") != -1)
  fileUrl.deleteCharAt(0);// delete flag
  final String baseDir = fileUrl.substring(0, fileUrl.lastIndexOf("classes") + 8);
  ReflectUtils.doListNameOfPackageInDirectory(new File(baseDir), new File(baseDir), result, pkgBuf.toString(), checkedExtenion, fullPkgName);
 } else {
  ReflectUtils.doListNameOfPackageInJar(urlPath, urlPath, result, pkgBuf.toString(), checkedExtenion, fullPkgName);
 }

 return result;
 }

 /**
 */
 private static void doListNameOfPackageInJar(final URL baseUrl, final URL urlPath, final List<String> result, final String clazzPkg, final String extenion, final boolean fullPkgName) {
 try {
  // It does not work with the filesystem: we must
  // be in the case of a package contained in a jar file.
  final JarURLConnection conn = (JarURLConnection) urlPath.openConnection();
  final JarFile jfile = conn.getJarFile();
  final Enumeration<JarEntry> e = jfile.entries();

  ZipEntry entry;
  String entryname;

  while (e.hasMoreElements()) {
  entry = e.nextElement();
  entryname = entry.getName();

  if (entryname.startsWith(clazzPkg) && entryname.endsWith(extenion)) {
   if (fullPkgName)
   result.add(entryname.substring(0, entryname.lastIndexOf('.')).replace('/', '.'));
   else
   result.add(entryname.substring(entryname.lastIndexOf('/') + 1, entryname.lastIndexOf('.')));
  }
  }
 } catch (final IOException ioex) {
 }
 }

 private static void doListNameOfPackageInDirectory(final File baseDirectory, final File directory, final List<String> result, final String clazzPkg, final String extenion,
  final boolean fullPkgName) {
 File[] files = directory.listFiles();
 if (null == files)
  files = new File[] {};
 String clazzPath;
 final int baseDirLen = baseDirectory.getAbsolutePath().length() + 1;
 for (final File file : files) {
  if (file.isDirectory()) {
  ReflectUtils.doListNameOfPackageInDirectory(baseDirectory, file, result, clazzPkg, extenion, fullPkgName);
  } else {
  if (!file.getName().endsWith(extenion))
   continue;

  if (fullPkgName) {
   clazzPath = file.getAbsolutePath().substring(baseDirLen);
   clazzPath = clazzPath.substring(0, clazzPath.length() - 6);
   result.add(clazzPath.replace(File.separatorChar, '.'));
  } else {
   result.add(file.getName().substring(0, file.getName().length() - 6));
  }
  }
 }
 }

 public static final <T> T initClass(final String implClass, final Class<T> tType) {
 return ReflectUtils.initClass(implClass, tType, true);
 }

 public static final <T> T initClass(final String implClass, final Class<T> tType, final boolean doInit) {
 try {
  final Object object = Class.forName(implClass, doInit, Thread.currentThread().getContextClassLoader()).newInstance();
  return tType.cast(object);
 } catch (final Throwable e) {
  return null;
 }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Java反射机制的实现详解

    很多主流框架都使用了反射技术.像ssh框架都采用两种技术 xml做配置文件+反射技术. 与反射有关的类包. java.lang.reflect.*;和java.lang.Class; Java中所有类型(包括基本类型)都对应一个Class对象,这个Class就是java.lang.Class.即每一个类型,在Class中都有一个Class对象跟它对应.Class 没有公共构造方法.注意不是没有,是没有公共的. 如何获得Class对象 复制代码 代码如下: .针对每一个对象.getCalss(),

  • Java反射机制及Method.invoke详解

    JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制. Java反射机制主要提供了以下功能: 在运行时判断任意一个对象所属的类:在运行时构造任意一个类的对象:在运行时判断任意一个类所具有的成员变量和方法:在运行时调用任意一个对象的方法:生成动态代理. 1. 得到某个对象的属性 复制代码 代码如下: public Object get

  • JAVA反射机制实例教程

    本文以实例形式详细讲述了Java的反射机制,是Java程序设计中重要的技巧.分享给大家供大家参考.具体分析如下: 首先,Reflection是Java 程序开发语言的特征之一,它允许运行中的 Java 程序对自身进行检查,或者说"自审",并能直接操作程序的内部属性.例如,使用它能获得 Java 类中各成员的名称并显示出来. Java 的这一能力在实际应用中也许用得不是很多,但是在其它的程序设计语言中根本就不存在这一特性.例如,Pascal.C 或者 C++ 中就没有办法在程序中获得函数

  • java反射实现javabean转json实例代码

    ObjectToJson.java 复制代码 代码如下: package com.ObjectToJson; import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Iterator; public class ObjectToJson {

  • Java 反射获取类详细信息的常用方法总结

    类ReflectionDemo 复制代码 代码如下: package Reflection; @Deprecated public class ReflectionDemo {     private String pri_field;     public String pub_field;     public ReflectionDemo(){}     public ReflectionDemo(String name){}     private ReflectionDemo(Stri

  • Java反射机制的学习总结

    一.什么是反射机制 简单的来说,反射机制指的是程序在运行时能够获取自身的信息.在java中,只要给定类的名字,那么就可以通过反射机制来获得类的所有信息. 二.哪里用到反射机制 有些时候,我们用过一些知识,但是并不知道它的专业术语是什么,在刚刚学jdbc时用过一行代码, Class.forName("com.mysql.jdbc.Driver.class").newInstance();但是那时候只知道那行代码是生成驱动对象实例,并不知道它的具体含义.听了反射机制这节课后,才知道,原来这

  • java反射机制示例详解

    1.什么是反射?一个类有多个组成部分,例如:成员变量,方法,构造方法等.反射就是加载类,并解剖出类的各个组成部分. 2.加载类java中有一个Class类用于代表某一个类的字节码.Class类既然代表某个类的字节码,那就要提供加载某个类字节码的方法:forName().   此方法用于加载某个类的字节码到内存中,并使用class对象进行封装.另外2种得到class对象的方式:类名.class对象.getClass() 先创建一个简单的Person类 复制代码 代码如下: public class

  • Java 采用反射获取class属性值的实现代码

    原理:Java的反射能够获取属性的名称,然后通过invoke调用类的某个方法.比如有个属性叫userName,这个类写了个方法叫getUserName,通过invoke调用getUserName这个方法.代码如下 复制代码 代码如下: import java.lang.reflect.Field;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;public class ParameterB

  • Java 反射之私有字段和方法详细介绍

    尽管我们通常认为通过JAVA的反射机制来访问其它类的私有字段和私有方法是可行的,其实并没有那么困难. 注释:只有在单独的JAVA程序中运行该代码才有效,就像你做一些单元测试或者常规的程序.如果你尝试在JAVA APPLET内使用该方法,你需要稍稍修改SecurityManager.但是,因为你不是经常需要与它打交道,这里也就不再赘述了. 这里是本次内容的列表: 1.访问私有字段. 2.访问私有方法. 访问私有字段: 为了访问私有字段,你需要调用Class.getDeclaredField(Str

  • java根据方法名称取得反射方法的参数类型示例

    复制代码 代码如下: /** * 根据方法名称取得反射方法的参数类型(没有考虑同名重载方法使用时注意) * @param obj         类实例   * @param methodName  方法名 * @return * @throws ClassNotFoundException */public static Class[]  getMethodParamTypes(Object classInstance,  String methodName) throws ClassNotF

随机推荐