From d969b3c0a85ccc7edac586226a90b51dbc1924c1 Mon Sep 17 00:00:00 2001 From: Dmitry Gusev Date: Thu, 22 May 2014 18:28:05 +0400 Subject: [PATCH] Don't throw exception every time EclipseUtils.parse is used on platforms without Java8 support --- .../eclipse/tapestry5/EclipseUtils.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/com.anjlab.eclipse.tapestry5/src/com/anjlab/eclipse/tapestry5/EclipseUtils.java b/com.anjlab.eclipse.tapestry5/src/com/anjlab/eclipse/tapestry5/EclipseUtils.java index 4892cc1..eb80e87 100644 --- a/com.anjlab.eclipse.tapestry5/src/com/anjlab/eclipse/tapestry5/EclipseUtils.java +++ b/com.anjlab.eclipse.tapestry5/src/com/anjlab/eclipse/tapestry5/EclipseUtils.java @@ -18,7 +18,6 @@ import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.Name; @@ -327,7 +326,6 @@ public static CompilationUnit parse(ICompilationUnit unit) return parse(source); } - @SuppressWarnings("deprecation") // -- Since Eclipse Luna public static CompilationUnit parse(String source) { if (source == null) @@ -335,22 +333,36 @@ public static CompilationUnit parse(String source) throw new IllegalStateException(SOURCE_NOT_FOUND); } - ASTParser parser; - try - { - int parserLevel = 8; // AST.JLS8 -- Since Eclipse Luna - parser = ASTParser.newParser(parserLevel); - } - catch (IllegalArgumentException e) + if (parserLevel == -1) { - parser = ASTParser.newParser(AST.JLS4); + parserLevel = getParserLevel(); } + + ASTParser parser = ASTParser.newParser(parserLevel); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(source.toCharArray()); parser.setResolveBindings(true); return (CompilationUnit) parser.createAST(null); } + private static int parserLevel = -1; + + private static int getParserLevel() + { + try + { + int JLS8 = 8; + // Try to use Java 8's AST.JLS8 + ASTParser.newParser(JLS8); + return JLS8; + } + catch (IllegalArgumentException e) + { + // Fallback to Java 7 + return 4; // AST.JLS4 + } + } + public static IProject getProjectFromSelection(ISelection selection) { if (selection instanceof ITreeSelection)