导入时如何防止Eclipse提供不推荐使用的类?

亚当·阿罗德(Adam Arold):

我在使用Eclipse时经常遇到问题考虑以下示例:

使用过时的类别组织进口

如您所见,我已经按下Ctrl+ Shift+ O我可以选择推荐使用的注解不推荐使用的注解。我的问题是,经常为我提供数十个类,而其中一半类已被弃用(一个完美的例子是JUnit Assert类)。

我的问题是在组织导入时如何使Eclipse忽略所有不推荐使用的类?

盖拉

当前Eclipse不提供这样的选项... 用于组织导入的Eclipse文档(Kepler版本)

但是,通过软糖您可以达到相同的结果...

Eclipse允许您提供要过滤掉的类/包的列表。为此,请导航至“首选项”>“类型过滤器”。

类型过滤器的首选项

我已经在我的环境中这样做了,以确保当我真的想要“ java.util.List”时不建议使用“ java.awt.List”。

您想要的是将所有不赞成使用的类添加到此列表中。

该列表在您的Eclipse工作空间首选项中维护...

File ... C:\Users\[YOUR_USER_NAME]\workspace\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.ui.prefs
Property ... org.eclipse.jdt.ui.typefilter.enabled=java.awt.List;

所需要做的只是创建一个不赞成使用的类的列表,并将其存储在此属性文件中。

Eclispe可以帮助创建此列表...对“已弃用”执行“ Java搜索”。 搜索不推荐使用的类

然后将结果按类型分组。 在此处输入图片说明

And copy the results using "Copy Qualified Name"

The results will contain Generics, and this should be removed. For example, "javafx.scene.control.Cell<T>" should read "javafx.scene.control.Cell".

In addition to containing deprecated classes, the results will also contain any class that has the word "Deprecated". This could be a comment or a method annotation. This list will need to be filtered to retain only deprecated classes.

The script below processes this class list to remove generics, and filtering out classes that are not deprecated (ie, only has method deprecation). The class list is read from a file named "DeprecatedClassList.txt". When it cannot check the class annotation, it skips the class and prints it out (for manual checking).

import java.lang.annotation.Annotation;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ConfigurationGenerator {

    public static void main(String[] args) throws Exception {

        List<String> cleanedList = Files
                .readAllLines(Paths.get("DeprecatedClassList.txt")).stream()
                .map(ConfigurationGenerator::removeGenerics)
                .filter(ConfigurationGenerator::hasDeprecatedConstructor)
                .collect(Collectors.toList());

        String propertyName = "org.eclipse.jdt.ui.typefilter.enabled=";
        String propertyValue = String.join(";", cleanedList).concat(";");

        String configuration = propertyName + propertyValue;
        System.out.println("Configuration property...");
        System.out.println(configuration);
    }

    public static String removeGenerics(String className) {
        int openingBracket = className.indexOf("<");
        if (openingBracket == -1)
            return className;
        else
            return className.substring(0, openingBracket);
    }

    public static boolean hasDeprecatedConstructor(String className) {

        Class theClass = null;
        try {
            theClass = Class.forName(className);
        } catch (Throwable e) {
            // Ignore bad results
            System.out.println("Skipping: " + className);
            return false;
        }

        Annotation[] annotations = theClass.getAnnotations();
        Optional<Annotation> deprecatedConstructor = Stream
                .of(annotations)
                .filter(annotation -> annotation.toString().equals(
                        "@java.lang.Deprecated()")).findAny();

        return deprecatedConstructor.isPresent();
    }
}

但是,这种方法存在一个问题。如果不存在不推荐使用的版本,则可能要使用不推荐使用的类。如果已故意隐藏了不推荐使用的类,则不会看到该类。要解决此问题,只需确保将它们从过滤器中排除即可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何替换不推荐使用的类方法?

如何在Ruby中将类标记为不推荐使用?

不推荐使用Polymer HTML导入

Eclipse:导入时使用双分号

替换不推荐使用的类LogStream

不推荐使用Springfox类参数

setDefaultOptions不推荐使用类OptionsResolverInterface

如何防止Eclipse进行愚蠢的自动导入?

禁止使用Java中不推荐的导入警告

如何将所有不推荐使用的类列出到文件中?

Android / Eclipse:如何在导入时更改项目名称?

ReflectionUtils如何替换不推荐使用的方法?

如何替换不推荐使用的jQuery函数?

如何安装不推荐使用的 uws 版本

Python:使用导入时找不到对类的引用

导入时强制转换类的名称,以防止出现“不是构造函数”错误

如何在不提供不推荐使用的功能的情况下在Linux / GCC上编译C代码?

不推荐使用的HTTP类Android lollipop 5.1

不推荐使用的springfox ClassOrApiAnnotationResourceGrouping类的替代方法是什么?

Android P中不推荐使用DialogFragment类

Spring Boot 1.4中不推荐使用ErrorPage类

Azure SDK 2.5中不推荐使用的类

stdlib.jar中不推荐使用的Java类

为什么不推荐使用从xml生成的类?

警告消息:如何删除不推荐使用的“withUnsafeMutableBytes”和不推荐使用的“withUnsafeBytes”?

xcode中不推荐使用的警告以及如何处理不推荐使用

Gulp不推荐使用-如何将某些任务标记为不推荐使用?

如何从Eclipse中的参考库导入类?

不推荐使用KeyPairGeneratorSpec