0
我想對Java源代碼進行一些靜態代碼分析。爲了解析我使用Eclipse JDT(3.6)ASTParser
Eclipse之外與下面的代碼:Eclipse JDT AST:使用嵌套成員解析類的問題
private static final Map<String, String> COMPILER_OPTIONS;
static {
COMPILER_OPTIONS = new HashMap<String, String>(JavaCore.getOptions());
COMPILER_OPTIONS.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_6);
COMPILER_OPTIONS.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_6);
COMPILER_OPTIONS.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
}
private CompilationUnit parseReadSourceFileIfPossible(String readSourceFile) {
CompilationUnit result = null;
if (isPossibleToParse(readSourceFile)) {
final ASTParser parser = createAndConfigureParser();
parser.setSource(readSourceFile.toCharArray());
result = (CompilationUnit) parser.createAST(null);
}
return result;
}
private ASTParser createAndConfigureParser() {
final ASTParser result = ASTParser.newParser(AST.JLS3);
result.setKind(ASTParser.K_COMPILATION_UNIT);
result.setCompilerOptions(COMPILER_OPTIONS); return result;
}
對於「正常」的Java類,這種方法工作完全正常。但是,如果我對 解析了以下類(ValidUnrestrictedComponent
),則解析器會運行 問題。
package valid;
import de.htwg_konstanz.joi.annotations.JoiComponten;
@JoiComponent
public final class ValidUnrestrictedComponent {
private static final class Implementation implements TestInterface {
@Override
public int doSomething() {
// TODO Auto-generated method stub
return 0;
}
}
private ValidUnrestrictedComponent() {
throw new AssertionError();
}
public static Implementation getInstance() {
return new Implementation();
}
private static void getNothing() {
// Nothing to do here
}
private void doNothing() {
// Nothing to do here
}
}
我確實收到CompilationUnit
類型的對象,然而它並只包含 嵌套構件Implementation
及其方法。其餘的課程 - 如 getInstance
或doNothing
缺失。
將所得CompilationUnit
包含一個字段problems
具有以下 三個問題:
- DefaultProblem(ID = 141):鉛(240)中的語法錯誤,插入 「}」 來完成ClassBody
- DefaultProblem( ID = 143):鉛(240)中的語法錯誤,插入 「}」 來完成ClassBody
- DefaultProblem(ID = 164):鉛(240)中的語法錯誤,插入 「}」 來完成MethodBody
我看不到上面提到的類 ValidUnrestrictedComponent
中的任何語法錯誤。
好的,謝謝。我稍後會看看。 – JFFF
謝謝Deepak Azad!這是缺失的行分隔符。 – JFFF