eclipse-jdt

How to add a code snippet to method body with JDT/AST

独自空忆成欢 提交于 2019-12-06 17:19:58
问题 I'm trying to generate Java source code with JDT/AST. I now have MethodDeclaration and want to add a code snippet (from another source) to the method body. The code snippet can contain any Java code, even syntactically invalid code . I just can't find the way to do this. With JCodeModel you would use JBlock#directStatement(String s) method. Is there a way to do this with JDT/AST? 回答1: Since you have a well-formed tree for the rest of the application, and you want to insert non-well-formed

How to use ASTRewrite to replace a particular SimpleType with a PrimitiveType?

浪子不回头ぞ 提交于 2019-12-06 15:18:34
I need to preprocess some code before compiling for a java based language - Processing. In this language, all instances of type color, need to be replaced with int. For ex, here's a code snippet: color red = 0xffaabbcc; color[][] primary = new color[10][10]; After preprocessing, the above code should look like: int red = 0xffaabbcc; int[][] primary = new int[10][10]; I'm working in a non eclipse environment. I'm using Eclipse JDT ASTParser to do this. I've implemented my ASTVisitor which visits all SimpleType nodes. Here's the code snippet from the ASTVisitor implementation: public boolean

Track files changes done by eclipse cleanup programmatically

孤人 提交于 2019-12-06 15:06:26
I have written one eclipse osgi plugin that runs cleanup and formatting actions on java files present in eclipse project. Some thing like: Run batch file that has eclipse command It open's eclipse editor Loads eclipse project passed as parameter in batch command Run cleanup and formatting actions Closes eclipse Now my problem is I need to track the files that has been changed by this action. I am performing cleanup changes using cleanUpsAction that runs as thread over multiple files and forks further. It returns void. There is IResourceChangeListener which I tried as well but I am not able to

Incremental java compile with maven (like Eclipse does)

孤者浪人 提交于 2019-12-06 14:59:00
问题 I want to use maven to build projects in which there are unresolved compilation problems. The main purpose is package and deploy or run aplications using some kind of stubs for classes that contains compilation errors , like I understand that Eclipse does (thanks to JDT Core). I configurate maven java compiler plugin following Apache Maven documentation at Using Non-Javac compiler to use Eclipse compiler. Thinking that maybe should set some arguments to modify the compiler/builder behaivor I

extract interface that a class implementing using AST parser

China☆狼群 提交于 2019-12-06 09:20:28
I am compiling a project source using AST parser. In what way i can extract class hierarchy infromation, that is whether it is implementing any interface or extends from another class? You can visit the TypeDeclaration node and get the type binding from it. ITypeBinding typeBind = typDec.resolveBinding(); You can then get the super class and implemented interfaces as follows: public boolean visit(TypeDeclaration typeDeclaration) { ITypeBinding typeBind = typeDeclaration.resolveBinding(); ITypeBinding superTypeBind = typeBind.getSuperclass(); ITypeBinding[] interfaceBinds = typeBind

JUnit test for jdt.core Java Models

孤街浪徒 提交于 2019-12-06 08:16:23
I am trying to make some JUnit tests for my code. But the problem is, I make use of the Java Models like ICompilationUnit, IPackageFragment, ITypes and etc. I did not get how to create some ICompilationUnit and then test. I searched google and stackoverflow for information but did not find something. My question is, how can I make Junit test with classes of the jdt.core...can somebody give me may be some code examples. Thanks Here is a Method I coded: private void updateLists() { if(!getCompilationUnit().isEmpty()){ for(int i = 0; i < getCompilationUnit().size(); i++){ try { Document doc = new

JDT - Trying to change superclass of Type. I don't know the Qualified Name of a Super Class

感情迁移 提交于 2019-12-06 05:13:36
I have a program that, among other tasks, has to change the super class of some classes using JDT. I have two strings with the qualified name of the superclasses to swap, for example "org.example.John" and "org.example.Smith" and I'm parsing the whole AST searching for classes that extend these ones. My problem happens when I'm trying to find out the qualified name of the super class. I just can't find a way. Given an ICompilationUnit (representing a class), I use a an ASTVisitor to parse the TypeDeclaration, as the following method shows. public boolean visit(TypeDeclaration typeDeclaration)

Potential null pointer access

天涯浪子 提交于 2019-12-06 03:21:54
I encounter a strange situation that is currently not that clear to me: When having the potential null pointer access warning enabled in Eclipse, I get warnings like in the following (the warnings are stick to the line preceding the corresponding comment): protected Item findItemByName(String itemName, Items items) { boolean isItemNameMissing = null == itemName || itemName.isEmpty(); boolean isItemsMissing = null == items || null == items.getItems() || items.getItems().isEmpty(); if (isItemNameMissing || isItemsMissing) { return null; } // potential null pointer access: the variable items

Oracle JDK and Eclipse JDT compilers disagree! Which is compiling this incorrectly? Unusual generics and inferrence

爱⌒轻易说出口 提交于 2019-12-05 16:45:57
I have a piece of code which is compiling inconsistently between Oracle JDK 7 and Eclipse JDT 7, but since I'm not sure about which compiler is making the mistake(s) I thought I should ask for opinions here before submitting any bug reports. This is the simplest test I could come up with to demonstrate the inconsistency: interface Foo<S extends Foo<S, T>, T> { // should this compile? public <X extends Foo<S, Y>, Y> Y method1(); // what about this? public <X extends Foo<? extends S, Y>, Y> Y method2(); } Oracle JDK gives an error on method1 but not method2, whereas Eclipse has no problem with

eclipse ASTNode to source code line number

霸气de小男生 提交于 2019-12-05 16:06:27
Given an ASTNode in eclipse, is there any way to get the corresponding source code line number? You can get the line number of an ASTNode using the below code int lineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1; the compilation unit can be obtained from the ASTParser using the below code ASTParser parser = ASTParser.newParser(AST.JLS3); // Parse the class as a compilation unit. parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(source); // give your java source here as char array parser.setResolveBindings(true); // Return the compiled class as a compilation