删除子类中的注释?

发布于 2024-11-16 21:45:15 字数 489 浏览 0 评论 0原文

我有一个子类,需要一个注释,在删除的父类中声明。最好的方法是什么?

public class Parent

@MyAnnoation
String foobar;

}


public class Child extends Parent {
//here I only want to remove the @MyAnnotation from String foobar;

}

我想避免的是,“覆盖”成员

 public class Child extends Parent {

 String foobar;

 }

,因为这有几个缺点(因为它“覆盖”底层父成员= this.foobar 可能与 super.foobar 不同)...

1.)有没有一种简单的方法从子类(这里是子类)的父类中删除注释?

2.) 删除子类中注释的官方方法是什么?

非常感谢!马库斯

i have a child Class, that needs an Annotation, that is declared in the Parent class removed. What is the best way to do this?

public class Parent

@MyAnnoation
String foobar;

}


public class Child extends Parent {
//here I only want to remove the @MyAnnotation from String foobar;

}

What I want to avoid is, "overriding" the member

 public class Child extends Parent {

 String foobar;

 }

as this has several disadvantages (as it "covers" the underlaying parent member = this.foobar can be different to super.foobar)...

1.) Is there an easy way of removing an annotation from a Parent Class in the Subclass (here Child)?

2.) What is the official way to remove annotations in a Subclass?

Thanks very much! Markus

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

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

发布评论

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

评论(2

梦冥 2024-11-23 21:45:15

如果你想做这样的事情,那么你可能应该再看看你的设计。

我认为,如果有一种方法可以删除运行时注释,那么不推荐这样做,因为您将删除类定义的一部分。例如删除成员访问修饰符。

这可能会对您的逻辑产生意外的结果,因为其他代码可能依赖这些定义来做出运行时决策。

如果您需要在运行时修改类定义,那么这个类可能不应该首先以这种方式定义。

If you want to do something like this than you probably should have another look at your design.

I think that if there is a way to remove a runtime annotation than that would not be recommended, because you would be remove a part of a class definition. Like removing a member access modifier for instance.

That can have unexpected results on your logic since other code might rely on these definitions to make runtime decisions.

If you need to modify a class definition during runtime than this class should probably not be defined in such a way in the first place.

掀纱窥君容 2024-11-23 21:45:15

据我所知,您无法删除注释。但这可能会有所帮助:

  1. 并非所有注释在运行时都可用。因此,在这种情况下显然没有理由删除它们。只有带有 @Retention(RUNTIME) 的注释在运行时可用。 (例如,@Nullable 注释在运行时不可用,但 @XmlElement 可以。)。

  2. 仅适用于类级注释,还有另一件事需要考虑。并非所有注释都会自动继承到子类。这仅适用于具有自己的 @Inherited 注释的注释。

  3. 通过重新声明不同的注释,某些注释可能会产生矛盾。 (例如,具有 @XmlAccessorType(FIELD) 的父类可以具有具有 @XmlAccessorType(PUBLIC) 的子类。)

因此,删除是不可能的,但重新定义/替换是可以的。 实际上,这意味着可以通过将某些注释设置为其默认值 @MyAnnotation(NONE)@MyAnnotation(DEFAULT) 来“删除”它们的效果或 @NotMyAnnotation 因此,这意味着它实际上取决于您正在处理的注释类型。

还有一件事:尽管您可以在子类上重新定义注释,但它仍然并不总是能达到预期的效果。 (例如,在@XmlElement的情况下,有时父方法上的注释在执行期间仍然具有优先权)。很高兴知道某些框架(例如持久性)支持替代 xml 绑定文件作为 java 类注释的替代方案。

To my knowledge you can't remove annotations. But this may help:

  1. Not all annotations are available at runtime. So, in that case obviously there's no reason to remove them. Only annotations with a @Retention(RUNTIME) are available at runtime. (e.g. @Nullable annotations are not available at runtime, but @XmlElement is.).

  2. Only for class-level annotations, there's another thing to take in account. Not all annotations are inherited automatically to child classes. This is only the case for annotations which have an @Inherited annotation of their own.

  3. Some annotations can be contradicted by redeclaring a different annotation. (e.g. a parent class with @XmlAccessorType(FIELD) can have a child class with @XmlAccessorType(PUBLIC).)

So, removing is impossible, but redefining/replacing is. In practice that means that the effect of some annotations can be "removed" by setting them to their default value @MyAnnotation(NONE) or @MyAnnotation(DEFAULT) or @NotMyAnnotation. So, that means it really depends what kind of annotations you are dealing with.

One more thing: Even though you can redefine annotations on child classes, still it doesn't always give the desired effect. (e.g. In the case of @XmlElement sometimes the annotation on the parent method still has priority during execution). It's good to know that some frameworks (e.g. persistence) support alternative xml binding files as an alternative to java class annotations).

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