Skip to content

Commit

Permalink
Don't throw exception every time EclipseUtils.parse is used on platfo…
Browse files Browse the repository at this point in the history
…rms without Java8 support
  • Loading branch information
dmitrygusev committed May 22, 2014
1 parent 23716ae commit d969b3c
Showing 1 changed file with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -327,30 +326,43 @@ public static CompilationUnit parse(ICompilationUnit unit)
return parse(source);
}

@SuppressWarnings("deprecation") // -- Since Eclipse Luna
public static CompilationUnit parse(String source)
{
if (source == null)
{
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)
Expand Down

0 comments on commit d969b3c

Please sign in to comment.