简体   繁体   English

如何在Eclipse中自定义和触发Java编译错误

[英]How to customize and trigger Java compilation error in Eclipse

I want to define and trigger some customized Java compilation errors in Eclipse . 我想在Eclipse中定义并触发一些自定义的Java编译错误。 Basically I want that some system calls in some methods trigger a compilation error . 基本上,我希望某些方法中的某些系统调用触发编译错误

In this discussion an Annotaion based solution is proposed but it is not suitable in my case because I cannot predict when the copilation errors will happen. 此讨论中 ,提出了一种基于Annotaion的解决方案,但由于我无法预测何时会发生编译错误,因此该解决方案不适用于我的情况。 All I want is that when a developer makes a System Call for example a compilation error will be triggered. 我想要的是,例如,当开发人员进行系统调用时,将触发编译错误。

Not exactly what you look for, but consider using a custom Checkstyle definition. 不完全是您要查找的内容,而是考虑使用自定义Checkstyle定义。 Here is a sample from the checkstyle documentation that marks calls to System.out.println as invalid: 这是来自checkstyle文档的示例,该示例将对System.out.println调用标记为无效:

<module name="Regexp">
    <!-- . matches any character, so we need to escape it and use \. to match dots. -->
    <property name="format" value="System\.out\.println"/>
    <property name="illegalPattern" value="true"/>
</module>

The only way to accomplish something like that would be to write a plug-in for Eclipse that analyzes code during builds. 完成类似任务的唯一方法是为Eclipse编写一个在构建过程中分析代码的插件。 But the details of doing so is really out of the scope of a StackOverflow question. 但是这样做的细节确实超出了StackOverflow问题的范围。

You need to create your Builder implementation and register it with the Java Project. 您需要创建您的Builder实施,并将其注册到Java Project中。

You could start here . 你可以从这里开始

If you want to prevent developpers from using java.lang.System for example, a nice trick is to create a custom class java.lang.System in your projet, so every call to any methods of java.lang.System (the real one) will occure a compilation problem. 例如,如果要防止开发人员使用java.lang.System,一个不错的技巧是在projet中创建一个自定义类java.lang.System ,因此每次调用java.lang.System的任何方法(真正的方法是)会发生编译问题。

If you use maven, you can also create the class in a seperate maven project with scope provided to not be packaged with the real application. 如果使用maven,则还可以在单​​独的maven项目中创建类,其提供的范围不与实际应用程序打包在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM