gpt4 book ai didi

java - 如何使用 ReflectionUtils 获取包私有(private)方法/字段?

转载 作者:行者123 更新时间:2023-12-01 18:16:37 29 4
gpt4 key购买 nike

我尝试通过反射获取包私有(private)方法/字段,如下所示:

for (Method m : getAllMethods(cls, withModifier(Modifier.STATIC))){

但它是空的。有没有办法按包私有(private)进行过滤?

最佳答案

for (Method m : getAllMethods(cls, ReflectionUtils.getAllMethods(cls, Predicates.not(withModifier(Modifier.PRIVATE)), Predicates.not(withModifier(Modifier.PUBLIC)), Predicates.not(withModifier(Modifier.PROTECTED)))))
{
// DO STUFF
}

我使用以下内容来测试上述内容。 MyClass 包含 5 个方法;一个用于每个访问修饰符,一个静态:

public class MyClass
{

public void method1()
{
System.out.println("method1() invoked");
}

private void method2()
{
System.out.println("method2() invoked");
}

protected void method3()
{
System.out.println("method3() invoked");
}

void method4()
{
System.out.println("method4() invoked");
}

public static void method5()
{
System.out.println("method5() invoked");
}
}

我的测试类:

import static org.reflections.ReflectionUtils.withModifier;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Set;

import org.reflections.ReflectionUtils;

import accessmodifier.bar.MyClass;

import com.google.common.base.Predicates;

public class ReflectionTest
{

public static void main(String[] args)
{
Class<MyClass> cls = MyClass.class;
Set<Method> privateMethods = ReflectionUtils.getAllMethods(cls, withModifier(Modifier.PRIVATE));
Set<Method> protectedMethods = ReflectionUtils.getAllMethods(cls, withModifier(Modifier.PROTECTED));
Set<Method> publicMethods = ReflectionUtils.getAllMethods(cls, withModifier(Modifier.PUBLIC));
Set<Method> defaultMethods = ReflectionUtils.getAllMethods(cls, Predicates.not(withModifier(Modifier.PRIVATE)), Predicates.not(withModifier(Modifier.PUBLIC)), Predicates.not(withModifier(Modifier.PROTECTED)));
Set<Method> staticMethods = ReflectionUtils.getAllMethods(cls, withModifier(Modifier.STATIC));

System.out.println("Private Methods");
for (Method m : privateMethods)
System.out.println(m.getName());

System.out.println("\nProtected Methods");
for (Method m : protectedMethods)
System.out.println(m.getName());

System.out.println("\nPublic Methods");
for (Method m : publicMethods)
System.out.println(m.getName());

System.out.println("\nPackage-Private Methods");
for (Method m : defaultMethods)
System.out.println(m.getName());

System.out.println("\nStatic Methods");
for (Method m : staticMethods)
System.out.println(m.getName());
}
}

程序输出

Private Methods
method2

Protected Methods
method3

Public Methods
method1
method5

Package-Private Methods
method4

Static Methods
method5

更新如果您希望能够调用可访问的方法(例如 package-private),您将必须这样做:

        m.setAccessible(true); // bypass access
try
{
m.invoke(new MyClass(), null); // invoke method (you have to know parameter types and pass them if needed. Use *Method.getParameter...()* methods for that.
}
catch (IllegalAccessException | IllegalArgumentException
| InvocationTargetException e)
{
e.printStackTrace();
}

关于java - 如何使用 ReflectionUtils 获取包私有(private)方法/字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29142774/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com