This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository jaxx. See http://git.nuiton.org/jaxx.git commit a3fba0e9c01dd6c71581393629dc7b5b12fd3349 Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Apr 12 13:49:32 2015 +0200 refs #3667 can now skip method body parse --- .../java/jaxx/compiler/java/parser/JavaParser.java | 56 +++++++++++-- .../ClassDescriptorResolverFromJavaFile.java | 14 +++- .../jaxx/compiler/java/parser/JavaParserTest.java | 98 ++++++++++++++++++++++ 3 files changed, 159 insertions(+), 9 deletions(-) diff --git a/jaxx-compiler/src/main/java/jaxx/compiler/java/parser/JavaParser.java b/jaxx-compiler/src/main/java/jaxx/compiler/java/parser/JavaParser.java index 4e7d5b0..6368eb6 100644 --- a/jaxx-compiler/src/main/java/jaxx/compiler/java/parser/JavaParser.java +++ b/jaxx-compiler/src/main/java/jaxx/compiler/java/parser/JavaParser.java @@ -116,7 +116,11 @@ public class JavaParser/*@bgen(jjtree)*/ implements JavaParserTreeConstants, Jav } public JavaParser(String fileName) { - this(System.in); + this(fileName, false); + } + + public JavaParser(String fileName, boolean parseMethodBody) { + this(System.in, parseMethodBody); try { ReInit(new FileInputStream(new File(fileName))); } catch (Exception e) { @@ -143,11 +147,11 @@ public class JavaParser/*@bgen(jjtree)*/ implements JavaParserTreeConstants, Jav JavaParser parser; if (args.length == 0) { System.out.println("Java Parser Version 1.1: Reading from standard input . . ."); - parser = new JavaParser(System.in); + parser = new JavaParser(System.in, true); } else if (args.length == 1) { System.out.println("Java Parser Version 1.1: Reading from file " + args[0] + " . . ."); try { - parser = new JavaParser(new java.io.FileInputStream(args[0])); + parser = new JavaParser(new java.io.FileInputStream(args[0]), true); } catch (java.io.FileNotFoundException e) { System.out.println("Java Parser Version 1.1: File " + args[0] + " not found."); return; @@ -1687,7 +1691,30 @@ public class JavaParser/*@bgen(jjtree)*/ implements JavaParserTreeConstants, Jav } switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { case LBRACE: - Block(); + + if (parseMethodBody) { + + Block(); + + } else { + + // We do not want to parse inside a method implementation, we just need to get method prototypes + // See https://forge.nuiton.org/issues/3667 + + int nbBlocks = 0; + do { + switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) { + case LBRACE: + nbBlocks++; + break; + case RBRACE: + nbBlocks--; + break; + } + jj_consume_token(jj_ntk); + } while (nbBlocks > 0); + + } break; case SEMICOLON: jj_consume_token(SEMICOLON); @@ -9908,11 +9935,22 @@ public class JavaParser/*@bgen(jjtree)*/ implements JavaParserTreeConstants, Jav private boolean jj_semLA; + private final boolean parseMethodBody; + public JavaParser(java.io.InputStream stream) { - this(stream, null); + this(stream, null, false); + } + + public JavaParser(java.io.InputStream stream, boolean parseMethodBody) { + this(stream, null, parseMethodBody); } public JavaParser(java.io.InputStream stream, String encoding) { + this(stream, encoding, false); + } + + public JavaParser(java.io.InputStream stream, String encoding, boolean parseMethodBody) { + this.parseMethodBody = parseMethodBody; try { jj_input_stream = new JavaCharStream(stream, encoding, 1, 1); } catch (java.io.UnsupportedEncodingException e) { @@ -9940,6 +9978,11 @@ public class JavaParser/*@bgen(jjtree)*/ implements JavaParserTreeConstants, Jav } public JavaParser(java.io.Reader stream) { + this(stream, false); + } + + public JavaParser(java.io.Reader stream, boolean parseMethodBody) { + this.parseMethodBody=parseMethodBody; jj_input_stream = new JavaCharStream(stream, 1, 1); token_source = new JavaParserTokenManager(jj_input_stream); token = new Token(); @@ -9954,7 +9997,8 @@ public class JavaParser/*@bgen(jjtree)*/ implements JavaParserTreeConstants, Jav jjtree.reset(); } - public JavaParser(JavaParserTokenManager tm) { + public JavaParser(JavaParserTokenManager tm, boolean parseMethodBody) { + this.parseMethodBody =parseMethodBody; token_source = tm; token = new Token(); jj_ntk = -1; diff --git a/jaxx-compiler/src/main/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFile.java b/jaxx-compiler/src/main/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFile.java index c135a5e..250a618 100644 --- a/jaxx-compiler/src/main/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFile.java +++ b/jaxx-compiler/src/main/java/jaxx/compiler/reflect/resolvers/ClassDescriptorResolverFromJavaFile.java @@ -61,8 +61,14 @@ public class ClassDescriptorResolverFromJavaFile extends ClassDescriptorResolver private static final Log log = LogFactory.getLog(ClassDescriptorResolverFromJavaFile.class); + private final boolean parseMethodBody; + public ClassDescriptorResolverFromJavaFile() { + this(false); + } + public ClassDescriptorResolverFromJavaFile(boolean parseMethodBody) { super(ClassDescriptorHelper.ResolverType.JAVA_FILE); + this.parseMethodBody = parseMethodBody; } @Override @@ -79,7 +85,7 @@ public class ClassDescriptorResolverFromJavaFile extends ClassDescriptorResolver if (log.isDebugEnabled()) { log.debug("for source " + displayName); } - JavaFileParser parser = new JavaFileParser(classLoader); + JavaFileParser parser = new JavaFileParser(classLoader, parseMethodBody); if (log.isDebugEnabled()) { log.debug("starting parsing : " + displayName); } @@ -206,6 +212,7 @@ public class ClassDescriptorResolverFromJavaFile extends ClassDescriptorResolver public static final String[] EMPTY_STRING_ARRAY = new String[0]; + private final boolean parseMethodBody; /** * To test if a compilation unit was already parsed. If so, then stop @@ -216,7 +223,8 @@ public class ClassDescriptorResolverFromJavaFile extends ClassDescriptorResolver */ private boolean firstTypeScanned; - protected JavaFileParser(ClassLoader classLoader) { + protected JavaFileParser(ClassLoader classLoader,boolean parseMethodBody) { + this.parseMethodBody = parseMethodBody; //FIXME-TC-20100504 : shoudl remove this to make the parser free of jaxx :) // We could imagine just to offers to the parser a list of namespaces // (for class resolving)... @@ -237,7 +245,7 @@ public class ClassDescriptorResolverFromJavaFile extends ClassDescriptorResolver firstTypeScanned = false; try { - JavaParser p = new JavaParser(src); + JavaParser p = new JavaParser(src, parseMethodBody); p.CompilationUnit(); SimpleNode node = p.popNode(); if (node != null) { diff --git a/jaxx-compiler/src/test/java/jaxx/compiler/java/parser/JavaParserTest.java b/jaxx-compiler/src/test/java/jaxx/compiler/java/parser/JavaParserTest.java new file mode 100644 index 0000000..927e73f --- /dev/null +++ b/jaxx-compiler/src/test/java/jaxx/compiler/java/parser/JavaParserTest.java @@ -0,0 +1,98 @@ +package jaxx.compiler.java.parser; + +import jaxx.compiler.CompilerException; +import jaxx.compiler.reflect.ClassDescriptor; +import jaxx.compiler.reflect.MethodDescriptor; +import jaxx.compiler.reflect.resolvers.ClassDescriptorResolverFromJavaFile; +import org.junit.Assert; +import org.junit.Test; +import org.nuiton.util.FileUtil; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; + +/** + * Created on 4/12/15. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 2.24 + */ +public class JavaParserTest { + + @Test + public void testParseClassButNotMethodBody() throws IOException, ClassNotFoundException, NoSuchMethodException { + + File file = new File("").getAbsoluteFile(); + Path rootSourceDirectory = file.toPath().resolve("src").resolve("test").resolve("java"); + + File thisClassJavaFile = new File(FileUtil.getFileFromFQN(rootSourceDirectory.toFile(), getClass().getPackage().getName()), getClass().getSimpleName() + ".java"); + + Assert.assertTrue("Could not find java file " + thisClassJavaFile, thisClassJavaFile.exists()); + + ClassDescriptorResolverFromJavaFile resolver = new ClassDescriptorResolverFromJavaFile(); + + ClassDescriptor yo = resolver.resolvDescriptor("yo", thisClassJavaFile.toURI().toURL()); + MethodDescriptor[] methodDescriptors = yo.getMethodDescriptors(); + Assert.assertTrue(methodDescriptors.length>3); + + { + MethodDescriptor method = yo.getMethodDescriptor("testParseClassButNotMethodBody"); + Assert.assertNotNull(method); + } + + { + MethodDescriptor method = yo.getMethodDescriptor("methodWithDiamondInside"); + Assert.assertNotNull(method); + } + + { + MethodDescriptor method = yo.getMethodDescriptor("methodWithTryResourceInside"); + Assert.assertNotNull(method); + } + + } + + + @Test + public void testParseClassAndMethodBody() throws IOException, ClassNotFoundException, NoSuchMethodException { + + File file = new File("").getAbsoluteFile(); + Path rootSourceDirectory = file.toPath().resolve("src").resolve("test").resolve("java"); + + File thisClassJavaFile = new File(FileUtil.getFileFromFQN(rootSourceDirectory.toFile(), getClass().getPackage().getName()), getClass().getSimpleName() + ".java"); + + Assert.assertTrue("Could not find java file " + thisClassJavaFile, thisClassJavaFile.exists()); + + ClassDescriptorResolverFromJavaFile resolver = new ClassDescriptorResolverFromJavaFile(true); + + try { + resolver.resolvDescriptor("yo", thisClassJavaFile.toURI().toURL()); + Assert.fail("Can't compile body method (there is some jdk7 syntaxes inside them)..."); + } catch (RuntimeException e) { + Assert.assertTrue(e.getCause() instanceof CompilerException); + } + + } + + public void methodWithDiamondInside() { + + new ArrayList<>(); + + } + + public void methodWithTryResourceInside() throws IOException { + + try (BufferedReader reader = Files.newBufferedReader(new File("").toPath(), Charset.forName("UTF-8"))) { + + System.out.println(reader); + } + + + } + +} \ No newline at end of file -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.