我正在尝试在非插件环境中使用AST解析器.代码编译,但我得到以下运行时错误:
org.eclipse.jdt.core中org.eclipse.jdt.core.dom.ASTParser.(ASTParser.java:189)中的线程"main"java.lang.NoClassDefFoundError:org/eclipse/core/resources/IResource中的异常. dom.ASTParser.newParser(ASTParser.java:118)
这是我正在运行的代码:
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.dom.*;
public class TestAST
{
private void runTest()
{
String helloStr ="\n"+
"public class HelloWorld {\n"+
"\n"+
" private String name=\"\"\n\n"+
" /**\n"+
" * \n"+
" */\n"+
" public void sayHello() {\n"+
" System.out.println(\"Hello \"+name+\"!\");\n"+
" }\n"+
"\n"+
"}";
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(helloStr.toCharArray());
parser.setResolveBindings(true);
ASTNode tree = parser.createAST(null);
tree.toString();
}
public static void main(String args[])
{
TestAST ast = new TestAST();
ast.runTest();
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会这样?
提前致谢,
雪莉
我正在开发一个插件,它接受实现特定接口(IDomain)的工作空间中的所有枚举解析代码(使用AST)对枚举进行一些修改并将其标记为使用注释(@IDomainInfo)处理.
例如,它需要像这样:
public
enum SomeEnum implements IDomain {
// ...
}
Run Code Online (Sandbox Code Playgroud)
并生成这样的东西:
public @IDomainInfo(domainId = 1)
enum SomeEnum implements IDomain {
// Some changes here...
}
Run Code Online (Sandbox Code Playgroud)
@IDomainInfo背后的想法是插件不再需要处理带注释的枚举.
基本上我完成任务的目的是使用JavaSearch API进行搜索以查找实现IDomain(简单任务)的所有枚举,结果我得到了IJavaElements列表(实际上是IType的实例).然后我调用一个迭代结果列表的方法,并创建一个未使用@IDomainInfo注释的所有IType实例的新列表,然后处理结果列表:对于每个未注释的IType做一些工作,用@注释IType IDomainInfo注释(使用AST)然后将结果保存回文件(使用IFile,所以我可以看到更改而不刷新,事实上,如果我在编辑器中打开枚举,我看到它立即刷新:-)
一切正常,但如果我打开一个@IDomainInfo带注释的枚举(仅用于测试),然后删除@IDomainInfo,保存文件(我确定)然后调用执行我之前描述的所有工作的操作,当我到达从非注释的过滤带注释的IType的部分时,代码是这样的:
for (IType type : typeList) {
IAnnotation annotation = type.getAnnotation(“IDomainInfo”);
if (!annotation.exists()) {
// The annotation does not exist, so add the type to the
// list of elements to update and go on...
ret.add(type);
continue;
}
// Something else here...
}
Run Code Online (Sandbox Code Playgroud)
好吧,结果是对于我刚刚保存的文件,IType检测到我刚删除的注释,就像它仍然存在一样.如果我关闭并重新打开eclipse,所有工作都会正常进行.
现在,我刚检查并三重检查了我的代码,所以我确定我没有使用注释版本保留未编辑的旧IType的旧版本(我的所有IType都来自每次新的java搜索调用我执行动作).
所以问题是,我可能做错了什么?我的意思是,我刚刚多次阅读JavaCore API来检查我是否可能使用它错了或者我是否有一些概念上的缺陷但我真的没有线索,就像eclipse会缓存IType忽略变化我刚刚在编辑中做出: - …
你如何在Eclipse中打破(暂停)Java程序?
我不是在谈论断点.我在谈论随机暂停程序而不知道它当前正在执行什么,就像在Visual Studio中一样.
我正在寻找将org.eclipse.jdt.core.dom.ITypeBinding实例转换为实例的一般方法org.eclipse.jdt.core.dom.Type.虽然我觉得应该有一些API调用来做到这一点,但我找不到一个.
根据具体类型,似乎有多种方法可以手动执行此操作.
ITypeBinding如果Type没有所有这些特殊情况,是否有任何一般方法可以获得并得到一个?拿a String和返回a Type也是可以接受的.
更新
从目前为止的反应来看,似乎我必须处理所有这些特殊情况.这是第一次尝试这样做.我确信这不是完全正确的,所以请仔细审查:
public static Type typeFromBinding(AST ast, ITypeBinding typeBinding) {
if( ast == null )
throw new NullPointerException("ast is null");
if( typeBinding == null )
throw new NullPointerException("typeBinding is null");
if( typeBinding.isPrimitive() ) {
return ast.newPrimitiveType(
PrimitiveType.toCode(typeBinding.getName()));
}
if( typeBinding.isCapture() ) {
ITypeBinding wildCard = typeBinding.getWildcard();
WildcardType capType = ast.newWildcardType();
ITypeBinding bound = wildCard.getBound();
if( bound != null ) {
capType.setBound(typeFromBinding(ast, bound)),
wildCard.isUpperbound());
}
return capType; …Run Code Online (Sandbox Code Playgroud) 使用Eclise JDT,我需要检索任何ASTNode的子代。我可以在某处使用实用程序方法吗?
我现在唯一想到的方法是子类化ASTVisitor并手动处理每种节点以找到其子节点。但是研究每种节点类型需要大量工作。
首先,一点背景(如果不感兴趣,可以跳过一点).我很生气,很困惑!这应该是一个非常简单的用例,事实上我的代码已经用Eclipse JDT编译器编译得很好,所以直到现在我一直在配置Maven以确保这样做.尽管它不能用Oracle JDK和OpenJDK编译,但是我一直在困扰我,因为我认为它可能实际上是我的代码的问题,所以我再次研究它.
我想也许这个bug是在JDT编译器中允许它编译的,而不是Oracle JDK和OpenJDK因为不允许它,我也用它来测试这两个.有问题的原始代码要复杂得多,所以我很难看到问题出在哪里,事实上,我很惊讶地发现在不编译的情况下可以减少这个问题的程度.
Eclipse JDT编译器或Oracle JDK和OpenJDK都有一个非常重要的(imho)错误.
这是相关代码的相当小的表示.(Anything的类型绑定可以被任何接口替换,编译器行为不会改变):
public class Bug<X extends Property<?, ?> & Anything> {
}
interface Property<C, S extends C> extends PropertyConst<C> {
@Override
public S get();
}
interface PropertyConst<C> {
public C get();
}
interface Anything {
}
Run Code Online (Sandbox Code Playgroud)
总而言之,我认为这应该编译得很好,但Oracle JDK 7&8和OpenJDK 7不同意.它使用Eclipse Juno为我编译.
当使用这些编译器中的任何一个编译时,上面的代码给出类似于以下错误的东西,但是对于JDT编译器工作得很好:
Bug.java:3: error: types PropertyConst<?> and Property<?,?> are incompatible; both define get(), but with unrelated return types
public class Bug<X extends Property<?, ?> & Anything> {
^
1 …Run Code Online (Sandbox Code Playgroud) 每次我点击Save整个文件的格式在这两种格式之间交替:
this.getObject()
.method()
.method();
this.method(arg1, arg2,
arg3, arg4);
Run Code Online (Sandbox Code Playgroud)
和
this.getObject()
.method()
.method();
this.method(arg1, arg2,
arg3, arg4);
Run Code Online (Sandbox Code Playgroud)
我希望它坚持第一种格式.
当使用基于注释的空分析时,我会得到以下(令人困惑的)警告,其中涉及一个数组:
Null type safety (type annotations): The expression of type 'int[]' needs unchecked conversion to conform to 'int @Nullable[]'
当我将未注释int[]的int @Nullable[]参数传递给参数时会发生这种情况.
这是令人惊讶的.通常只有一个问题,如果你取一个可空的值并尝试将它传递给一个非空参数,但我正在做相反的事情 - 采用一个已知的非null(虽然没有注释)数组并将其传递给一个方法(Arrays.equals)确实接受空值.
此外,它似乎不是非数组对象的问题.通常,T可以将(未注释的,非数组)类型的变量或返回分配给a @Nullable T.
所以我的问题是为什么在T数组类型时这会改变?
我的类是一个可清除/可比较的代理,用于使用int[](取自C++函数)作为唯一标识符的本机类.我的包使用基于注释的空分析,而第三方包(使用本机类)则不使用.
这是一个显示问题的简化版本:
TagKey.java:
package example;
import java.util.Arrays;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import other.Tag;
import com.google.common.collect.Interner;
import com.google.common.collect.Interners;
public class TagKey {
private TagKey(Tag tag) {
this.tag = tag;
}
public static TagKey forTag(Tag tag) {
TagKey candidateKey …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置我的eclipse插件开发目标并多次收到以下错误:
!ENTRY org.eclipse.e4.ui.css.swt 4 0 2015-01-30 15:15:06.879
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.BundleException: Could not resolve module: org.eclipse.e4.ui.css.swt [955]
Unresolved requirement: Require-Bundle: org.eclipse.e4.ui.css.core; bundle-version="0.9.0"
-> Bundle-SymbolicName: org.eclipse.e4.ui.css.core; bundle-version="0.10.100.v20140424-2042"; singleton:="true"
org.eclipse.e4.ui.css.core [1134]
Unresolved requirement: Require-Bundle: org.apache.batik.css; bundle-version="1.7.0"
-> Bundle-SymbolicName: org.apache.batik.css; bundle-version="1.7.0.v201011041433"
org.apache.batik.css [1177]
Unresolved requirement: Import-Package: org.w3c.dom.events; version="[3.0.0,4.0.0)"
Run Code Online (Sandbox Code Playgroud)
这是否意味着这些插件都无法解析,或者只是不是最后一个插件,或者org.w3c.dom.events这是由于捆绑版本的不匹配造成的org.eclipse.e4.ui.css.core?
在Eclipse IDE上编写Java代码时,按Control + Space将弹出内容辅助窗口.
例如,内容辅助窗口System.将列出类System的所有可用字段和方法.
我需要通过代码访问内容辅助窗口的"数据模型".
使用上面的例子,它是:给定类名System,我如何检索所有可用的字段和方法?
我花了一些时间在grepcode.com上这三个类的源代码:
org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext
org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposalComputer
org.eclipse.jdt.ui.text.java.CompletionProposalCollector
Run Code Online (Sandbox Code Playgroud)
看起来像一个ICompilationUnit实例用于提供字段和方法名称.
那我不明白如何为ICompilationUnitjre系统库或第三方库中的类生成实例?或者,如果我没有以正确的方式读取代码,那么程序如何查找字段和方法名称?(我不需要担心偏移和UI的东西,只需要担心"数据模型"部分).
eclipse-jdt ×10
java ×9
eclipse ×6
annotations ×1
break ×1
coding-style ×1
compiler-bug ×1
debugging ×1
eclipse-rcp ×1
formatter ×1
generics ×1
nullable ×1
openjdk ×1
osgi-bundle ×1