Skip to content

Commit

Permalink
GROOVY-10414
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Dec 14, 2021
1 parent b91e6c2 commit c869670
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4913,4 +4913,28 @@ public void testTypeChecked10351() {

runConformTest(sources);
}

@Test
public void testTypeChecked10414() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TypeChecked\n" +
"class Outer {\n" +
" class Inner {\n" +
" void test() {\n" +
" foo = 'bar'\n" +
" print(foo);\n" +
" setFoo('baz')\n" +
" print(getFoo())\n" +
" }\n" +
" }\n" +
" def foo\n" +
"}\n" +
"new Outer.Inner(new Outer()).test()\n",
};
//@formatter:on

runConformTest(sources, "barbaz");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5764,18 +5764,7 @@ protected List<MethodNode> findMethod(ClassNode receiver, final String name, fin
curNode = curNode.getSuperClass();
}
*/
outer_upper: // can't use existsProperty because it calls findMethod
for (ClassNode cn = receiver; cn != null; cn = cn.getSuperClass()) {
property = cn.getProperty(pname);
if (property != null) break outer_upper;
if (!cn.isStaticClass() && cn.getOuterClass() != null
&& typeCheckingContext.getEnclosingClassNodes().contains(cn)) {
ClassNode outer = cn.getOuterClass();
do { property = outer.getProperty(pname);
if (property != null) break outer_upper;
} while (!outer.isStaticClass() && (outer = outer.getOuterClass()) != null);
}
}
property = findProperty(receiver, pname);
// GRECLIPSE end
if (property != null) {
int mods = Opcodes.ACC_PUBLIC | (property.isStatic() ? Opcodes.ACC_STATIC : 0);
Expand All @@ -5788,12 +5777,16 @@ protected List<MethodNode> findMethod(ClassNode receiver, final String name, fin
// maybe we are looking for a setter ?
String pname = extractPropertyNameFromMethodName("set", name);
if (pname != null) {
/* GRECLIPSE edit -- GROOVY-10414
ClassNode curNode = receiver;
PropertyNode property = null;
while (property == null && curNode != null) {
property = curNode.getProperty(pname);
curNode = curNode.getSuperClass();
}
*/
PropertyNode property = findProperty(receiver, pname);
// GRECLIPSE end
if (property != null && !Modifier.isFinal(property.getModifiers())) { // GRECLIPSE add
ClassNode type = property.getOriginType();
if (implementsInterfaceOrIsSubclassOf(wrapTypeIfNecessary(args[0]), wrapTypeIfNecessary(type))) {
Expand Down Expand Up @@ -5847,6 +5840,25 @@ protected List<MethodNode> findMethod(ClassNode receiver, final String name, fin
return EMPTY_METHODNODE_LIST;
}

// GRECLIPSE add
private PropertyNode findProperty(final ClassNode receiver, final String name) {
for (ClassNode cn = receiver; cn != null; cn = cn.getSuperClass()) {
PropertyNode property = cn.getProperty(name);
if (property != null) return property;

if (!cn.isStaticClass() && cn.getOuterClass() != null
&& typeCheckingContext.getEnclosingClassNodes().contains(cn)) {
ClassNode outer = cn.getOuterClass();
do {
property = outer.getProperty(name);
if (property != null) return property;
} while (!outer.isStaticClass() && (outer = outer.getOuterClass()) != null);
}
}
return null;
}
// GRECLIPSE end

private List<MethodNode> filterMethodsByVisibility(List<MethodNode> methods) {
if (!asBoolean(methods)) {
return EMPTY_METHODNODE_LIST;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5493,18 +5493,7 @@ protected List<MethodNode> findMethod(ClassNode receiver, final String name, fin
curNode = curNode.getSuperClass();
}
*/
outer_upper: // can't use existsProperty because it calls findMethod
for (ClassNode cn = receiver; cn != null; cn = cn.getSuperClass()) {
property = cn.getProperty(pname);
if (property != null) break outer_upper;
if (!cn.isStaticClass() && cn.getOuterClass() != null
&& typeCheckingContext.getEnclosingClassNodes().contains(cn)) {
ClassNode outer = cn.getOuterClass();
do { property = outer.getProperty(pname);
if (property != null) break outer_upper;
} while (!outer.isStaticClass() && (outer = outer.getOuterClass()) != null);
}
}
property = findProperty(receiver, pname);
// GRECLIPSE end
if (property != null) {
int mods = Opcodes.ACC_PUBLIC | (property.isStatic() ? Opcodes.ACC_STATIC : 0);
Expand All @@ -5517,12 +5506,16 @@ protected List<MethodNode> findMethod(ClassNode receiver, final String name, fin
// maybe we are looking for a setter ?
String pname = extractPropertyNameFromMethodName("set", name);
if (pname != null) {
ClassNode curNode = receiver;
PropertyNode property = null;
/* GRECLIPSE edit -- GROOVY-10414
ClassNode curNode = receiver;
while (property == null && curNode != null) {
property = curNode.getProperty(pname);
curNode = curNode.getSuperClass();
}
*/
property = findProperty(receiver, pname);
// GRECLIPSE end
if (property != null && !Modifier.isFinal(property.getModifiers())) { // GRECLIPSE add
ClassNode type = property.getOriginType();
if (implementsInterfaceOrIsSubclassOf(wrapTypeIfNecessary(args[0]), wrapTypeIfNecessary(type))) {
Expand Down Expand Up @@ -5571,6 +5564,25 @@ protected List<MethodNode> findMethod(ClassNode receiver, final String name, fin
return EMPTY_METHODNODE_LIST;
}

// GRECLIPSE add
private PropertyNode findProperty(final ClassNode receiver, final String name) {
for (ClassNode cn = receiver; cn != null; cn = cn.getSuperClass()) {
PropertyNode property = cn.getProperty(name);
if (property != null) return property;

if (!cn.isStaticClass() && cn.getOuterClass() != null
&& typeCheckingContext.getEnclosingClassNodes().contains(cn)) {
ClassNode outer = cn.getOuterClass();
do {
property = outer.getProperty(name);
if (property != null) return property;
} while (!outer.isStaticClass() && (outer = outer.getOuterClass()) != null);
}
}
return null;
}
// GRECLIPSE end

/**
* Given a method name and a prefix, returns the name of the property that should be looked up,
* following the java beans rules. For example, "getName" would return "name", while
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4814,21 +4814,9 @@ protected List<MethodNode> findMethod(ClassNode receiver, final String name, fin
}
PropertyNode property = null;
if (pname != null) {
outer_upper: // can't use existsProperty because it calls findMethod
for (ClassNode cn = receiver; cn != null; cn = cn.getSuperClass()) {
property = cn.getProperty(pname);
if (property != null) break outer_upper;
if (!cn.isStaticClass() && cn.getOuterClass() != null
&& typeCheckingContext.getEnclosingClassNodes().contains(cn)) {
ClassNode outer = cn.getOuterClass();
do {
property = outer.getProperty(pname);
if (property != null) break outer_upper;
} while (!outer.isStaticClass() && (outer = outer.getOuterClass()) != null);
}
}
} else { // look for property via getGetterName() for non-canonical case
out:
property = findProperty(receiver, pname);
} else {
out: // look for property via getGetterName() for non-canonical case
for (ClassNode cn = receiver; cn != null; cn = cn.getSuperClass()) {
for (PropertyNode pn : cn.getProperties()) {
if (name.equals(pn.getGetterName())) {
Expand All @@ -4848,12 +4836,7 @@ protected List<MethodNode> findMethod(ClassNode receiver, final String name, fin
// maybe we are looking for a setter ?
String pname = extractPropertyNameFromMethodName("set", name);
if (pname != null) {
ClassNode curNode = receiver;
PropertyNode property = null;
while (property == null && curNode != null) {
property = curNode.getProperty(pname);
curNode = curNode.getSuperClass();
}
PropertyNode property = findProperty(receiver, pname);
if (property != null && !Modifier.isFinal(property.getModifiers())) {
ClassNode type = property.getOriginType();
if (implementsInterfaceOrIsSubclassOf(wrapTypeIfNecessary(args[0]), wrapTypeIfNecessary(type))) {
Expand Down Expand Up @@ -4898,6 +4881,23 @@ protected List<MethodNode> findMethod(ClassNode receiver, final String name, fin
return EMPTY_METHODNODE_LIST;
}

private PropertyNode findProperty(final ClassNode receiver, final String name) {
for (ClassNode cn = receiver; cn != null; cn = cn.getSuperClass()) {
PropertyNode property = cn.getProperty(name);
if (property != null) return property;

if (!cn.isStaticClass() && cn.getOuterClass() != null
&& typeCheckingContext.getEnclosingClassNodes().contains(cn)) {
ClassNode outer = cn.getOuterClass();
do {
property = outer.getProperty(name);
if (property != null) return property;
} while (!outer.isStaticClass() && (outer = outer.getOuterClass()) != null);
}
}
return null;
}

/**
* Given a method name and a prefix, returns the name of the property that should be looked up,
* following the java beans rules. For example, "getName" would return "name", while
Expand Down

0 comments on commit c869670

Please sign in to comment.