Skip to content
Permalink
Browse files

Fixes #958 convert constructors of nested classes to public as well (#…

…978)

* Fixes #958 convert constructors of nested classes to public as well

* fix build as jdk11 is not supported yet
  • Loading branch information...
sam-ma authored and thekingnothing committed Apr 21, 2019
1 parent 54f49a5 commit be38fb294a0d5fb9ffb473ff741ac64b61bccfd3
@@ -21,6 +21,7 @@
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.Modifier;
import javassist.NotFoundException;
import org.powermock.core.transformers.TransformStrategy;

import static org.powermock.core.transformers.TransformStrategy.CLASSLOADER;
@@ -41,14 +42,28 @@ public CtClass transform(final CtClass clazz) {
}

if (getStrategy() == CLASSLOADER) {
for (CtConstructor c : clazz.getDeclaredConstructors()) {
transform(new CtClass[]{clazz});
// we also need to transform nested class at this time due to JEP181 since JDK11
// otherwise, we might have trouble during further transformation
// see github #958
try {
CtClass[] nestedClasses = clazz.getDeclaredClasses();
transform(nestedClasses);
} catch (NotFoundException ignored) {
// ignored
}
}
return clazz;
}

private static void transform(final CtClass[] clazzArray) {
for (CtClass nestedClazz : clazzArray) {
for (CtConstructor c : nestedClazz.getDeclaredConstructors()) {
final int modifiers = c.getModifiers();
if (!Modifier.isPublic(modifiers)) {
c.setModifiers(Modifier.setPublic(modifiers));
}
}
}
return clazz;
}

}
@@ -40,6 +40,7 @@ include("tests:java8:mockito-junit4")
include("tests:java8:mockito-junit4-agent")
include("tests:java8:mockito-junit4-rule-xstream")
include("tests:java8:easymock-junit4")
include("tests:java11:mockito-junit4")
include("powermock-release:powermock-easymock")
include("powermock-release:powermock-mockito2")
include("powermock-release:powermock-easymock-junit")
@@ -0,0 +1,42 @@
configure(project.subprojects) { subproject ->

// TODO: uncomment the followings once gradle supports JDK11
// if (JavaVersion.current() != JavaVersion.VERSION_11) {
// project.tasks.all { task -> task.enabled = false }
// }

dependencies {
testCompile("junit:junit:${junitVersion}") {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
}
testCompile("org.hamcrest:hamcrest-core:${hamcrestVersion}")
testCompile("org.assertj:assertj-core:${assertjVersion}")

testCompile files(project(":tests:mockito:junit4").sourceSets.test.output)
}

// TODO: uncomment the followings once JDK11 is supported
// compileJava {
// sourceCompatibility = 11
// targetCompatibility = 11
// }
//
// compileTestJava {
// sourceCompatibility = 11
// targetCompatibility = 11
// }
}

project(":tests:java11:mockito-junit4") {
description = "Tests for Mockito module with JUnit 4.x. and Java11."

dependencies {
testCompile project(":powermock-modules:powermock-module-junit4")

testCompile project(":powermock-api:powermock-api-mockito2")
testCompile ("org.mockito:mockito-core:${mockitoVersion}"){
exclude group: 'net.bytebuddy', module: 'byte-buddy'
exclude group: 'net.bytebuddy', module: 'byte-buddy-agent'
}
}
}
@@ -0,0 +1,9 @@
package samples.powermockito.junit4.bugs.github958;

public class OuterClass {
public static InnerSingleton theInstance = new InnerSingleton();
public static class InnerSingleton {
public String name = "inner";
private InnerSingleton() {}
}
}
@@ -0,0 +1,17 @@
package samples.powermockito.junit4.bugs.github958;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(OuterClass.class)
public class Github958Test {
@Test
public void test() {
assertEquals("inner", OuterClass.theInstance.name);
}
}

0 comments on commit be38fb2

Please sign in to comment.
You can’t perform that action at this time.