Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在runtie期间操作类中声明的文件?_Java - Fatal编程技术网

Java 如何在runtie期间操作类中声明的文件?

Java 如何在runtie期间操作类中声明的文件?,java,Java,我在一个项目中工作,我试图在运行时操纵类,但我不知道怎么做 到目前为止,我的做法如下: 关闭java安全管理器 上课 接收反射数据 操纵申报字段 问题是,当我尝试列出给定字段时,默认模式中有字段,而不是我期望的值 以下是我的资料来源: public static void manipulatingClassData(Class<?> clazz) { try { Field originalReflectionData = null; //the normal ref fun

我在一个项目中工作,我试图在运行时操纵类,但我不知道怎么做

到目前为止,我的做法如下:

  • 关闭java安全管理器
  • 上课
  • 接收反射数据
  • 操纵申报字段
问题是,当我尝试列出给定字段时,默认模式中有字段,而不是我期望的值

以下是我的资料来源:

public static void manipulatingClassData(Class<?> clazz) {
try {
  Field originalReflectionData = null; //the normal ref function are just copying thinks look at Class:2574
  for (Field declaredField : clazz.getClass().getDeclaredFields()) {
    if (declaredField.getName().equalsIgnoreCase("reflectionData")) {
      originalReflectionData = declaredField; // this is private class from Class where every field method and so on is saved Class:2464
    }
  }
  originalReflectionData.setAccessible(true);
  SoftReference<?> softReference = (SoftReference<?>) originalReflectionData.get(clazz.getClass());
  Object ob = softReference.get(); //getting the object (you cant use the class it self cause its an private inner class)
  for (Field allFieldsInAClass : ob.getClass().getDeclaredFields()) {
    if(allFieldsInAClass.getName().equalsIgnoreCase("declaredFields")) { // this is the set of fields saved in your given class
      allFieldsInAClass.setAccessible(true);
      
      Field[] realFieldsInYourClass = (Field[]) allFieldsInAClass.get(ob);  //the whole think casted
      for (Field fieldsInYourClass : realFieldsInYourClass) { // this shut now throw out every field declared in your class but it does not....
        fieldsInYourClass.setAccessible(true);
        System.out.println(fieldsInYourClass.getName()); //as far as I cann tell its throwing out its own class and links in itself as far as I can see
        // at this point I do not understand why its not giving me the data i want to have
      }
    }
  }

}catch (Exception e){
  e.printStackTrace();
}}
publicstaticvoid操作类数据(类clazz){
试一试{
Field originalReflectionData=null;//正常的ref函数只是复制类:2574
for(字段declaredField:clazz.getClass().getDeclaredFields()){
if(declaredField.getName().equalsIgnoreCase(“reflectionData”)){
originalReflectionData=declaredField;//这是保存每个字段方法等的类的私有类。类:2464
}
}
原始ReflectionData.setAccessible(true);
SoftReference SoftReference=(SoftReference)originalReflectionData.get(clazz.getClass());
Object ob=softReference.get();//获取对象(不能使用对象本身的类,因为它是私有的内部类)
对于(字段allFieldsInAClass:ob.getClass().getDeclaredFields()){
if(allFieldsInAClass.getName().equalsIgnoreCase(“declaredFields”){//这是保存在给定类中的字段集
allFieldsInAClass.setAccessible(true);
Field[]realFieldsInYourClass=(Field[])allFieldsInAClass.get(ob);//整个过程
for(Field-fieldsInYourClass:realFieldsInYourClass){//this-shut现在抛出类中声明的所有字段,但它不。。。。
fieldsInYourClass.setAccessible(true);
System.out.println(fieldsInYourClass.getName());//据我所知,它抛出了自己的类和链接
//在这一点上,我不明白为什么它没有给我我想要的数据
}
}
}
}捕获(例外e){
e、 printStackTrace();
}}
输出:

我希望得到像test这样的值,因为我的示例类test有字段test(String)