From cd2b1f0a36d7a699aea60e1a2f93650ac5be860d Mon Sep 17 00:00:00 2001 From: peter Date: Sun, 22 Nov 2009 19:29:06 +0000 Subject: [PATCH] don't fail when compiling with old groovy distributions (IDEADEV-41513, IDEADEV-41514) --- .../groovy/compiler/rt/GroovycRunner.java | 98 +++++++++++-------- 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/plugins/groovy/rt/src/org/jetbrains/groovy/compiler/rt/GroovycRunner.java b/plugins/groovy/rt/src/org/jetbrains/groovy/compiler/rt/GroovycRunner.java index 7a9e61af0618..8eb49b42ddf9 100644 --- a/plugins/groovy/rt/src/org/jetbrains/groovy/compiler/rt/GroovycRunner.java +++ b/plugins/groovy/rt/src/org/jetbrains/groovy/compiler/rt/GroovycRunner.java @@ -330,16 +330,12 @@ public class GroovycRunner { } private static CompilationUnit createCompilationUnit(final boolean forStubs, final CompilerConfiguration config, final String finalOutput) { - config.setClasspathList(Collections.EMPTY_LIST); + config.setClasspath(""); final GroovyClassLoader classLoader = buildClassLoaderFor(config); final GroovyClassLoader transformLoader = new GroovyClassLoader(classLoader) { public Enumeration getResources(String name) throws IOException { - if (forStubs) { - //return Collections.enumeration(Collections.EMPTY_LIST); - } - if (name.endsWith("org.codehaus.groovy.transform.ASTTransformation")) { final Enumeration resources = super.getResources(name); final ArrayList list = Collections.list(resources); @@ -354,47 +350,71 @@ public class GroovycRunner { return super.getResources(name); } }; - final CompilationUnit unit = new CompilationUnit(config, null, classLoader, transformLoader) { + CompilationUnit unit; + try { + unit = new CompilationUnit(config, null, classLoader, transformLoader) { - public void gotoPhase(int phase) throws CompilationFailedException { - super.gotoPhase(phase); - if (phase <= Phases.ALL) { - System.out.println(PRESENTABLE_MESSAGE + (forStubs ? "Groovy stub generator: " : "Groovy compiler: ") + getPhaseDescription()); + public void gotoPhase(int phase) throws CompilationFailedException { + super.gotoPhase(phase); + if (phase <= Phases.ALL) { + System.out.println(PRESENTABLE_MESSAGE + (forStubs ? "Groovy stub generator: " : "Groovy compiler: ") + getPhaseDescription()); + } } - } - }; + }; + } + catch (NoSuchMethodError e) { + //groovy 1.5.x + unit = new CompilationUnit(config, null, classLoader) { + + public void gotoPhase(int phase) throws CompilationFailedException { + super.gotoPhase(phase); + if (phase <= Phases.ALL) { + System.out.println(PRESENTABLE_MESSAGE + (forStubs ? "Groovy stub generator: " : "Groovy compiler: ") + getPhaseDescription()); + } + } + }; + } if (forStubs) { - //todo reuse JavaStubCompilationUnit in groovy 1.7 - boolean useJava5 = config.getTargetBytecode().equals(CompilerConfiguration.POST_JDK5); - final JavaStubGenerator stubGenerator = new JavaStubGenerator(config.getTargetDirectory(), false, useJava5); - - //but JavaStubCompilationUnit doesn't have this... - unit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { - public void call(SourceUnit source, GeneratorContext context, ClassNode node) throws CompilationFailedException { - new JavaAwareResolveVisitor(unit).startResolving(node, source); - } - },Phases.CONVERSION); - - unit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { - public void call(final SourceUnit source, final GeneratorContext context, final ClassNode node) throws CompilationFailedException { - final String name = node.getNameWithoutPackage(); - if ("package-info".equals(name)) { - return; - } - - System.out.println(PRESENTABLE_MESSAGE + "Generating stub for " + name); - try { - stubGenerator.generateClass(node); - } - catch (FileNotFoundException e) { - source.addException(e); - } - } - },Phases.CONVERSION); + try { + addStubGeneration(config, unit); + } + catch (LinkageError e) { + //older groovy distributions, just don't generate stubs + } } return unit; } + private static void addStubGeneration(CompilerConfiguration config, final CompilationUnit unit) { + //todo reuse JavaStubCompilationUnit in groovy 1.7 + boolean useJava5 = config.getTargetBytecode().equals(CompilerConfiguration.POST_JDK5); + final JavaStubGenerator stubGenerator = new JavaStubGenerator(config.getTargetDirectory(), false, useJava5); + + //but JavaStubCompilationUnit doesn't have this... + unit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { + public void call(SourceUnit source, GeneratorContext context, ClassNode node) throws CompilationFailedException { + new JavaAwareResolveVisitor(unit).startResolving(node, source); + } + }, Phases.CONVERSION); + + unit.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() { + public void call(final SourceUnit source, final GeneratorContext context, final ClassNode node) throws CompilationFailedException { + final String name = node.getNameWithoutPackage(); + if ("package-info".equals(name)) { + return; + } + + System.out.println(PRESENTABLE_MESSAGE + "Generating stub for " + name); + try { + stubGenerator.generateClass(node); + } + catch (FileNotFoundException e) { + source.addException(e); + } + } + },Phases.CONVERSION); + } + static GroovyClassLoader buildClassLoaderFor(final CompilerConfiguration compilerConfiguration) { return (GroovyClassLoader) AccessController.doPrivileged(new PrivilegedAction() { public Object run() {