English 中文(简体)
• 如何从贾瓦的班级名称获得班级目标
原标题:How to get a Class Object from the Class Name in Java

I know the class name, say "MyClass" and want to retrieve the Class object, ie. MyClass.class for future references. Is there a way to do that?

我通过这个网络研究,但我发现的与这个网络有关的大多数事情是关于<代码>ClassLoader的,我认为这并不适用于我的情况。 我不想开一个班子,但只能拿一个班子供今后使用。

EDIT:关于这方面的第一个答案:

我已经检查了()方法,但我认为,这一类别也应初步确定。 现在,我可以把全部论据和通过<代码>false,来谈谈第二点,但第三点是null,还是什么?

难道

Class.forName("MyClass", false, null);

事实上,我要做的是,取代一系列与<代码>有关的固定身份识别器。 类别:物体,有一系列的识别器,各式物体自动排出,以摆脱一些手工作业:

感谢迅速的答复,并对以前没有提到这一点表示担忧。

最佳回答

您可以使用:

Class c = Class.forName("com.package.MyClass");

其后即时发送物体:

Object obj = c.newInstance();

EDIT:这只是最简单的使用案例。 如评论意见所述,你需要考虑由初始化进程提出的构造论点和例外。 JavaDocs for newInstance 所有细节。

问题回答
Class.forName("MyClass")

http://java.sun.com/javase/6/docs/api/java/lang/Class.html#forName(java.lang.String) rel=“noreferer”

http://java.sun.com/javase/6/docs/api/java/lang/Class.html

值得注意的是,上述建议是正确的,但只能为违约(无分数)建筑商工作。 也可以做这样的事情:

    public Object newInstance(String className, Object...args) throws Exception {
        Class<?> clazz = Class.forName(className);
        if(args == null || args.length == 0) {
            return clazz.newInstance();
        }

        List<Class<?>> argTypes = new ArrayList<Class<?>>();
        for(Object object : args) {
            argTypes.add(object.getClass());
        }
        Constructor<?> explicitConstructor = clazz.getConstructor(argTypes.toArray(new Class[argTypes.size()]));
        return explicitConstructor.newInstance(args);
    }

如果你不想具体列出整个一揽子名称,请:

Class.forName("MyClass", true, this.getClass().getClassLoader());

只要你同时搬进,这一辛迪加允许你重新组织该项目,或将这一守则复制到另一个项目。 感谢Joachim ,提醒我阅读!

在 Java17年,你可以尝试这样做......只是给你找的班子和财产名称。

import org.springframework.beans.BeanUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

public class ObjectFinder {
    public static Object getObject(Object obj, String propertyName) {
        var objectFound = 
         Arrays.stream(BeanUtils.getPropertyDescriptors(obj.getClass()))
            .filter(pd->pd.getName().equals(propertyName))
            .findFirst();
    if(objectFound.isPresent()) {
        try {
            return objectFound.get().getReadMethod().invoke(obj);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    return null;
   } 
 }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签