Skip to content

Commit

Permalink
Prep for #395: remove extra scope
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Aug 23, 2018
1 parent 564aa80 commit 7b37389
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.codehaus.groovy.ast.ClassNode;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.groovy.core.util.ArrayUtils;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
Expand Down Expand Up @@ -131,8 +132,9 @@ protected void buildTypeBindings(AccessRestriction accessRestriction) {
GroovyCompilationUnitDeclaration unitDecl = (GroovyCompilationUnitDeclaration) referenceContext;

for (TypeDeclaration typeDecl : unitDecl.types) {
if (typeDecl instanceof GroovyTypeDeclaration) {
((GroovyTypeDeclaration) typeDecl).fixAnonymousTypeBinding(this);
if (typeDecl instanceof GroovyTypeDeclaration &&
(typeDecl.bits & ASTNode.IsAnonymousType) != 0) {
((GroovyTypeDeclaration) typeDecl).fixAnonymousTypeBinding();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.PropertyNode;
import org.eclipse.jdt.groovy.core.util.ArrayUtils;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
import org.eclipse.jdt.internal.compiler.lookup.Scope;
import org.eclipse.jdt.internal.compiler.parser.Parser;

public class GroovyTypeDeclaration extends TypeDeclaration {
Expand All @@ -52,10 +51,10 @@ public ClassNode getClassNode() {
// FIXASC Is this always what we want to do - are there any other implications?
@Override
public void parseMethods(Parser parser, CompilationUnitDeclaration unit) {
// prevent Groovy types from having their methods re-parsed
// if (parser instanceof MultiplexingCommentRecorderParser) {
// super.parseMethods(parser, unit);
// }
// prevent Groovy types from having their methods re-parsed
/*if (parser instanceof MultiplexingCommentRecorderParser) {
super.parseMethods(parser, unit);
}*/
}

@Override
Expand All @@ -66,54 +65,33 @@ public boolean isScannerUsableOnThisDeclaration() {

//--------------------------------------------------------------------------

/**
* Anonymous types that are declared in this type's methods
*/
private GroovyTypeDeclaration[] anonymousTypes;

private static final GroovyTypeDeclaration[] NO_TYPES = {};

public GroovyTypeDeclaration[] getAnonymousTypes() {
return anonymousTypes;
return (anonymousTypes != null ? anonymousTypes : NO_TYPES);
}

public void addAnonymousType(GroovyTypeDeclaration anonymousType) {
if (anonymousTypes == null) {
anonymousTypes = new GroovyTypeDeclaration[] { anonymousType };
} else {
GroovyTypeDeclaration[] newTypes = new GroovyTypeDeclaration[anonymousTypes.length + 1];
System.arraycopy(anonymousTypes, 0, newTypes, 0, anonymousTypes.length);
newTypes[anonymousTypes.length] = anonymousType;
anonymousTypes = newTypes;
}
anonymousTypes = (GroovyTypeDeclaration[]) ArrayUtils.add(getAnonymousTypes(), anonymousType);
}

/**
* Fixes the super types of anonymous inner classes These kinds of classes are always constructed so that they extend the super
* type, even if the super type is an interface. This is because during parse time we don't know if the super type is a class or
* interface, se we need to wait until after the resolve phase to fix this.
*/
public void fixAnonymousTypeBinding(GroovyCompilationUnitScope groovyCompilationUnitScope) {
if ((this.bits & ASTNode.IsAnonymousType) != 0) {
if (classNode.getInterfaces() != null &&
classNode.getInterfaces().length == 1 &&
classNode.getSuperClass().getName().equals("java.lang.Object")) {

this.superInterfaces = new TypeReference[] { this.superclass };
this.binding.superInterfaces = new ReferenceBinding[] { (ReferenceBinding) this.superclass.resolvedType };
this.superclass = null;
}
}
if (anonymousTypes != null) {
fixAnonymousTypeDeclarations(anonymousTypes, groovyCompilationUnitScope);
}
}

private void fixAnonymousTypeDeclarations(GroovyTypeDeclaration[] types, Scope parentScope) {
for (GroovyTypeDeclaration type : types) {
GroovyClassScope anonScope = new GroovyClassScope(parentScope, type);
type.scope = anonScope;
if (type.anonymousTypes != null) {
fixAnonymousTypeDeclarations(type.anonymousTypes, anonScope);
}
public void fixAnonymousTypeBinding() {
if (classNode.getInterfaces() != null &&
classNode.getInterfaces().length == 1 &&
classNode.getSuperClass().getName().equals("java.lang.Object")) {

superInterfaces = new TypeReference[] {superclass};
binding.superInterfaces = new ReferenceBinding[] {
(ReferenceBinding) superclass.resolvedType,
};
superclass = null;
}
}
}

0 comments on commit 7b37389

Please sign in to comment.