Skip to content

Commit

Permalink
GROOVY-8144: Invoking a public method declared in a non-public class …
Browse files Browse the repository at this point in the history
…result in a IllegalAccessError

Commit 1a4c991 introduced the DecompiledClassNode as part of
enabling the ASM class resolver.
  • Loading branch information
jwagenleitner committed Apr 15, 2017
1 parent e06a1ea commit f79c007
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 1 deletion.
Expand Up @@ -27,6 +27,7 @@
import org.codehaus.groovy.ast.InnerClassNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.decompiled.DecompiledClassNode;
import org.codehaus.groovy.ast.expr.*;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.ForStatement;
Expand Down Expand Up @@ -706,7 +707,9 @@ public ClassNode getType() {
} else {
type = getWrapper(controller.getTypeChooser().resolveType(receiver, controller.getClassNode()));
ClassNode declaringClass = target.getDeclaringClass();
if (type.getClass() != ClassNode.class && type.getClass() !=InnerClassNode.class) {
if (type.getClass() != ClassNode.class
&& type.getClass() != InnerClassNode.class
&& type.getClass() != DecompiledClassNode.class) {
type = declaringClass; // ex: LUB type
}
if (OBJECT_TYPE.equals(type) && !OBJECT_TYPE.equals(declaringClass)) {
Expand Down
86 changes: 86 additions & 0 deletions src/test/groovy/bugs/Groovy8144Bug.groovy
@@ -0,0 +1,86 @@
/*
* 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

import org.codehaus.groovy.classgen.asm.AbstractBytecodeTestCase

/**
* This test requires the test classes executed are compiled and on the
* classpath and not in the same compilation unit.
*/
class Groovy8144Bug extends AbstractBytecodeTestCase {

void testMethodInheritedFromNonPublicAbstractBaseClass() {
String code = '''
import org.codehaus.groovy.dummy.Groovy8144A
@groovy.transform.CompileStatic
def m() {
new Groovy8144A().answer()
}
assert m() == 42
'''

assert compile(method:'m', code).hasSequence([
'INVOKEVIRTUAL org/codehaus/groovy/dummy/Groovy8144A.answer ()I'
])

assertScript(code)
}

void testMethodInheritedFromPublicAbstractBaseClass() {
String code = '''
import org.codehaus.groovy.dummy.Groovy8144B
@groovy.transform.CompileStatic
def m() {
new Groovy8144B().answer()
}
assert m() == 42
'''

assert compile(method:'m', code).hasSequence([
'INVOKEVIRTUAL org/codehaus/groovy/dummy/Groovy8144B.answer ()I'
])

assertScript(code)
}

void testMethodInheritedFromPublicBaseClass() {
String code = '''
import org.codehaus.groovy.dummy.Groovy8144C
@groovy.transform.CompileStatic
def m() {
new Groovy8144C().answer()
}
assert m() == 42
'''

assert compile(method:'m', code).hasSequence([
'INVOKEVIRTUAL org/codehaus/groovy/dummy/Groovy8144C.answer ()I'
])

assertScript(code)
}

}
26 changes: 26 additions & 0 deletions src/test/org/codehaus/groovy/dummy/Groovy8144A.java
@@ -0,0 +1,26 @@
/*
* 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.dummy;

/**
* Inherits a public method from a package-private base class and is used to verify
* the generated INVOKEVIRTUAL call is on this and not super.
*/
public class Groovy8144A extends Groovy8144ABase {
}
25 changes: 25 additions & 0 deletions src/test/org/codehaus/groovy/dummy/Groovy8144ABase.java
@@ -0,0 +1,25 @@
/*
* 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.dummy;

abstract class Groovy8144ABase {
public int answer() {
return 42;
}
}
26 changes: 26 additions & 0 deletions src/test/org/codehaus/groovy/dummy/Groovy8144B.java
@@ -0,0 +1,26 @@
/*
* 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.dummy;

/**
* Inherits a public method from a public abstract base class and is used to verify
* the generated INVOKEVIRTUAL call is on this and not super.
*/
public class Groovy8144B extends Groovy8144BBase {
}
23 changes: 23 additions & 0 deletions src/test/org/codehaus/groovy/dummy/Groovy8144BBase.java
@@ -0,0 +1,23 @@
/*
* 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.dummy;

public abstract class Groovy8144BBase {
public int answer() { return 42; }
}
26 changes: 26 additions & 0 deletions src/test/org/codehaus/groovy/dummy/Groovy8144C.java
@@ -0,0 +1,26 @@
/*
* 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.dummy;

/**
* Inherits a public method from a public base class and is used to verify
* the generated INVOKEVIRTUAL call is on this and not super.
*/
public class Groovy8144C extends Groovy8144CBase {
}
23 changes: 23 additions & 0 deletions src/test/org/codehaus/groovy/dummy/Groovy8144CBase.java
@@ -0,0 +1,23 @@
/*
* 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.dummy;

public class Groovy8144CBase {
public int answer() { return 42; }
}

0 comments on commit f79c007

Please sign in to comment.