标签: reflection

在Java中使用Reflections时出错

我在此代码中遇到了转换问题,但不知道如何纠正它.

public void showFrame(String className, Object controller) throws Exception{
    try {
       Class c = Class.forName("com." + className);
       // "(Object.class)" I want this to be of type held in className var
       // this type will be same as one passed into  "controller" at runtime
       Constructor ctr = c.getConstructor(Object.class);  
       ctr.newInstance(controller);
    } catch (ClassNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "Error" );
    }
}
Run Code Online (Sandbox Code Playgroud)

java reflection casting

-2
推荐指数
1
解决办法
79
查看次数

为什么通过接口通过反射调用方法要快得多

为什么通过反射调用方法比创建一个接口然后通过反射调用它要慢得多.第一个版本显示了其他版本显示增强方式的繁琐方式?

 // first version
  class A
    {
        public void fn()
        { 
        }
    }
  void Main(String[]x)
  {
        Type type = typeof(A);
        object obj = Activator.CreateInstance(type);
        type.InvokeMember("fn", BindingFlags.Public, null, obj, null);
  }

  //second verison
   interface IA
    {
        void fn();
    }

    class A :IA
    {
        public void fn()
        {
        }
    }

 void Main(String []x)
 {
        Type type = typeof(A);
        IA obj =(IA) Activator.CreateInstance(type);
        obj.fn();
 }
Run Code Online (Sandbox Code Playgroud)

c# reflection interface

-2
推荐指数
1
解决办法
380
查看次数

铸造和反射C#

我正在尝试使用反射将Object转换为其类型.

所以我想做一些事情,比如使用typeOf()方法找到它的类型,然后动态地将对象转换为它的found类型.

我不知道如何使用linq很好地做到这一点?

我使用的是c#4.0.

谢谢.

.net c# reflection c#-4.0

-2
推荐指数
1
解决办法
263
查看次数

如何防止JFrame关闭仍然无法正常工作

我正在测试问题的解决方案

在这种情况下,JVM或反射的新实例是否有用

我遇到的是,如果我尝试调用诸如房地产游戏之类的应用程序,则此解决方案不起作用.因此,在调用main之后只需添加以下代码即可 Applic2

        Frame[] f2 = JFrame.getFrames();
        for(Frame fx: f2){


              if (fx instanceof JFrame) {
                  JFrame aFrame = (JFrame)fx;

                  aFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

              }
            }
Run Code Online (Sandbox Code Playgroud)

我创建了一个异步线程,不断将操作JFrame.EXIT_ON_CLOSE更改为JFrame.DISPOSE_ON_CLOSE,如下所示

import java.awt.Frame;

import javax.swing.JFrame;

public class FrameMonitor extends Thread{

    @Override
    public void run(){

        while(true){
            Frame[] f2 = JFrame.getFrames();

            for(Frame fx : f2){

                if(fx instanceof JFrame){
                    JFrame aframe =(JFrame)fx;
                    aframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并通过MyApp类中的start方法调用此线程实例.但解决方案无效.我仍然面临关闭一帧时所有帧关闭的相同问题.为什么会发生任何建议以及如何克服这个?

请仔细检查以下问题:

让我更详细地介绍这个问题

房地产游戏代码添加到工作区

将以下包添加到RealEstate代码中

package MyApplication;

import java.awt.Frame;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import …
Run Code Online (Sandbox Code Playgroud)

java reflection swing jframe

-2
推荐指数
1
解决办法
592
查看次数

Java中的反射api

我需要解释以下问题:

  • java中的反射是什么?
  • 在什么情况下我需要使用反射?
  • 需要使用反射的实时场景和示例?

我在反思中很困惑。我已经阅读了很多文件,但我仍然感到困惑。请给我一个我能完全理解的解释。

java reflection

-2
推荐指数
1
解决办法
2136
查看次数

java reflection constructor.newInstance给出"错误的参数个数"

如何解决我的代码?

package mypackage;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class testReflection {
    public class You {
        public You(String s) {
        }

        public void f(String s, int i) {
            System.out.println(i + 100);
        }
    }

    public static void main(String[] args) throws NoSuchMethodException {
        Constructor constructor =
                You.class.getConstructor(testReflection.class, String.class);
        try {
            You y = (You)constructor.newInstance("xzy");//Exception!!!!
            System.out.println("ok");
            y.f("xyz",2);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

异常消息是:

java.lang.IllegalArgumentException: wrong number of arguments
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) …
Run Code Online (Sandbox Code Playgroud)

java reflection constructor

-2
推荐指数
1
解决办法
325
查看次数

How to get all members on a type elegantly?

I've been through "Questions that may already have your answer" and didn't find the thing I'm looking for. I want to get all members on a type via reflection. When I try

var type = typeof(int);
var members = type.GetMembers();
for(var i = 0; i < members.Length; i++)
    Console.WriteLine($"{i, 2} {members[i]}");
Run Code Online (Sandbox Code Playgroud)

I get 19 members.

I found that some members need specific BindingFlags. Since I don't know those flags and different members have different flags I pass all …

c# reflection

-2
推荐指数
1
解决办法
70
查看次数

如何使反射在 JDK 16 及更高版本上工作?

我将以下遗留代码迁移到 Java 16,但是由于这个新版本引入了强封装,它不起作用:

try {
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    method.setAccessible(true);
    method.invoke(new URLClassLoader(
        new URL[] {}),
        new File("C:/external-folder/my.jar").toURI().toURL()
    );
} catch (Exception exc) {
    exc.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

有办法让它发挥作用吗?

java reflection java-16 java-17

-2
推荐指数
2
解决办法
1万
查看次数

为什么Java中无法通过反射构造私有对象

我明明把构造函数的setAccessible设置为true了,可是为什么还是报错!你能告诉我如何修复它吗

public class Person {
    // fields...

    private Person(String name, int age, String address, int num) {
        this.name = name;
        this.age = age;
        this.address = address;
        this.num = num;
        System.out.println("private full-constructor be created");
    }

}
Run Code Online (Sandbox Code Playgroud)

// 发生错误?!为什么?

public static void operatePrivateConstructorFullParameter(Class<? extends Person> clzz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Constructor<? extends Person> constructor = clzz.getConstructor(String.class, int.class, String.class, int.class);
    constructor.setAccessible(true);

    Person person = constructor.newInstance("Miao", 18, "MOON", 88);
}


public static void main(String[] args) throws Exception {
    Class<? extends Person> clzz …
Run Code Online (Sandbox Code Playgroud)

java reflection parameterized-types

-2
推荐指数
1
解决办法
48
查看次数

我如何计算为Java类定义的int成员数?

我有一个存储大量int成员变量的类,每个变量定义如下:

public final static int int1 = int1value;
public final static int int2 = int2value;
...
public final static int int106 = int106value;
Run Code Online (Sandbox Code Playgroud)

还有第二个类需要根据int第一个类中的s 数进行循环.我如何向第一个类添加一个函数来计算这些成员变量?

java reflection variables

-3
推荐指数
1
解决办法
2571
查看次数