Extract static functions to CodegenTestUtil

Add Nullable/NotNull annotations
This commit is contained in:
Alexander Udalov
2013-01-25 15:30:12 +04:00
committed by Alexander Udalov
parent 63aacc416f
commit 9bf73cf4b3
13 changed files with 229 additions and 140 deletions
@@ -25,6 +25,8 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.List; import java.util.List;
import static org.jetbrains.jet.codegen.CodegenTestUtil.findDeclaredMethodByName;
public class ClassGenTest extends CodegenTestCase { public class ClassGenTest extends CodegenTestCase {
@Override @Override
@@ -161,7 +163,7 @@ public class ClassGenTest extends CodegenTestCase {
loadText("abstract class Foo { abstract fun x(): String; fun y(): Int = 0 }"); loadText("abstract class Foo { abstract fun x(): String; fun y(): Int = 0 }");
final Class aClass = generateClass("Foo"); final Class aClass = generateClass("Foo");
assertNotNull(aClass.getMethod("x")); assertNotNull(aClass.getMethod("x"));
assertNotNull(findMethodByName(aClass, "y")); assertNotNull(findDeclaredMethodByName(aClass, "y"));
} }
public void testInheritedMethod() { public void testInheritedMethod() {
@@ -16,9 +16,12 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
public class ClassPathInTheSameClassLoaderTest extends CodegenTestCase { public class ClassPathInTheSameClassLoaderTest extends CodegenTestCase {
@NotNull
@Override @Override
protected GeneratedClassLoader createClassLoader(ClassFileFactory factory) { protected GeneratedClassLoader createClassLoader(@NotNull ClassFileFactory factory) {
initializedClassLoader = new GeneratedClassLoader(factory, CodegenTestCase.class.getClassLoader(), getClassPathURLs()); initializedClassLoader = new GeneratedClassLoader(factory, CodegenTestCase.class.getClassLoader(), getClassPathURLs());
return initializedClassLoader; return initializedClassLoader;
} }
@@ -16,48 +16,38 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.UsefulTestCase; import com.intellij.testFramework.UsefulTestCase;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.ConfigurationKind;
import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.TestJdkKind; import org.jetbrains.jet.TestJdkKind;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys; import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.codegen.state.GenerationState;
import org.jetbrains.jet.codegen.state.Progress;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetPsiUtil; import org.jetbrains.jet.lang.psi.JetPsiUtil;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils; import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.parsing.JetParsingTest; import org.jetbrains.jet.parsing.JetParsingTest;
import org.jetbrains.jet.utils.ExceptionUtils; import org.jetbrains.jet.utils.ExceptionUtils;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import static org.jetbrains.jet.codegen.CodegenTestUtil.*;
public abstract class CodegenTestCase extends UsefulTestCase { public abstract class CodegenTestCase extends UsefulTestCase {
// for environment and classloader // for environment and classloader
protected JetCoreEnvironment myEnvironment; protected JetCoreEnvironment myEnvironment;
protected CodegenTestFiles myFiles; protected CodegenTestFiles myFiles;
private GenerationState alreadyGenerated; private ClassFileFactory classFileFactory;
protected GeneratedClassLoader initializedClassLoader; protected GeneratedClassLoader initializedClassLoader;
protected void createEnvironmentWithMockJdkAndIdeaAnnotations(@NotNull ConfigurationKind configurationKind) { protected void createEnvironmentWithMockJdkAndIdeaAnnotations(@NotNull ConfigurationKind configurationKind) {
@@ -67,22 +57,11 @@ public abstract class CodegenTestCase extends UsefulTestCase {
myEnvironment = JetTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable(), configurationKind); myEnvironment = JetTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(getTestRootDisposable(), configurationKind);
} }
protected static void assertThrows(Method foo, Class<? extends Throwable> exceptionClass, Object instance, Object... args) throws IllegalAccessException {
boolean caught = false;
try {
foo.invoke(instance, args);
}
catch(InvocationTargetException ex) {
caught = exceptionClass.isInstance(ex.getTargetException());
}
assertTrue(caught);
}
@Override @Override
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
myFiles = null; myFiles = null;
myEnvironment = null; myEnvironment = null;
alreadyGenerated = null; classFileFactory = null;
if (initializedClassLoader != null) { if (initializedClassLoader != null) {
initializedClassLoader.dispose(); initializedClassLoader.dispose();
@@ -92,15 +71,17 @@ public abstract class CodegenTestCase extends UsefulTestCase {
super.tearDown(); super.tearDown();
} }
protected void loadText(final String text) { protected void loadText(@NotNull String text) {
myFiles = CodegenTestFiles.create("a.kt", text, myEnvironment.getProject()); myFiles = CodegenTestFiles.create("a.kt", text, myEnvironment.getProject());
} }
protected String loadFile(final String name) { @NotNull
protected String loadFile(@NotNull String name) {
return loadFileByFullPath(JetParsingTest.getTestDataDir() + "/codegen/" + name); return loadFileByFullPath(JetParsingTest.getTestDataDir() + "/codegen/" + name);
} }
protected String loadFileByFullPath(final String fullPath) { @NotNull
protected String loadFileByFullPath(@NotNull String fullPath) {
try { try {
File file = new File(fullPath); File file = new File(fullPath);
final String content = FileUtil.loadFile(file, true); final String content = FileUtil.loadFile(file, true);
@@ -111,7 +92,7 @@ public abstract class CodegenTestCase extends UsefulTestCase {
} }
} }
protected void loadFiles(final String... names) { protected void loadFiles(@NotNull String... names) {
myFiles = CodegenTestFiles.create(myEnvironment.getProject(), names); myFiles = CodegenTestFiles.create(myEnvironment.getProject(), names);
} }
@@ -119,20 +100,21 @@ public abstract class CodegenTestCase extends UsefulTestCase {
loadFile(getPrefix() + "/" + getTestName(true) + ".kt"); loadFile(getPrefix() + "/" + getTestName(true) + ".kt");
} }
@NotNull
protected String getPrefix() { protected String getPrefix() {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
protected void blackBoxFile(String filename) { protected void blackBoxFile(@NotNull String filename) {
blackBoxMultiFile(filename); blackBoxMultiFile(filename);
} }
protected void blackBoxFileByFullPath(String filename) { protected void blackBoxFileByFullPath(@NotNull String filename) {
loadFileByFullPath(filename); loadFileByFullPath(filename);
blackBox(); blackBox();
} }
protected void blackBoxMultiFile(String... filenames) { protected void blackBoxMultiFile(@NotNull String... filenames) {
loadFiles(filenames); loadFiles(filenames);
blackBox(); blackBox();
} }
@@ -161,7 +143,7 @@ public abstract class CodegenTestCase extends UsefulTestCase {
} }
protected void blackBoxFileWithJava(@NotNull String ktFile) { protected void blackBoxFileWithJava(@NotNull String ktFile) {
File javaClassesTempDirectory = compileJava(ktFile.replaceFirst("\\.kt$", ".java")); File javaClassesTempDirectory = CodegenTestUtil.compileJava(ktFile.replaceFirst("\\.kt$", ".java"));
myEnvironment = new JetCoreEnvironment(getTestRootDisposable(), JetTestUtils.compilerConfigurationForTests( myEnvironment = new JetCoreEnvironment(getTestRootDisposable(), JetTestUtils.compilerConfigurationForTests(
ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, JetTestUtils.getAnnotationsJar(), javaClassesTempDirectory)); ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, JetTestUtils.getAnnotationsJar(), javaClassesTempDirectory));
@@ -169,27 +151,8 @@ public abstract class CodegenTestCase extends UsefulTestCase {
blackBoxFile(ktFile); blackBoxFile(ktFile);
} }
protected File compileJava(@NotNull String filename) { @NotNull
try { protected GeneratedClassLoader createClassLoader(@NotNull ClassFileFactory factory) {
File javaClassesTempDirectory = new File(FileUtil.getTempDirectory(), "java-classes");
JetTestUtils.mkdirs(javaClassesTempDirectory);
String classPath = "out/production/runtime" + File.pathSeparator + JetTestUtils.getAnnotationsJar().getPath();
List<String> options = Arrays.asList(
"-classpath", classPath,
"-d", javaClassesTempDirectory.getPath()
);
File javaFile = new File("compiler/testData/codegen/" + filename);
JetTestUtils.compileJavaFiles(Collections.singleton(javaFile), options);
return javaClassesTempDirectory;
}
catch (IOException e) {
throw ExceptionUtils.rethrow(e);
}
}
protected GeneratedClassLoader createClassLoader(ClassFileFactory factory) {
if (initializedClassLoader != null) { if (initializedClassLoader != null) {
fail("Double initialization of class loader in same test"); fail("Double initialization of class loader in same test");
} }
@@ -199,6 +162,7 @@ public abstract class CodegenTestCase extends UsefulTestCase {
return initializedClassLoader; return initializedClassLoader;
} }
@NotNull
protected URL[] getClassPathURLs() { protected URL[] getClassPathURLs() {
List<URL> urls = Lists.newArrayList(); List<URL> urls = Lists.newArrayList();
for (File file : myEnvironment.getConfiguration().getList(JVMConfigurationKeys.CLASSPATH_KEY)) { for (File file : myEnvironment.getConfiguration().getList(JVMConfigurationKeys.CLASSPATH_KEY)) {
@@ -212,40 +176,22 @@ public abstract class CodegenTestCase extends UsefulTestCase {
return urls.toArray(new URL[urls.size()]); return urls.toArray(new URL[urls.size()]);
} }
@NotNull
protected String generateToText() { protected String generateToText() {
if (alreadyGenerated == null) { if (classFileFactory == null) {
alreadyGenerated = generateCommon(myEnvironment, myFiles); classFileFactory = generateFiles(myEnvironment, myFiles);
} }
return alreadyGenerated.getFactory().createText(); return classFileFactory.createText();
} }
@NotNull @NotNull
protected static GenerationState generateCommon(@NotNull JetCoreEnvironment environment, @NotNull CodegenTestFiles files) { protected Class<?> generateNamespaceClass() {
AnalyzeExhaust analyzeExhaust = AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegrationAndCheckForErrors( JvmClassName name = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFiles.getPsiFile()));
environment.getProject(), return generateClass(name.getFqName().getFqName());
files.getPsiFiles(),
files.getScriptParameterTypes(),
Predicates.<PsiFile>alwaysTrue());
analyzeExhaust.throwIfError();
AnalyzingUtils.throwExceptionOnErrors(analyzeExhaust.getBindingContext());
CompilerConfiguration configuration = environment.getConfiguration();
GenerationState state = new GenerationState(
environment.getProject(), ClassBuilderFactories.TEST, Progress.DEAF, analyzeExhaust.getBindingContext(), files.getPsiFiles(),
configuration.get(JVMConfigurationKeys.BUILTIN_TO_JAVA_TYPES_MAPPING_KEY, BuiltinToJavaTypesMapping.ENABLED),
configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, true),
configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, true),
/*generateDeclaredClasses = */true
);
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
return state;
} }
protected Class generateNamespaceClass() { @NotNull
String name = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFiles.getPsiFile())).getFqName().getFqName(); protected Class generateClass(@NotNull String name) {
return generateClass(name);
}
protected Class generateClass(String name) {
try { try {
return createClassLoader(generateClassesInFile()).loadClass(name); return createClassLoader(generateClassesInFile()).loadClass(name);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@@ -256,70 +202,39 @@ public abstract class CodegenTestCase extends UsefulTestCase {
@NotNull @NotNull
protected ClassFileFactory generateClassesInFile() { protected ClassFileFactory generateClassesInFile() {
if (alreadyGenerated == null) { if (classFileFactory == null) {
try { try {
alreadyGenerated = generateCommon(myEnvironment, myFiles); classFileFactory = generateFiles(myEnvironment, myFiles);
if (DxChecker.RUN_DX_CHECKER) { if (DxChecker.RUN_DX_CHECKER) {
DxChecker.check(alreadyGenerated.getFactory()); DxChecker.check(classFileFactory);
} }
} catch (Throwable e) { } catch (Throwable e) {
System.out.println(generateToText()); System.out.println(generateToText());
throw ExceptionUtils.rethrow(e); throw ExceptionUtils.rethrow(e);
} }
} }
return alreadyGenerated.getFactory(); return classFileFactory;
} }
@NotNull
protected Method generateFunction() { protected Method generateFunction() {
Class aClass = generateNamespaceClass(); Class<?> aClass = generateNamespaceClass();
try { try {
Method r = null; return findTheOnlyMethod(aClass);
for (Method method : aClass.getMethods()) {
if (method.getDeclaringClass().equals(Object.class)) {
continue;
}
if (r != null) {
throw new AssertionError("more then one public method in class " + aClass);
}
r = method;
}
if (r == null) {
throw new AssertionError();
}
return r;
} catch (Error e) { } catch (Error e) {
System.out.println(generateToText()); System.out.println(generateToText());
throw e; throw e;
} }
} }
protected Method generateFunction(String name) { @NotNull
Class aClass = generateNamespaceClass(); protected Method generateFunction(@NotNull String name) {
final Method method = findMethodByName(aClass, name); Class<?> aClass = generateNamespaceClass();
Method method = findDeclaredMethodByName(aClass, name);
if (method == null) { if (method == null) {
throw new IllegalArgumentException("couldn't find method " + name); throw new IllegalArgumentException("Couldn't find method " + name + " in class " + aClass);
} }
return method; return method;
} }
@Nullable
protected static Method findMethodByName(Class aClass, String name) {
for (Method method : aClass.getDeclaredMethods()) {
if (method.getName().equals(name)) {
return method;
}
}
return null;
}
protected static void assertIsCurrentTime(long returnValue) {
long currentTime = System.currentTimeMillis();
long diff = Math.abs(returnValue - currentTime);
long toleratedDifference = SystemInfo.isWindows ? 15 : 1;
assertTrue("Difference with current time: " + diff + " (this test is a bad one: it may fail even if the generated code is correct)",
diff <= toleratedDifference);
}
} }
@@ -0,0 +1,140 @@
/*
* 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;
import com.google.common.base.Predicates;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.analyzer.AnalyzeExhaust;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.codegen.state.GenerationState;
import org.jetbrains.jet.codegen.state.Progress;
import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.utils.ExceptionUtils;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static junit.framework.Assert.assertTrue;
public class CodegenTestUtil {
private CodegenTestUtil() {}
@NotNull
public static ClassFileFactory generateFiles(@NotNull JetCoreEnvironment environment, @NotNull CodegenTestFiles files) {
AnalyzeExhaust analyzeExhaust = AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegrationAndCheckForErrors(
environment.getProject(),
files.getPsiFiles(),
files.getScriptParameterTypes(),
Predicates.<PsiFile>alwaysTrue());
analyzeExhaust.throwIfError();
AnalyzingUtils.throwExceptionOnErrors(analyzeExhaust.getBindingContext());
CompilerConfiguration configuration = environment.getConfiguration();
GenerationState state = new GenerationState(
environment.getProject(), ClassBuilderFactories.TEST, Progress.DEAF, analyzeExhaust.getBindingContext(), files.getPsiFiles(),
configuration.get(JVMConfigurationKeys.BUILTIN_TO_JAVA_TYPES_MAPPING_KEY, BuiltinToJavaTypesMapping.ENABLED),
configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_ASSERTIONS, true),
configuration.get(JVMConfigurationKeys.GENERATE_NOT_NULL_PARAMETER_ASSERTIONS, true),
/*generateDeclaredClasses = */true
);
KotlinCodegenFacade.compileCorrectFiles(state, CompilationErrorHandler.THROW_EXCEPTION);
return state.getFactory();
}
public static void assertThrows(@NotNull Method foo, @NotNull Class<? extends Throwable> exceptionClass,
@Nullable Object instance, @NotNull Object... args) throws IllegalAccessException {
boolean caught = false;
try {
foo.invoke(instance, args);
}
catch (InvocationTargetException ex) {
caught = exceptionClass.isInstance(ex.getTargetException());
}
assertTrue(caught);
}
@Nullable
public static Method findDeclaredMethodByName(@NotNull Class<?> aClass, @NotNull String name) {
for (Method method : aClass.getDeclaredMethods()) {
if (method.getName().equals(name)) {
return method;
}
}
return null;
}
public static void assertIsCurrentTime(long returnValue) {
long currentTime = System.currentTimeMillis();
long diff = Math.abs(returnValue - currentTime);
long toleratedDifference = SystemInfo.isWindows ? 15 : 1;
assertTrue("Difference with current time: " + diff + " (this test is a bad one: it may fail even if the generated code is correct)",
diff <= toleratedDifference);
}
@NotNull
public static File compileJava(@NotNull String filename) {
try {
File javaClassesTempDirectory = new File(FileUtil.getTempDirectory(), "java-classes");
JetTestUtils.mkdirs(javaClassesTempDirectory);
String classPath = "out/production/runtime" + File.pathSeparator + JetTestUtils.getAnnotationsJar().getPath();
List<String> options = Arrays.asList(
"-classpath", classPath,
"-d", javaClassesTempDirectory.getPath()
);
File javaFile = new File("compiler/testData/codegen/" + filename);
JetTestUtils.compileJavaFiles(Collections.singleton(javaFile), options);
return javaClassesTempDirectory;
}
catch (IOException e) {
throw ExceptionUtils.rethrow(e);
}
}
@NotNull
public static Method findTheOnlyMethod(@NotNull Class<?> aClass) {
Method r = null;
for (Method method : aClass.getMethods()) {
if (method.getDeclaringClass().equals(Object.class)) {
continue;
}
if (r != null) {
throw new AssertionError("More than one public method in class " + aClass);
}
r = method;
}
if (r == null) {
throw new AssertionError("No public methods in class " + aClass);
}
return r;
}
}
@@ -16,12 +16,15 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.ConfigurationKind;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.jetbrains.jet.codegen.CodegenTestUtil.assertThrows;
public class ControlStructuresTest extends CodegenTestCase { public class ControlStructuresTest extends CodegenTestCase {
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
@@ -29,6 +32,7 @@ public class ControlStructuresTest extends CodegenTestCase {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
} }
@NotNull
@Override @Override
protected String getPrefix() { protected String getPrefix() {
return "controlStructures"; return "controlStructures";
@@ -16,10 +16,13 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.ConfigurationKind;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.jetbrains.jet.codegen.CodegenTestUtil.assertThrows;
public class ExtensionFunctionsTest extends CodegenTestCase { public class ExtensionFunctionsTest extends CodegenTestCase {
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
@@ -27,6 +30,7 @@ public class ExtensionFunctionsTest extends CodegenTestCase {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
} }
@NotNull
@Override @Override
protected String getPrefix() { protected String getPrefix() {
return "extensionFunctions"; return "extensionFunctions";
@@ -27,13 +27,15 @@ import org.jetbrains.jet.TestJdkKind;
import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys; import org.jetbrains.jet.cli.jvm.JVMConfigurationKeys;
import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentUtil; import org.jetbrains.jet.cli.jvm.compiler.CompileEnvironmentUtil;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment; import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.codegen.state.GenerationState;
import org.jetbrains.jet.config.CompilerConfiguration; import org.jetbrains.jet.config.CompilerConfiguration;
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils; import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.FqName;
import java.io.File; import java.io.File;
import static org.jetbrains.jet.codegen.CodegenTestUtil.compileJava;
import static org.jetbrains.jet.codegen.CodegenTestUtil.generateFiles;
public class GenerateNotNullAssertionsTest extends CodegenTestCase { public class GenerateNotNullAssertionsTest extends CodegenTestCase {
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
@@ -78,10 +80,10 @@ public class GenerateNotNullAssertionsTest extends CodegenTestCase {
CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests( CompilerConfiguration configuration = JetTestUtils.compilerConfigurationForTests(
ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK); ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK);
JetCoreEnvironment tmpEnvironment = new JetCoreEnvironment(getTestRootDisposable(), configuration); JetCoreEnvironment tmpEnvironment = new JetCoreEnvironment(getTestRootDisposable(), configuration);
GenerationState state = generateCommon(tmpEnvironment, ClassFileFactory factory = generateFiles(tmpEnvironment,
CodegenTestFiles.create(tmpEnvironment.getProject(), new String[] {"notNullAssertions/noAssertionsForKotlin.kt"})); CodegenTestFiles.create(tmpEnvironment.getProject(), new String[] {"notNullAssertions/noAssertionsForKotlin.kt"}));
File compiledDirectory = new File(FileUtil.getTempDirectory(), "kotlin-classes"); File compiledDirectory = new File(FileUtil.getTempDirectory(), "kotlin-classes");
CompileEnvironmentUtil.writeToOutputDirectory(state.getFactory(), compiledDirectory); CompileEnvironmentUtil.writeToOutputDirectory(factory, compiledDirectory);
setUpEnvironment(true, false, compiledDirectory); setUpEnvironment(true, false, compiledDirectory);
@@ -24,6 +24,8 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import static org.jetbrains.jet.codegen.CodegenTestUtil.assertIsCurrentTime;
public class NamespaceGenTest extends CodegenTestCase { public class NamespaceGenTest extends CodegenTestCase {
@Override @Override
@@ -16,10 +16,13 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.ConfigurationKind;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.jetbrains.jet.codegen.CodegenTestUtil.assertThrows;
public class PatternMatchingTest extends CodegenTestCase { public class PatternMatchingTest extends CodegenTestCase {
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
@@ -27,6 +30,7 @@ public class PatternMatchingTest extends CodegenTestCase {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
} }
@NotNull
@Override @Override
protected String getPrefix() { protected String getPrefix() {
return "patternMatching"; return "patternMatching";
@@ -16,11 +16,15 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.asm4.Opcodes; import org.jetbrains.asm4.Opcodes;
import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.ConfigurationKind;
import java.lang.reflect.*; import java.lang.reflect.*;
import static org.jetbrains.jet.codegen.CodegenTestUtil.assertIsCurrentTime;
import static org.jetbrains.jet.codegen.CodegenTestUtil.findDeclaredMethodByName;
public class PropertyGenTest extends CodegenTestCase { public class PropertyGenTest extends CodegenTestCase {
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
@@ -28,6 +32,7 @@ public class PropertyGenTest extends CodegenTestCase {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
} }
@NotNull
@Override @Override
protected String getPrefix() { protected String getPrefix() {
return "properties"; return "properties";
@@ -46,9 +51,9 @@ public class PropertyGenTest extends CodegenTestCase {
loadFile(); loadFile();
final Class aClass = generateClass("PrivateVar"); final Class aClass = generateClass("PrivateVar");
final Object instance = aClass.newInstance(); final Object instance = aClass.newInstance();
Method setter = findMethodByName(aClass, "setValueOfX"); Method setter = findDeclaredMethodByName(aClass, "setValueOfX");
setter.invoke(instance, 239); setter.invoke(instance, 239);
Method getter = findMethodByName(aClass, "getValueOfX"); Method getter = findDeclaredMethodByName(aClass, "getValueOfX");
assertEquals(239, ((Integer) getter.invoke(instance)).intValue()); assertEquals(239, ((Integer) getter.invoke(instance)).intValue());
} }
@@ -56,17 +61,17 @@ public class PropertyGenTest extends CodegenTestCase {
loadText("class PublicVar() { public var foo : Int = 0; }"); loadText("class PublicVar() { public var foo : Int = 0; }");
final Class aClass = generateClass("PublicVar"); final Class aClass = generateClass("PublicVar");
final Object instance = aClass.newInstance(); final Object instance = aClass.newInstance();
Method setter = findMethodByName(aClass, "setFoo"); Method setter = findDeclaredMethodByName(aClass, "setFoo");
setter.invoke(instance, 239); setter.invoke(instance, 239);
Method getter = findMethodByName(aClass, "getFoo"); Method getter = findDeclaredMethodByName(aClass, "getFoo");
assertEquals(239, ((Integer) getter.invoke(instance)).intValue()); assertEquals(239, ((Integer) getter.invoke(instance)).intValue());
} }
public void testAccessorsInInterface() { public void testAccessorsInInterface() {
loadText("class AccessorsInInterface() { public var foo : Int = 0; }"); loadText("class AccessorsInInterface() { public var foo : Int = 0; }");
final Class aClass = generateClass("AccessorsInInterface"); final Class aClass = generateClass("AccessorsInInterface");
assertNotNull(findMethodByName(aClass, "getFoo")); assertNotNull(findDeclaredMethodByName(aClass, "getFoo"));
assertNotNull(findMethodByName(aClass, "setFoo")); assertNotNull(findDeclaredMethodByName(aClass, "setFoo"));
} }
public void testPrivatePropertyInNamespace() throws Exception { public void testPrivatePropertyInNamespace() throws Exception {
@@ -118,14 +123,14 @@ public class PropertyGenTest extends CodegenTestCase {
loadText("class AccessorsWithoutBody() { protected var foo: Int = 349\n get\n private set\n fun setter() { foo = 610; } } "); loadText("class AccessorsWithoutBody() { protected var foo: Int = 349\n get\n private set\n fun setter() { foo = 610; } } ");
final Class aClass = generateClass("AccessorsWithoutBody"); final Class aClass = generateClass("AccessorsWithoutBody");
final Object instance = aClass.newInstance(); final Object instance = aClass.newInstance();
final Method getFoo = findMethodByName(aClass, "getFoo"); final Method getFoo = findDeclaredMethodByName(aClass, "getFoo");
getFoo.setAccessible(true); getFoo.setAccessible(true);
assertTrue((getFoo.getModifiers() & Modifier.PROTECTED) != 0); assertTrue((getFoo.getModifiers() & Modifier.PROTECTED) != 0);
assertEquals(349, getFoo.invoke(instance)); assertEquals(349, getFoo.invoke(instance));
final Method setFoo = findMethodByName(aClass, "setFoo"); final Method setFoo = findDeclaredMethodByName(aClass, "setFoo");
setFoo.setAccessible(true); setFoo.setAccessible(true);
assertTrue((setFoo.getModifiers() & Modifier.PRIVATE) != 0); assertTrue((setFoo.getModifiers() & Modifier.PRIVATE) != 0);
final Method setter = findMethodByName(aClass, "setter"); final Method setter = findDeclaredMethodByName(aClass, "setter");
setter.invoke(instance); setter.invoke(instance);
assertEquals(610, getFoo.invoke(instance)); assertEquals(610, getFoo.invoke(instance));
} }
@@ -16,6 +16,7 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.ConfigurationKind;
import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.TestJdkKind; import org.jetbrains.jet.TestJdkKind;
@@ -49,8 +50,9 @@ public class StdlibTest extends CodegenTestCase {
ConfigurationKind.ALL, TestJdkKind.FULL_JDK, JetTestUtils.getAnnotationsJar(), junitJar)); ConfigurationKind.ALL, TestJdkKind.FULL_JDK, JetTestUtils.getAnnotationsJar(), junitJar));
} }
@NotNull
@Override @Override
protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) { protected GeneratedClassLoader createClassLoader(@NotNull ClassFileFactory codegens) {
try { try {
return new GeneratedClassLoader( return new GeneratedClassLoader(
codegens, codegens,
@@ -16,6 +16,7 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.ConfigurationKind;
public class TraitsTest extends CodegenTestCase { public class TraitsTest extends CodegenTestCase {
@@ -26,6 +27,7 @@ public class TraitsTest extends CodegenTestCase {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
} }
@NotNull
@Override @Override
protected String getPrefix() { protected String getPrefix() {
return "traits"; return "traits";
@@ -16,10 +16,13 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.ConfigurationKind; import org.jetbrains.jet.ConfigurationKind;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.jetbrains.jet.codegen.CodegenTestUtil.assertThrows;
public class TypeInfoTest extends CodegenTestCase { public class TypeInfoTest extends CodegenTestCase {
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
@@ -27,6 +30,7 @@ public class TypeInfoTest extends CodegenTestCase {
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
} }
@NotNull
@Override @Override
protected String getPrefix() { protected String getPrefix() {
return "typeInfo"; return "typeInfo";