如何从代码完成中隐藏所有 java.lang.Object 方法?
我想为现有的类库创建一些简单的包装类。 为了使语法易于阅读且易于猜测(通过代码完成),我想删除 java.lang.Object 的方法。
问题是java中的所有非原子事物都继承自Object,因此具有这些方法。 我已经尝试通过枚举和接口创建包装器语法,但没有成功; 因为枚举和接口也是java.lang.Objects。
java.lang.Object 有九个我不想在接口的代码完成中看到的方法。 这是我想要删除的内容(红色)和我想要保留的内容(绿色):
alt text http ://ju.venile.de/upload/java-lang-object-methods.png
下面是一些示例代码,用于围绕现有类创建漂亮的包装器(构建器模式):
public interface IMySyntax{
public IMySyntax myMethod1();
public IMySyntax myMethod2();
}
public class MyBuilder implements IMySyntax{
public static IMySyntax build(){ return (IMySyntax) new MyBuilder() }
public IMySyntax myMethod1(){ /* do something */ return (IMySyntax) this }
public IMySyntax myMethod2(){ /* do something */ return (IMySyntax) this }
}
新包装器代码的用法应如下所示this:
MyBuilder.build()
.myMethod1()
.myMethod2();
将构建器的所有 this
语句强制转换为接口将降低方法的可见性,例如,如果构建器实现多个接口。 不幸的是,所有 java.lang.Object 方法都将保留。
如果这种方法隐藏在 Java 中是可能的(也许使用注释?),我可以创建一个与 IDE 无关的漂亮库(到处都有很好的代码完成)。 如果没有,那么至少 Eclipse IDE(也许是一个插件?)可能有一个技巧可以提供 java.lang.Object 方法隐藏。
i want to create some simple wrapper classes for an existing class library. To make the syntax nice to read AND nice to guess (via code completion) i'd like to remove the methods of java.lang.Object.
The problem is that all non-atomic things in java inherit from Object and thus have these methods. I already tried to create the wrapper syntax via enums and interfaces, without success; because enums and interfaces are also java.lang.Objects.
java.lang.Object has nine methods that i don't want to see in the code completion of my interfaces. Here's what i want to remove (red) and what i want to keep (green):
alt text http://ju.venile.de/upload/java-lang-object-methods.png
Here is some example code for creating nice wrappers around existing classes (Builder pattern):
public interface IMySyntax{
public IMySyntax myMethod1();
public IMySyntax myMethod2();
}
public class MyBuilder implements IMySyntax{
public static IMySyntax build(){ return (IMySyntax) new MyBuilder() }
public IMySyntax myMethod1(){ /* do something */ return (IMySyntax) this }
public IMySyntax myMethod2(){ /* do something */ return (IMySyntax) this }
}
Usage of the new wrapper code should look like this:
MyBuilder.build()
.myMethod1()
.myMethod2();
Casting all this
statements of the builder to an interface will reduce the method visibility, e.g. if the builder implements more that one interface. All java.lang.Object methods will stay, unfortunately.
If this method hiding was possible in Java (maybe using Annotations?), i could create a nice library that is IDE agnostic (nice code completion everywhere). If not, than maybe there's a trick for at least the Eclipse IDE (maybe a plugin?) that can provide java.lang.Object method hiding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 Eclipse 3.4 至少您可以执行以下操作:
1) 转到 Preferences -> Java-> 外观-> 类型过滤器
2)点击添加,然后输入
java.lang.Object
现在在代码辅助中,直接从
java.lang.Object
继承的方法将会消失For Eclipse 3.4 at least you can do the following:
1) Go to Preferences -> Java -> Appearance -> Type Filters
2) Click Add, and enter
java.lang.Object
Now in code assist, the methods inherited directly from
java.lang.Object
will dissapear