Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » AssertStatement parsing problem
AssertStatement parsing problem [message #1814163] Thu, 05 September 2019 03:48 Go to next message
Eclipse UserFriend
I use ASTParser to analyze a single Java file.
This is my testing code.
public void method10(int a) {
        assert a > 2;
        System.out.println("end");
    }

And this is my parse code
        Collection<File> srcFiles = FileUtils.listFiles(new File(folderPath), new String[] {"java"}, true);
        List<String> srcList = new ArrayList<>(srcFiles.size());
        for (File file : srcFiles) {
            srcList.add(file.getPath());
        }
        String[] sourceFilePaths = new String[srcList.size()];
        srcList.toArray(sourceFilePaths);

        ASTParser parser = ASTParser.newParser(AST.JLS8);
        String[] sourcepathEntries = {};
        parser.setEnvironment(null, sourcepathEntries, null, true);
        parser.setResolveBindings(true);
        parser.setStatementsRecovery(true);
        parser.setKind(ASTParser.K_COMPILATION_UNIT);

        ASTRequestor requestor = new ASTRequestor();
        String[] bindingKeys = new String[] {};
        parser.createASTs(sourceFilePaths, null, bindingKeys, requestor, null);

I collect all CompilationUnit into Requestor and traverse them using visitor pattern.
However, I can not find AssertStatement when I traverse this MethodDeclaratio and the final result is
public void method10(int a){
  assert a=2;
  System.out.println("end");
}

I can not figure out what is the problem.
Re: AssertStatement parsing problem [message #1814180 is a reply to message #1814163] Thu, 05 September 2019 06:28 Go to previous messageGo to next message
Eclipse UserFriend
I can see the AssertStatement in ASTView, To analyze better, please create a bug in JDT core component https://bugs.eclipse.org/bugs/enter_bug.cgi?product=JDT
Please attach a reproducible project and steps to reproduce.
Re: AssertStatement parsing problem [message #1814211 is a reply to message #1814180] Thu, 05 September 2019 13:16 Go to previous messageGo to next message
Eclipse UserFriend
I'm not sure what's the deal of ">" vs. "=" but it seems in standalone mode you have to pass the source level via ASTParser#setCompilerOptions().
You want option key "org.eclipse.jdt.core.compiler.source" and a value greater than "1.3".
Please see that the default *is* 1.3: https://help.eclipse.org/2019-06/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/JavaCore.html#COMPILER_SOURCE
Re: AssertStatement parsing problem [message #1814217 is a reply to message #1814211] Thu, 05 September 2019 21:28 Go to previous messageGo to next message
Eclipse UserFriend
@ Stephan HerrmannFriend you are right. I set
 JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8
and it works.
But I am confused with this. In standalone mode I have already indicate the AST level
[ASTParser parser = ASTParser.newParser(AST.JLS8)

what's the difference between and AST level and compiler_option

[Updated on: Thu, 05 September 2019 21:28] by Moderator

Re: AssertStatement parsing problem [message #1814644 is a reply to message #1814217] Sun, 15 September 2019 14:48 Go to previous message
Eclipse UserFriend
AST level defines which structure elements in the abstract syntax tree are available. This is a purely technical thing, by which plug-ins can decide their own pace in migrating to newer versions of JDT (supporting newer versions of Java).

For legality of source input only the compiler options COMPILER_SOURCE and COMPILER_COMPLIANCE are relevant.
Previous Topic:Unable to use Google Play Services Vision API in Eclipse
Next Topic: Problem with WindowBuilder on MacBook
Goto Forum:
  


Current Time: Thu Jul 03 19:42:30 EDT 2025

Powered by FUDForum. Page generated in 0.04614 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top