Ada*_*old 16 java eclipse import java-7 eclipse-kepler
我有一个使用Eclipse的反复出现的问题.请考虑以下示例:
你可以看到我按了Ctrl+ Shift+ O.我可以从已弃用和未弃用的注释中进行选择.我的问题是我经常提供几十个类,其中一半被弃用(一个完美的例子是JUnit Assert
类).
我的问题是如何在组织导入时使Eclipse忽略所有已弃用的类?
gah*_*rae 13
目前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;
Run Code Online (Sandbox Code Playgroud)
所需的只是您创建一个已弃用的类列表,并将其存储在此属性文件中.
Eclispe可以帮助创建此列表...执行"弃用"的"Java搜索".
然后按类型对结果进行分组.
并使用"复制合格名称"复制结果
结果将包含泛型,这应该被删除.例如,"javafx.scene.control.Cell <T>"应为"javafx.scene.control.Cell".
除了包含已弃用的类之外,结果还将包含具有"Deprecated"一词的任何类.这可以是注释或方法注释.需要过滤此列表以仅保留已弃用的类.
下面的脚本处理此类列表以删除泛型,并过滤掉未弃用的类(即,只有方法弃用).从名为"DeprecatedClassList.txt"的文件中读取类列表.当它无法检查类注释时,它会跳过该类并将其打印出来(用于手动检查).
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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是这种方法存在一个问题.如果不推荐使用不推荐的版本,您可能希望使用已弃用的类.如果有目的地隐藏它,您将看不到已弃用的类.要解决此问题,请务必将其从过滤器中排除.
归档时间: |
|
查看次数: |
1069 次 |
最近记录: |