通过从注释属性获取的路径实例化对象

发布于 2024-12-12 04:19:09 字数 656 浏览 5 评论 0原文

1. 有以下注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Trackable {
    String builder();
}

2. 此注释的用法:

@Trackable(builder = "pkg1.SomeVO")
public class MyService

3. pkg1.SomeVO - 是路径Java 对象,应该在我的方面类中进一步实例化。

4. 我得到了 build 的字符串值,它等于来自反射的“pkg1.SomeVO”。

问题是,如何实例化 SomeVO 对象?
我需要这样:
MyBuilder mb=new SomeVO(); 其中MyBuilder是抽象类,已经定义。

它可能是任何对象,例如 SomeVO2 等,所以我绝对不知道我的方面(参见步骤 3),应该实例化什么类。

1. Have following annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Trackable {
    String builder();
}

2. usage of this annotation:

@Trackable(builder = "pkg1.SomeVO")
public class MyService

3. pkg1.SomeVO - is path to the Java object,that should be instantiated further in my aspect class.

4. I've got String value of build,that is equals to 'pkg1.SomeVO' from reflection.

The question is,how actually to instantiate SomeVO object?
I need to it like:
MyBuilder mb=new SomeVO();
where MyBuilder is abstract class,already defined.

It may be any object,e. g. SomeVO2 etc.,so I definitely doesn't know in my aspect(see step 3.),what class should be instantiated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断桥再见 2024-12-19 04:19:09

执行类似的操作来获取注释值并创建类。此外,您可能希望在注释中使用默认值字段和 Class 而不是 String 以使事情变得更容易。

for (Method m : MyService.class.getDelcaredMethods())
  if (m.getAnnotation(Trackable.class) != null) {
      String className = m.getAnnotation(Trackable.class).builder()
      Class.forName(className).newInstance();
  }

如果您的类没有默认构造函数,您需要弄清楚它需要什么参数:

for (Constructor c : Class.forName(className).getConstructors())
   if (c.getParameterTypes() == /* your expected arg types */)
       return c.newInstance(/* your args */);

为了确保您的类属于某种类型(例如 MyBuilder),您可以将注释设置为:

public @interface Trackable {
   Class<? extends MyBuilder> value()
}

Do something like this to get the annotation value and create the class. Also, you may want to use the default value field and a Class instead of a String in your annotations to make things easier.

for (Method m : MyService.class.getDelcaredMethods())
  if (m.getAnnotation(Trackable.class) != null) {
      String className = m.getAnnotation(Trackable.class).builder()
      Class.forName(className).newInstance();
  }

If your class has no default constructor you need to figure out what args it takes:

for (Constructor c : Class.forName(className).getConstructors())
   if (c.getParameterTypes() == /* your expected arg types */)
       return c.newInstance(/* your args */);

To ensure your class is of a certain type (e.g. MyBuilder) you can make your annotation be:

public @interface Trackable {
   Class<? extends MyBuilder> value()
}
你的心境我的脸 2024-12-19 04:19:09

假设路径是指您的类所在的包(希望位于类路径上)。

String myClass = getClassNameFromAnnotationReflectively(); // YOU define this method...
Class<SomeVO> someVoClass = Class.forName(myClass);
SomeVO someVo = someVoClass.newInstance();

如果您的 class 没有默认构造函数,您可以使用 getConstructor(Class ...) (您在哪里传入构造函数的参数类型正在寻找),然后调用 newInstance(Object...) (传入实际值)。

对象。我建议看一下。

Assuming by path, you mean the package your class is in (which is hopefully on the classpath).

String myClass = getClassNameFromAnnotationReflectively(); // YOU define this method...
Class<SomeVO> someVoClass = Class.forName(myClass);
SomeVO someVo = someVoClass.newInstance();

If your class doesn't have a default constructor, you can find the proper one using getConstructor(Class<?> ...) (where you pass in the types of the arguments of the constructor you are looking for), and then calling newInstance(Object...) on that (passing in the actual values).

There are a lot of fun methods for use on the Class object. I recommend taking a look.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文