java 反射 get方法_java反射调用方法

java 反射 get方法_java反射调用方法I’mworkingwiththebasicsofJavareflectionandobservinginformationonmethodsofclasses.IneedtogetamethodthatmatchesspecificationsasdescribedbythegetMethod()function.However,…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

java 反射 get方法_java反射调用方法

I’m working with the basics of Java reflection and observing information on methods of classes. I need to get a method that matches specifications as described by the getMethod() function. However, when I do this I get a NoSuchMethodException, and I was hoping you could tell me why my implementation is incorrect.

static void methodInfo2(String className) throws ClassNotFoundException,

NoSuchMethodException{

Class cls = null;

try{

cls = Class.forName(className);

} catch(ClassNotFoundException e){

e.printStackTrace();

}

System.out.println(“Cls: “+cls);

Method method1 = cls.getMethod(“test”, null);

System.out.println(“method1: “+method1);

}

EDIT1:When I print out “Cls: “+cls, the output is “Cls: class a8.myclass2”. Why does it append the class part? (the a8 is correct, so don’t worry about that) /EDIT1

This is the function I use to read in a class from my main function, and then I want to getMethod() with the parameters “test” and null, where “test” is the name of the method and null means the method has no parameters. The class I am reading in is called myclass2 which is here:

package a8;

public class myclass2 {

void test(){

//”takes no parameters”

//”returns bool”

//”name starts with test”

//return true;

}

}

As you can see, the method does infact exist in the class. If you could point out my mistake, I would really appreciate it.

解决方案

Make your test method public. I believe Class.getMethod() is limited to public methods.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/190346.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • android加密参数定位方法

    android加密参数定位方法作者正在编写一本爬虫逆向相关的书籍《书名还没有想好》。目前刚写了50页,上图是文章目录的一部分,想问一下大家现在对哪阶段的内容会更感兴趣,或者哪部分的内容会更有学习力,或者帮我想个书名。可在本文末留言告知,感谢大家。下面开始本文正文内容。在逆向一个Android程序时,如果只是盲目的分析需要阅读N多代码才能找到程序的关键点或Hook点,本文将分享一下如何快速的找到APP程序的加密参数位置,其实不论是找关键位置、找hook点,找加密参数、代码逻辑追踪,都是类似的处理方法。巧用搜索-静态分析

    2022年5月17日
  • 一个c语言程序能实现几种算法_C语言实现算法

    一个c语言程序能实现几种算法_C语言实现算法E-mail:chengwenchi1986@gmail.com摘要:本文主要是对DOA(波达方向)估计中传统MUSIC算法及其改进算法作了简要的介绍,主要包括了MUSIC算法,求根MUSIC算法,循环MUSIC算法,波束空间MUSIC算法,SMARTMUSIC算法。并且在对每个算法做了原理性的分析的基础上给出了简要的性能分析。关键词:DOA估计;MUSIC算法;求根MUSIC算法;循环MUS…

    2022年10月29日
  • pycharm怎么看函数源代码_python查看第三方库的源码

    pycharm怎么看函数源代码_python查看第三方库的源码pycharm查看python源代码

    2022年8月28日
  • vb四环棋的实现,平面四子棋

    vb四环棋的实现,平面四子棋

    2021年3月12日
  • mac配置IDEA热部署[通俗易懂]

    mac配置IDEA热部署[通俗易懂]1、pom文件中添加依赖<!–开启热部署–><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional&gt

    2022年6月8日
  • JavaScript 数组排序——快速排序[通俗易懂]

    JavaScript 数组排序——快速排序[通俗易懂]数组中的快速排序就是取原始数组中的一个元素最为基点,小于基点的放在一个数组中,大于基点的放在一个数组中,无限循环,知道将数组分解到长度(length<1)停止vararr=[12,3,569,78,0,-56,-56,-56,1223,11,16,13,1];functionquickSort(arr){if(arr.length<1)returnarr;分解数组,长度小于1的时候停止执行varmiddle=pa…

    2022年5月1日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号