`

java反射:object is not an instance of declaring class

    博客分类:
  • Java
阅读更多
	/**
	 * <pre>
	 * <h3>通过反射执行某个方法,并将数据集放入cache中</h3>
	 * </pre>
	 * 
	 * 方法实现呆完成
	 * 
	 * @param key
	 *            缓存key
	 * @param _class
	 *            类名
	 * @param methodName
	 *            类方法名
	 * @param paramType
	 *            方法参数类型
	 * @param args
	 *            方法参数值
	 */
	public void reflect(String key, Class<T> _class, String methodName,
			Class<Object>[] paramType, Object... args) {
		try {
			// cache判断
			// 如果cache中没有这个值,则调用方法填充cache
			Method method = _class.getMethod(methodName, paramType);
			method.invoke(_class, args);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}


执行到:
method.invoke(_class, args);

报错:
object is not an instance of declaring class
说明Class没有实例化;
解决办法:
由于没有实力化可以有如下两种方法:
1、反射方法定义成为static的,故被反射类就不需要实例化;
2、method.invoke(_class.newInstance(), args);
分享到:
评论
2 楼 GGGGeek 2017-11-17  
1 楼 com.張 2015-01-29  
村上春树

相关推荐

    ant 简单教程

    1. 完备的 Java 构建和部署工具:Ant 提供全面的功能覆盖,能够满足各种 Java 项目的构建需求。 2. 平台中立:Ant 能够处理平台特定属性,如文件分隔符,确保跨平台兼容性。 3. 平台特定任务执行:例如,使用 'touch...

    大数据关键技术与挑战

    大数据是信息技术领域的一个重要分支,它涉及到数据的采集、存储、管理、分析与应用等多个方面。随着互联网、物联网、云计算等技术的发展,数据的规模和复杂性呈爆炸性增长,对数据处理技术提出了更高的要求。...

    java面试题英文版及其答案

    12. Explain the concept of polymorphism in Java.Answer: Polymorphism is the ability of an object to take on many forms. In Java, it is achieved through method overriding and method overloading. Method...

    JSP Simple Examples

    One of the strong features of java is that it is follows a OOPs concept, and one of the feature of OOP in java is that, we can assign a subclass object or variable to the variable of the superclass ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    For example, if your header file uses the File class in ways that do not require access to the declaration of the File class, your header file can just forward declare class File; instead of having ...

    IOS5 Programming Cookbook

    - **Project Structure**: Understanding the structure of an Xcode project is crucial. Projects typically consist of files such as source code files (.m), interface definition files (.h), resource files...

    Sakemail

    Fixed a bug when sending email to more than two address (the separator is still ‘,‘).9/3/981.6.0- Sometimes the filenames of an attachment contain invalid chars making very dificult to open a ...

    Objective-C 基础教程

    - **Static Typing to an Inherited Class(对继承类进行静态类型)**:解释了如何将静态类型应用于继承的类。 综上所述,Objective-C基础教程覆盖了从入门到进阶的多个方面,适合Objective-C初学者系统学习该语言...

    ZendFramework中文文档

    Fetching a Row as an Object 10.3. Zend_Db_Profiler 10.3.1. Introduction 10.3.2. Using the Profiler 10.3.3. Advanced Profiler Usage 10.3.3.1. Filter by query elapsed time 10.3.3.2. Filter by ...

    spring-framework-reference4.1.4

    Instantiation using an instance factory method ........................................... 30 4.4. Dependencies ...........................................................................................

    spring-framework-reference-4.1.2

    Instantiation using an instance factory method ........................................... 30 4.4. Dependencies ...........................................................................................

    SystemVerilog Reference Manual 3.1a(中英文版)+最新SV IEEE 标准

    Table of Contents Section 1 Introduction to SystemVerilog ...................................................................................................... 1 Section 2 Literal Values................

Global site tag (gtag.js) - Google Analytics