Skip to content

Commit

Permalink
GROOVY-8819: java.lang.VerifyError when calling this() with static fi…
Browse files Browse the repository at this point in the history
…nal field from a super class(closes #807)
  • Loading branch information
daniellansun committed Oct 7, 2018
1 parent 5ff63b1 commit f4e5745
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/codehaus/groovy/control/StaticImportVisitor.java
Expand Up @@ -208,6 +208,17 @@ protected Expression transformVariableExpression(VariableExpression ve) {
}
return result;
}
} else if (v instanceof FieldNode) {
if (inSpecialConstructorCall) { // GROOVY-8819
FieldNode fn = (FieldNode) v;
ClassNode declaringClass = fn.getDeclaringClass();
if (fn.isStatic() && currentClass.isDerivedFrom(declaringClass)) {
Expression result = new PropertyExpression(new ClassExpression(declaringClass), v.getName());
result.setSourcePosition(ve);

return result;
}
}
}
return ve;
}
Expand Down
56 changes: 56 additions & 0 deletions src/test/groovy/bugs/Groovy8819Bug.groovy
@@ -0,0 +1,56 @@
/*
* 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 groovy.bugs

class Groovy8819Bug extends GroovyTestCase {
void testAccessStaticFieldInThisConstructor() {
assertScript '''
class Parent {
static final String DEFAULT = 'default'
}
class Child extends Parent {
String value
Child() { this(DEFAULT) }
Child(String value) { this.value = value }
}
assert 'default' == new Child().value
'''
}

void testAccessStaticFieldInThisConstructorCS() {
assertScript '''
import groovy.transform.CompileStatic
@CompileStatic
class Parent {
static final String DEFAULT = 'default'
}
@CompileStatic
class Child extends Parent {
String value
Child() { this(DEFAULT) }
Child(String value) { this.value = value }
}
assert 'default' == new Child().value
'''
}
}

0 comments on commit f4e5745

Please sign in to comment.