Skip to content

Commit

Permalink
Extract classPathInTheSameClassLoader into a separate test
Browse files Browse the repository at this point in the history
- class loader is now always created as if classPathInTheSameClassLoader
  = false, which was the case of every codegen test except kt2781, which
  is extracted into a separate file
- testNoClassObjectForJavaClass() is moved from StdlibTest since it has
  a custom createClassLoader() which doesn't work well in a java-interop
  case
- get rid of generateClassesInFileGetState
- several similar methods are inlined
  • Loading branch information
udalov committed Jan 28, 2013
1 parent 639aa9e commit 63aacc4
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 108 deletions.
15 changes: 7 additions & 8 deletions compiler/tests/org/jetbrains/jet/codegen/ClassGenTest.java
Expand Up @@ -36,15 +36,15 @@ protected void setUp() throws Exception {
public void testPSVMClass() {
loadFile("classes/simpleClass.kt");

final Class aClass = loadClass("SimpleClass", generateClassesInFile());
final Class aClass = generateClass("SimpleClass");
final Method[] methods = aClass.getDeclaredMethods();
// public int SimpleClass.foo()
assertEquals(1, methods.length);
}

public void testArrayListInheritance() throws Exception {
loadFile("classes/inheritingFromArrayList.kt");
final Class aClass = loadClass("Foo", generateClassesInFile());
final Class aClass = generateClass("Foo");
assertInstanceOf(aClass.newInstance(), List.class);
}

Expand Down Expand Up @@ -159,8 +159,7 @@ public void testInitializerBlock() {

public void testAbstractMethod() throws Exception {
loadText("abstract class Foo { abstract fun x(): String; fun y(): Int = 0 }");
final ClassFileFactory codegens = generateClassesInFile();
final Class aClass = loadClass("Foo", codegens);
final Class aClass = generateClass("Foo");
assertNotNull(aClass.getMethod("x"));
assertNotNull(findMethodByName(aClass, "y"));
}
Expand Down Expand Up @@ -491,11 +490,11 @@ public void testKt2626() {
blackBoxFile("regressions/kt2626.kt");
}

public void testKt2781() throws Exception {
blackBoxFileWithJava("regressions/kt2781.kt", true);
}

public void testKt2607() {
blackBoxFile("regressions/kt2607.kt");
}

public void testNoClassObjectForJavaClass() {
blackBoxFileWithJava("stdlib/noClassObjectForJavaClass.kt");
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2013 JetBrains s.r.o.
*
* Licensed 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.jetbrains.jet.codegen;

public class ClassPathInTheSameClassLoaderTest extends CodegenTestCase {
@Override
protected GeneratedClassLoader createClassLoader(ClassFileFactory factory) {
initializedClassLoader = new GeneratedClassLoader(factory, CodegenTestCase.class.getClassLoader(), getClassPathURLs());
return initializedClassLoader;
}

public void testKt2781() {
blackBoxFileWithJava("regressions/kt2781.kt");
}
}
96 changes: 29 additions & 67 deletions compiler/tests/org/jetbrains/jet/codegen/CodegenTestCase.java
Expand Up @@ -58,7 +58,7 @@ public abstract class CodegenTestCase extends UsefulTestCase {
protected CodegenTestFiles myFiles;

private GenerationState alreadyGenerated;
private GeneratedClassLoader initializedClassLoader;
protected GeneratedClassLoader initializedClassLoader;

protected void createEnvironmentWithMockJdkAndIdeaAnnotations(@NotNull ConfigurationKind configurationKind) {
if (myEnvironment != null) {
Expand Down Expand Up @@ -124,27 +124,22 @@ protected String getPrefix() {
}

protected void blackBoxFile(String filename) {
blackBoxFile(filename, false);
}

private void blackBoxFile(String filename, boolean classPathInTheSameClassLoader) {
loadFile(filename);
blackBox(classPathInTheSameClassLoader);
blackBoxMultiFile(filename);
}

protected void blackBoxFileByFullPath(String filename) {
loadFileByFullPath(filename);
blackBox(false);
blackBox();
}

protected void blackBoxMultiFile(String... filenames) {
loadFiles(filenames);
blackBox(false);
blackBox();
}

private void blackBox(boolean classPathInTheSameClassLoader) {
private void blackBox() {
ClassFileFactory factory = generateClassesInFile();
GeneratedClassLoader loader = createClassLoader(factory, classPathInTheSameClassLoader);
GeneratedClassLoader loader = createClassLoader(factory);

JetFile firstFile = myFiles.getPsiFiles().get(0);
String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(firstFile)).getFqName().getFqName();
Expand All @@ -162,20 +157,16 @@ private void blackBox(boolean classPathInTheSameClassLoader) {
}

protected void blackBoxFileWithJavaByFullPath(@NotNull String ktFile) {
blackBoxFileWithJava(ktFile.substring("compiler/testData/codegen/".length()), false);
blackBoxFileWithJava(ktFile.substring("compiler/testData/codegen/".length()));
}

protected void blackBoxFileWithJava(@NotNull String ktFile) {
blackBoxFileWithJava(ktFile, false);
}

protected void blackBoxFileWithJava(@NotNull String ktFile, boolean classPathInTheSameClassLoader) {
File javaClassesTempDirectory = compileJava(ktFile.replaceFirst("\\.kt$", ".java"));

myEnvironment = new JetCoreEnvironment(getTestRootDisposable(), JetTestUtils.compilerConfigurationForTests(
ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, JetTestUtils.getAnnotationsJar(), javaClassesTempDirectory));

blackBoxFile(ktFile, classPathInTheSameClassLoader);
blackBoxFile(ktFile);
}

protected File compileJava(@NotNull String filename) {
Expand All @@ -198,15 +189,17 @@ protected File compileJava(@NotNull String filename) {
}
}

protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) {
return createClassLoader(codegens, false);
}

protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens, boolean classPathInTheSameClassLoader) {
protected GeneratedClassLoader createClassLoader(ClassFileFactory factory) {
if (initializedClassLoader != null) {
fail("Double initialization of class loader in same test");
}

ClassLoader parentClassLoader = new URLClassLoader(getClassPathURLs(), CodegenTestCase.class.getClassLoader());
initializedClassLoader = new GeneratedClassLoader(factory, parentClassLoader);
return initializedClassLoader;
}

protected URL[] getClassPathURLs() {
List<URL> urls = Lists.newArrayList();
for (File file : myEnvironment.getConfiguration().getList(JVMConfigurationKeys.CLASSPATH_KEY)) {
try {
Expand All @@ -216,17 +209,7 @@ protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens, bool
}
}

final URL[] urlsArray = urls.toArray(new URL[urls.size()]);

if (!classPathInTheSameClassLoader) {
ClassLoader parentClassLoader = new URLClassLoader(urlsArray, CodegenTestCase.class.getClassLoader());
initializedClassLoader = new GeneratedClassLoader(codegens, parentClassLoader);
}
else {
initializedClassLoader = new GeneratedClassLoader(codegens, CodegenTestCase.class.getClassLoader(), urlsArray);
}

return initializedClassLoader;
return urls.toArray(new URL[urls.size()]);
}

protected String generateToText() {
Expand Down Expand Up @@ -258,55 +241,34 @@ protected static GenerationState generateCommon(@NotNull JetCoreEnvironment envi
}

protected Class generateNamespaceClass() {
ClassFileFactory state = generateClassesInFile();
return loadRootNamespaceClass(state);
String name = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFiles.getPsiFile())).getFqName().getFqName();
return generateClass(name);
}

protected Class generateClass(String name) {
ClassFileFactory state = generateClassesInFile();
return loadClass(name, state);
}

protected Class loadRootNamespaceClass(@NotNull ClassFileFactory state) {
String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFiles.getPsiFile())).getFqName().getFqName();
try {
return createClassLoader(state).loadClass(fqName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}

protected Class loadClass(String fqName, @NotNull ClassFileFactory state) {
try {
return createClassLoader(state).loadClass(fqName);
return createClassLoader(generateClassesInFile()).loadClass(name);
} catch (ClassNotFoundException e) {
fail("No classfile was generated for: " + fqName);
fail("No class file was generated for: " + name);
return null;
}
}

@NotNull
protected ClassFileFactory generateClassesInFile() {
return generateClassesInFileGetState().getFactory();
}

@NotNull
protected GenerationState generateClassesInFileGetState() {
try {
if (alreadyGenerated == null) {
if (alreadyGenerated == null) {
try {
alreadyGenerated = generateCommon(myEnvironment, myFiles);
}

if (DxChecker.RUN_DX_CHECKER) {
DxChecker.check(alreadyGenerated.getFactory());
if (DxChecker.RUN_DX_CHECKER) {
DxChecker.check(alreadyGenerated.getFactory());
}
} catch (Throwable e) {
System.out.println(generateToText());
throw ExceptionUtils.rethrow(e);
}

return alreadyGenerated;
} catch (Throwable e) {
System.out.println(generateToText());
throw ExceptionUtils.rethrow(e);
}
return alreadyGenerated.getFactory();
}

protected Method generateFunction() {
Expand Down
10 changes: 5 additions & 5 deletions compiler/tests/org/jetbrains/jet/codegen/EnumGenTest.java
Expand Up @@ -33,21 +33,21 @@ protected void setUp() throws Exception {

public void testSuperclassIsEnum() throws NoSuchFieldException, IllegalAccessException {
loadFile("enum/simple.kt");
Class season = loadClass("Season", generateClassesInFile());
Class season = generateClass("Season");
assertEquals("java.lang.Enum", season.getSuperclass().getName());
}

public void testEnumClassModifiers() throws NoSuchFieldException, IllegalAccessException {
loadFile("enum/simple.kt");
Class season = loadClass("Season", generateClassesInFile());
Class season = generateClass("Season");
int modifiers = season.getModifiers();
assertTrue((modifiers & 0x4000) != 0); // ACC_ENUM
assertTrue((modifiers & Modifier.FINAL) != 0);
}

public void testEnumFieldModifiers() throws NoSuchFieldException, IllegalAccessException {
loadFile("enum/simple.kt");
Class season = loadClass("Season", generateClassesInFile());
Class season = generateClass("Season");
Field summer = season.getField("SUMMER");
int modifiers = summer.getModifiers();
assertTrue((modifiers & 0x4000) != 0); // ACC_ENUM
Expand Down Expand Up @@ -120,15 +120,15 @@ public void testSimpleJavaEnumWithFunction() throws Exception {
public void testNoClassForSimpleEnum()
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
loadFile("enum/name.kt");
Class cls = loadClass("State", generateClassesInFile());
Class cls = generateClass("State");
Field field = cls.getField("O");
assertEquals("State", field.get(null).getClass().getName());
}

public void testYesClassForComplexEnum()
throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
loadFile("enum/abstractmethod.kt");
Class cls = loadClass("IssueState", generateClassesInFile());
Class cls = generateClass("IssueState");
Field field = cls.getField("DEFAULT");
assertEquals("IssueState", field.get(null).getClass().getName());
field = cls.getField("FIXED");
Expand Down
24 changes: 11 additions & 13 deletions compiler/tests/org/jetbrains/jet/codegen/PropertyGenTest.java
Expand Up @@ -35,7 +35,7 @@ protected String getPrefix() {

public void testPrivateVal() throws Exception {
loadFile();
final Class aClass = loadClass("PrivateVal", generateClassesInFile());
final Class aClass = generateClass("PrivateVal");
final Field[] fields = aClass.getDeclaredFields();
assertEquals(1, fields.length); // prop
final Field field = fields[0];
Expand All @@ -44,7 +44,7 @@ public void testPrivateVal() throws Exception {

public void testPrivateVar() throws Exception {
loadFile();
final Class aClass = loadClass("PrivateVar", generateClassesInFile());
final Class aClass = generateClass("PrivateVar");
final Object instance = aClass.newInstance();
Method setter = findMethodByName(aClass, "setValueOfX");
setter.invoke(instance, 239);
Expand All @@ -54,7 +54,7 @@ public void testPrivateVar() throws Exception {

public void testPublicVar() throws Exception {
loadText("class PublicVar() { public var foo : Int = 0; }");
final Class aClass = loadClass("PublicVar", generateClassesInFile());
final Class aClass = generateClass("PublicVar");
final Object instance = aClass.newInstance();
Method setter = findMethodByName(aClass, "setFoo");
setter.invoke(instance, 239);
Expand All @@ -64,7 +64,7 @@ public void testPublicVar() throws Exception {

public void testAccessorsInInterface() {
loadText("class AccessorsInInterface() { public var foo : Int = 0; }");
final Class aClass = loadClass("AccessorsInInterface", generateClassesInFile());
final Class aClass = generateClass("AccessorsInInterface");
assertNotNull(findMethodByName(aClass, "getFoo"));
assertNotNull(findMethodByName(aClass, "setFoo"));
}
Expand Down Expand Up @@ -116,7 +116,7 @@ public void testFieldSetterPlusEq() throws Exception {

public void testAccessorsWithoutBody() throws Exception {
loadText("class AccessorsWithoutBody() { protected var foo: Int = 349\n get\n private set\n fun setter() { foo = 610; } } ");
final Class aClass = loadClass("AccessorsWithoutBody", generateClassesInFile());
final Class aClass = generateClass("AccessorsWithoutBody");
final Object instance = aClass.newInstance();
final Method getFoo = findMethodByName(aClass, "getFoo");
getFoo.setAccessible(true);
Expand All @@ -139,7 +139,7 @@ public void testInitializersForNamespaceProperties() throws Exception {

public void testPropertyReceiverOnStack() throws Exception {
loadFile();
final Class aClass = loadClass("Evaluator", generateClassesInFile());
final Class aClass = generateClass("Evaluator");
final Constructor constructor = aClass.getConstructor(StringBuilder.class);
StringBuilder sb = new StringBuilder("xyzzy");
final Object instance = constructor.newInstance(sb);
Expand All @@ -150,15 +150,13 @@ public void testPropertyReceiverOnStack() throws Exception {

public void testAbstractVal() throws Exception {
loadText("abstract class Foo { public abstract val x: String }");
final ClassFileFactory codegens = generateClassesInFile();
final Class aClass = loadClass("Foo", codegens);
final Class aClass = generateClass("Foo");
assertNotNull(aClass.getMethod("getX"));
}

public void testVolatileProperty() throws Exception {
loadText("abstract class Foo { public volatile var x: String = \"\"; }");
final ClassFileFactory codegens = generateClassesInFile();
final Class aClass = loadClass("Foo", codegens);
final Class aClass = generateClass("Foo");
Field x = aClass.getDeclaredField("x");
assertTrue((x.getModifiers() & Modifier.VOLATILE) != 0);
}
Expand Down Expand Up @@ -212,7 +210,7 @@ public void testKt1892() {

public void testKt1846() {
loadFile("regressions/kt1846.kt");
final Class aClass = loadClass("A", generateClassesInFile());
final Class aClass = generateClass("A");
try {
Method v1 = aClass.getMethod("getV1");
System.out.println(generateToText());
Expand Down Expand Up @@ -256,7 +254,7 @@ public void testKt1528() {

public void testKt2589() {
loadFile("regressions/kt2589.kt");
final Class aClass = loadClass("Foo", generateClassesInFile());
final Class aClass = generateClass("Foo");
assertTrue((aClass.getModifiers() & Opcodes.ACC_FINAL) == 0);

try {
Expand Down Expand Up @@ -284,7 +282,7 @@ public void testKt2589() {

public void testKt2677() {
loadFile("regressions/kt2677.kt");
final Class aClass = loadClass("DerivedWeatherReport", generateClassesInFile());
final Class aClass = generateClass("DerivedWeatherReport");
final Class bClass = aClass.getSuperclass();

try {
Expand Down

0 comments on commit 63aacc4

Please sign in to comment.