gpt4 book ai didi

java - 从公共(public)静态上下文访问私有(private)静态方法

转载 作者:搜寻专家 更新时间:2023-11-01 01:19:59 25 4
gpt4 key购买 nike

考虑这个示例类,

class TargetClass {
private static String SENSITIVE_DATA = "sw0rdfish";

private static String getSensitiveData() {
return SENSITIVE_DATA;
}
}

当我这样做时,

import java.lang.reflect.Method;

public class ClassPiercing {

public static void main(String... args) throws Exception {
Class targetClass = Class.forName("TargetClass");
Method[] methods = targetClass.getDeclaredMethods();
methods[0].setAccessible(true);
String sensitiveData = (String)methods[0].invoke(null, null);
System.out.println("Sensitive Data: " + sensitiveData);
}
}

输出是,

Sensitive Data: sw0rdfish

这很危险。我该如何防止这种情况发生?

最佳答案

关于java - 从公共(public)静态上下文访问私有(private)静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/862350/

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