我正在开发一个Eclipse插件,用于修改用户项目中的Java代码.
基本上这个插件的结果是Java注释被添加到某些方法中,所以
void foo() { ... }
Run Code Online (Sandbox Code Playgroud)
变
@MyAnnotation
void foo() { ... }
Run Code Online (Sandbox Code Playgroud)
除了它看起来不那么像; 新插入的注释的缩进是wack(具体来说,新注释一直到行的左侧).我想对文件进行所有更改,然后以编程方式调用"Correct Indentation".
有谁知道如何做到这一点?我在这里或在JDT论坛中找不到答案,所有看起来相关的类(IndentAction,JavaIndenter)都在内部包中,我不应该使用...
谢谢!
我对使用Eclipse JDT创建CAPTURE绑定感兴趣.
我已经阅读了几个捕获转换教程,但是当我复制粘贴示例代码片段时,我永远无法在抽象语法树中找到捕获转换绑定(使用插件ASTView来可视化AST).
如何实现这一目标?
刚刚发现这一点,同时阅读eclipse JDT的文档:
IMethodBinding.getParameterTypes():...注意:结果不包括内部类仿真引入的合成参数.
我在JLS中找不到任何对内部类仿真的引用......任何人都知道这个仿真是什么?举一个例子,也会有所帮助.:)
当我在Eclipse Juno中创建一个新类并自动添加main方法时,我得到以下内容:
public class Example {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Run Code Online (Sandbox Code Playgroud)
我想编辑这个方法模板来添加throws Exception.
我尝试在Preferences> Java> Editor> Templates>"main"中编辑模板,但这不会影响上面的场景.相反,这会配置我输入"main"时插入的代码并按Ctrl+ Space.
这可能吗?
我使用JDT来编译我的java类.BatchCompiler返回一个字符串,但我需要一系列问题/错误及其列和行信息.compiler.compile(单位); 将错误打印到其printwriter,compiler.resolve(unit)正是我想要的,但它只能编译一个java文件.
我用这种方式创建了一个编译器对象:
Compiler compiler = new Compiler(env, DefaultErrorHandlingPolicies.exitAfterAllProblems(), new CompilerOptions(), requestor, new DefaultProblemFactory());
Run Code Online (Sandbox Code Playgroud)
并创建包含文件名和文件内容的CompilationUnits到编译器.
CompilationUnit[] units = project.toCompilationUnit();
Run Code Online (Sandbox Code Playgroud)
AFAIK,有两种编译方式,其中一种是返回void并将错误和问题打印到其PrintWriter的编译(单位)方法,因为它不返回对我没用的列信息.另一种方法是resolve(unit)方法,但它只能使用一个CompilationUnit.
compiler.resolve(units[index], true, true, true);
Run Code Online (Sandbox Code Playgroud)
有谁知道如何以编程方式使用JDT编译器来编译多个文件?
有时,函数在某些情况下返回null,但是使用它们的人却没有意识到它,因此NPE是不可避免的.
例如(这只是一个例子,显示我在说什么):
public class Test
{
public static void main(final String[] args)
{
final int t=0;
System.out.println("result:"+foo(t).toString()); // no warning here...
}
public static String foo(final int t)
{
if(t>0)
return "positive";
else return null;
}
}
Run Code Online (Sandbox Code Playgroud)
Eclipse没有警告这样的事情,它甚至没有在设置上.
只有在没有涉及功能时才能检测到这些问题(参见下面的编辑).
不仅如此,我找不到一个注释,说明返回的值可以为null.我认为它有相反的结果.
所以我认为既然Eclipse没有一些检查(并且没问题),我可以使用替代静态代码分析工具,如FindBugs和CodePro Analytix.两者都未能找到这样的错误.
我试图联系这两个项目,但还没有得到任何答案.对于CodePro,我甚至不确定我是否做得对...事实上,我认为这个项目已经停止(2010年的最新更新......).
是否有任何工具可以帮助避免此类错误?
编辑:因为许多人认为只需更改代码就可以解决这个问题,所以考虑在一个庞大的团队中工作,有些人可能会忘记它(每个人都会犯错误).或者你甚至可能正在开发一个使用SDK(甚至可能是封闭源代码)的项目,它可以在某些情况下返回null,但是没有记录,因为SDK的创建者忘记了它.
无论你作为程序员有多好,都会发生这样的事情.
我要求的是通过让日食帮助来克服这个问题.它应该能够警告我,至少在我写在这里的基本例子上.
Eclipse应该能够进行这样的检查,就像它可以警告我这个例子一样:
int t=0;
--t;
String s="s";
if(t<0)
s=null;
System.out.println(s.toString()); // here you will get a warning
Run Code Online (Sandbox Code Playgroud)
在Eclipse的设置上启用它时可以检测到这种情况:
Java-> Compiler-> Errors/Warning-> Potential …Run Code Online (Sandbox Code Playgroud) java eclipse nullpointerexception eclipse-jdt static-code-analysis
我使用Eclipse jdt来格式化我生成的java文件,如下所示:
public String format(String code)
throws MalformedTreeException, BadLocationException {
Map options = new java.util.HashMap();
options.put(JavaCore.COMPILER_SOURCE, "1.5");
options.put(JavaCore.COMPILER_COMPLIANCE, "1.5");
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.5");
DefaultCodeFormatterOptions cfOptions =
DefaultCodeFormatterOptions.getDefaultSettings();
cfOptions.insert_new_line_after_annotation = false;
cfOptions.comment_insert_new_line_for_parameter = true;
cfOptions.blank_lines_before_method = 1;
cfOptions.number_of_empty_lines_to_preserve= 1;
cfOptions.tab_char = DefaultCodeFormatterOptions.SPACE;
CodeFormatter cf = new DefaultCodeFormatter(cfOptions, options);
TextEdit te = cf.format(CodeFormatter.K_UNKNOWN, code, 0,
code.length(), 0, null);
IDocument dc = new Document(code);
te.apply(dc);
return dc.get();
}
Run Code Online (Sandbox Code Playgroud)
但问题是我如何以编程方式使用Intellij Idea代码格式化程序API?Jetbrains是否推出了任何API?
我这里有一个代码:
public class TestOverride {
int foo() {
return -1;
}
}
class B extends TestOverride {
@Override
int foo() {
// error - quick fix to add "return super.foo();"
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我已经提到了错误.我正试图在eclipse jdt ui中为此创建一个quickfix.但我无法获得类B的超类节点,即类TestOverride.
我尝试了以下代码
if(selectedNode instanceof MethodDeclaration) {
ASTNode type = selectedNode.getParent();
if(type instanceof TypeDeclaration) {
ASTNode parentClass = ((TypeDeclaration) type).getSuperclassType();
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我只将parentClass作为TestOverride.但是当我检查时,这不是TypeDeclaration类型,它也不是SimpleName类型.
我的查询是如何获得类TestOverride节点的?
编辑
for (IMethodBinding parentMethodBinding :superClassBinding.getDeclaredMethods()){
if (methodBinding.overrides(parentMethodBinding)){
ReturnStatement rs = ast.newReturnStatement();
SuperMethodInvocation smi = ast.newSuperMethodInvocation();
rs.setExpression(smi);
Block oldBody = methodDecl.getBody();
ListRewrite listRewrite = …Run Code Online (Sandbox Code Playgroud) 在独立的Eclipse Java编译器中,我可以通过命令行属性(如下面的存根所示)以XML登录编译信息:
java -jar ecj-4.3.2.jar -log compile.xml <classpath,files>
Run Code Online (Sandbox Code Playgroud)
但是,当我将avenus-compiler-plugin与plexus-compiler-eclipse一起使用时,似乎无法将此参数传递给编译器,并且我不确定其原因,即插件的编译器是否为另一个编译器,它不会不会产生新进程(我什至尝试过可执行参数)或其他原因。
这是pom.xml部分:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>eclipse</compilerId>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<!--<compilerArgument> -log compile.xml </compilerArgument>-->
<compilerArgs>
<arg>-log</arg>
<arg>compile.xml</arg>
</compilerArgs>
<fork>true</fork>
<verbose>true</verbose>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-eclipse</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)