我想在一个类中进行以下导入.
import org.eclipse.jdt.core.dom.*;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.ClassFile;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.Compiler;
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.text.edits.TextEdit;
Run Code Online (Sandbox Code Playgroud)
如何在Eclipse中导入JDT?干杯.
我需要在本地安装eclipse JDT.Core插件(例如,使用site.xml等下载文件),而不是由于工作中可怕的防火墙而远程安装它.任何人都可以建议在哪里下载它,以便我可以进行本地安装?谢谢
在下面的测试中,TesterClass对其两个类型参数之间的关系设置了约束.方法func2()似乎打破了这个约束,我希望它会在某处导致类型编译错误(在func2的定义中,或者每当类与String之外的任何第二个参数一起使用时),但它不会!
此外,如果我调用func2并将结果保存在适当类型的变量中,则编译失败(在该变量的类型上).但是做同样的事情并保存在更通用的类型(例如Object)中会成功,尽管事实上函数的返回类型在两种情况下都应该具有相同的类型(在向上转换之前).
这里发生了什么?
谢谢!
public class TestGenerics {
public static class ParamedType<T> {}
public class TesterClass<A extends ParamedType<B>, B> {
public TesterClass<A, B> func() {
return new TesterClass<A, B>();
}
public TesterClass<A, String> func2() {
return new TesterClass<A, String>();
}
}
public Object test() {
// How can I use these type parameters? Doesn't .func2 now have an invalid return type?
TesterClass<ParamedType<Integer>,Integer> testClass = new TesterClass<TestGenerics.ParamedType<Integer>, Integer>();
//TesterClass<ParamedType<String>, Integer> res2 = testClass.func2(); // <-- will not compile
Object res = …Run Code Online (Sandbox Code Playgroud) 给定eclipse中的ASTNode,有没有办法获得相应的源代码行号?
如何将FieldDeclaration(type:ASTNode)转换为IField(type:JavaElement).是否可以从FieldDeclaration ASTNode获取绑定,就像node.resolveBinding()和MethodDeclaration节点一样.
需要:我正在访问具有公共常量的类中的FieldDeclaration节点,并且想要在项目中搜索该字段的引用.我正在使用JDT的SearchEngine.为此,我想创建一个搜索模式,如下所示:
SearchPattern.createPattern(iField, IJavaSearchConstants.REFERENCES);
Run Code Online (Sandbox Code Playgroud)
我已将此问作为对我的一个问题的评论,但没有得到相同的答案.将其作为单独的问题发布.
在此先感谢您的回答.
回答Deepak的回答.
使用您的方法,我可以检索JavaElement,如下所示
List<VariableDeclarationFragment> fragments = node.fragments();
VariableDeclarationFragment fragment = fragments.get(0);
IJavaElement fieldElement = fragment.resolveBinding().getJavaElement();
Run Code Online (Sandbox Code Playgroud)
如果我传递此IJavaElement来创建搜索模式而不是IField,它将返回与IField相同的结果.
我正在尝试按照这篇文章尝试 eclipse jdt/ast 。
这是作为输入的java代码:
class Hello
{
int hello()
{
int a = 0, b = 3;
/* hello */
{
b = a * 3;
}
return a;
}
public static void main(String[] args)
{
int z = 0, i = 3;
/* hello */
{
i = z * 3;
}
}
}
Run Code Online (Sandbox Code Playgroud)
用ASTView,显示VariableDeclarationFragment有对应的绑定。

然而,在这个访问者代码中VariableDeclarationFragment node,我总是得到 4 个局部变量 (a,b,z,i) 的空值作为resolveBinding()返回值。
这有什么问题?我使用日食靛蓝。

这是获取 AST 的代码
private static CompilationUnit createCompilationUnit(String sourceFile) …Run Code Online (Sandbox Code Playgroud) 在重构某些遗留代码期间,需要创建我们自己的Eclipse Quick Fix对代码进行一些小的更正。在本文(德语)之后,这(本身)非常简单:http : //jaxenter.de/artikel/Eclipse-JDT-um-eigene-Quickfixes-erweitern
快速IQuickFixProcessor扩展()是通过扩展点添加的,扩展点org.eclipse.jdt.ui.quickFixProcessors创建了一个IJavaCompletionProposal要执行的工作。在IQuickFixProcessor有现成的AST做的代码更改。
我现在面临的问题是,我一次只能将快速修复应用于一个问题。如果我选择多个问题(所有问题都属于同一类型,因此适用我的自定义快速修复),则会出现错误“所选问题没有通用的适用快速修复”。
如何创建可用于同一类型的多个问题的快速修复?
org.eclipse.ui.ide.markerResolution对于Java源文件,使用Acanda建议的扩展点似乎很难实现。对于一个没有AST可用,只有一个IMarker实例。我如何获得AST CompilationUnit,并违规ASTNode为IMarker?
更笼统:JDT中是否有API桥可用于IMarker实例` ?
我正在构建静态分析工具的原型,我打算使用eclipse来完成繁重的工作.当我使用ASTVisitor访问声明时,如何检查对方法应用了哪些注释.我只对正在分析的类中的某些方法感兴趣,我正在考虑使用注释标记它们
例如,一个类Exam有一些带注释的方法.
@Override
public void add() {
int c=12;
}
Run Code Online (Sandbox Code Playgroud)
如何获取@Override使用注释的方法名称(添加)org.eclipse.jdt.core.IAnnotation?
我想在插件启动后立即显示一条消息.如果我将我的代码放在Activator.start()方法的末尾,我会收到一个错误(可能是因为尚未加载所需的资源).
错误看起来像这样:
!MESSAGE While loading class "de.stefansurkamp.package.ClassIWantToLoad", thread "Thread[main,6,main]" timed out waiting (5006ms) for thread "Thread[Worker-2,5,main]" to finish starting bundle "MyPlugin_1.0.0.qualifier [302]". To avoid deadlock, thread "Thread[main,6,main]" is proceeding but "de.stefansurkamp.package.ClassIWantToLoad" may not be fully initialized.
!STACK 0
org.osgi.framework.BundleException: State change in progress for bundle "reference:file:/C:/Users/Stefan/workspace/MyPlugin/" by thread "Worker-2".
at org.eclipse.osgi.framework.internal.core.AbstractBundle.beginStateChange(AbstractBundle.java:1088)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:298)
at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:478)
at org.eclipse.osgi.internal.loader.BundleLoader.setLazyTrigger(BundleLoader.java:263)
at org.eclipse.core.runtime.internal.adaptor.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:109)
[...]
Caused by: org.eclipse.osgi.framework.internal.core.AbstractBundle$BundleStatusException
... 53 more
Root exception:
org.eclipse.osgi.framework.internal.core.AbstractBundle$BundleStatusException
[...]
Run Code Online (Sandbox Code Playgroud)
如果需要,我可以提供完整的堆栈跟踪.
那么有没有任何方法在start()完成后立即运行?
编辑:使用作业和org.eclipse.ui.startup
根据greg的建议,我在一个由org.eclipse.ui.startup扩展点加载的类中创建了一个Job …