Skip to content

Commit

Permalink
Merge branch 'groovy7966'
Browse files Browse the repository at this point in the history
  • Loading branch information
paulk-asert committed Dec 9, 2016
2 parents 35ae961 + b43dddb commit eae0083
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/main/org/codehaus/groovy/control/OptimizerVisitor.java
Expand Up @@ -19,20 +19,26 @@
package org.codehaus.groovy.control;

import org.codehaus.groovy.ast.ClassCodeExpressionTransformer;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.objectweb.asm.Opcodes;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
* Visitor to produce several optimizations
* - to replace numbered constants with references to static fields
*
* @author Alex Tkachman
* Visitor to produce several optimizations:
* <ul>
* <li>to replace numbered constants with references to static fields</li>
* <li>remove superfluous references to GroovyObject interface</li>
* </ul>
*/
public class OptimizerVisitor extends ClassCodeExpressionTransformer {
private ClassNode currentClass;
Expand All @@ -51,6 +57,31 @@ public void visitClass(ClassNode node, SourceUnit source) {
missingFields.clear();
super.visitClass(node);
addMissingFields();
pruneUnneededGroovyObjectInterface(node);
}

private void pruneUnneededGroovyObjectInterface(ClassNode node) {
ClassNode superClass = node.getSuperClass();
boolean isSuperGroovy = superClass.isDerivedFromGroovyObject();
if (isSuperGroovy) {
ClassNode[] interfaces = node.getInterfaces();
boolean needsFix = false;
for (ClassNode classNode : interfaces) {
if (classNode.equals(ClassHelper.GROOVY_OBJECT_TYPE)) {
needsFix = true;
break;
}
}
if (needsFix) {
List<ClassNode> newInterfaces = new ArrayList<ClassNode>(interfaces.length);
for (ClassNode classNode : interfaces) {
if (!classNode.equals(ClassHelper.GROOVY_OBJECT_TYPE)) {
newInterfaces.add(classNode);
}
}
node.setInterfaces(newInterfaces.toArray(new ClassNode[newInterfaces.size()]));
}
}
}

private void addMissingFields() {
Expand Down
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.codehaus.groovy.tools.stubgenerator

/**
* Test that fileorder doesn't impact whether GroovyObject appears in implements list.
*/
class Groovy7966Bug extends StringSourcesStubTestCase {

@Override
Map<String, String> provideSources() {
[
'Before.groovy' : '''
class Before extends AbstractThing {}
''',
'AbstractThing.groovy' : '''
abstract class AbstractThing {}
''',
'JavaThing.java' : '''
public class JavaThing {
}
''',
'After.groovy' : '''
class After extends AbstractThing {}
''',
]
}

@Override
protected List<File> collectSources(File path) {
// parent method uses order returned by the file system - we want to maintain supplied order
return provideSources().collect{ name, _ -> new File(path, name) }
}

@Override
void verifyStubs() {
// actually, we don't care about the stubs but let's verify the resulting class files
def classLoader = new URLClassLoader([targetDir.toURI().toURL()] as URL[], loader)
assert classLoader.loadClass('Before').interfaces.length == 0
assert classLoader.loadClass('After').interfaces.length == 0
}
}

0 comments on commit eae0083

Please sign in to comment.