我在使用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] 删除。
我来说两句