Java Eclipse JDT AST:解析具有嵌套成员的类时出现问题
我想对Java源代码进行一些静态代码分析。对于解析,我在Eclipse外部使用Eclipse JDT 3.6 ASTParser,并使用以下代码:Java Eclipse JDT AST:解析具有嵌套成员的类时出现问题,java,eclipse,eclipse-plugin,abstract-syntax-tree,eclipse-jdt,Java,Eclipse,Eclipse Plugin,Abstract Syntax Tree,Eclipse Jdt,我想对Java源代码进行一些静态代码分析。对于解析,我在Eclipse外部使用Eclipse JDT 3.6 ASTParser,并使用以下代码: private static final Map<String, String> COMPILER_OPTIONS; static { COMPILER_OPTIONS = new HashMap<String, String>(JavaCore.getOptions()); COMPILER_OPTIONS
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;
}
我确实收到了CompilationUnit类型的对象,但是它只包含
嵌套成员实现及其方法。班上的其他同学——比如
getInstance或doNothing丢失
获取的CompliationUnit包含一个字段,该字段存在以下问题
三个问题:
DefaultProblem id=141:Pb240语法错误,插入}以完成类主体
DefaultProblem id=143:Pb240语法错误,插入}以完成类主体
DefaultProblem id=164:Pb240语法错误,插入}以完成MethodBody
我在上面提到的类中看不到任何语法错误
ValidUnrestrictedComponent.您能再次检查“readSourceFile”的内容吗?我猜其中没有“\n”或新行字符。缺少新行字符将导致第一条注释后的所有行成为注释本身的一部分。谢谢Deepak Azad!这是丢失的行分隔符。
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
}
}