9d021ee1ac
Perform actual codegen test execution in separate process. One server process is used to run all codegen tests through socket connection.
80 lines
3.0 KiB
Java
80 lines
3.0 KiB
Java
/*
|
|
* Copyright 2010-2016 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.kotlin.codegen;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil;
|
|
import org.jetbrains.kotlin.psi.KtDeclaration;
|
|
import org.jetbrains.kotlin.psi.KtFile;
|
|
import org.jetbrains.kotlin.psi.KtNamedFunction;
|
|
import org.jetbrains.kotlin.psi.KtProperty;
|
|
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
|
|
|
|
import java.io.File;
|
|
import java.lang.reflect.Method;
|
|
import java.util.List;
|
|
|
|
import static org.jetbrains.kotlin.codegen.TestUtilsKt.clearReflectionCache;
|
|
import static org.jetbrains.kotlin.codegen.TestUtilsKt.getBoxMethodOrNull;
|
|
import static org.jetbrains.kotlin.codegen.TestUtilsKt.getGeneratedClass;
|
|
|
|
public abstract class AbstractBlackBoxCodegenTest extends CodegenTestCase {
|
|
|
|
@Override
|
|
protected void doMultiFileTest(@NotNull File wholeFile, @NotNull List<TestFile> files, @Nullable File javaFilesDir) throws Exception {
|
|
compile(files, javaFilesDir);
|
|
blackBox();
|
|
}
|
|
|
|
protected void blackBox() {
|
|
// If there are many files, the first 'box(): String' function will be executed.
|
|
GeneratedClassLoader generatedClassLoader = generateAndCreateClassLoader();
|
|
for (KtFile firstFile : myFiles.getPsiFiles()) {
|
|
String className = getFacadeFqName(firstFile);
|
|
if (className == null) continue;
|
|
Class<?> aClass = getGeneratedClass(generatedClassLoader, className);
|
|
try {
|
|
Method method = getBoxMethodOrNull(aClass);
|
|
if (method != null) {
|
|
callBoxMethodAndCheckResult(generatedClassLoader, aClass, method);
|
|
return;
|
|
}
|
|
}
|
|
catch (Throwable e) {
|
|
System.out.println(generateToText());
|
|
throw ExceptionUtilsKt.rethrow(e);
|
|
}
|
|
finally {
|
|
clearReflectionCache(generatedClassLoader);
|
|
}
|
|
}
|
|
fail("Can't find box method!");
|
|
}
|
|
|
|
|
|
@Nullable
|
|
private static String getFacadeFqName(@NotNull KtFile firstFile) {
|
|
for (KtDeclaration declaration : firstFile.getDeclarations()) {
|
|
if (declaration instanceof KtProperty || declaration instanceof KtNamedFunction) {
|
|
return JvmFileClassUtil.getFileClassInfoNoResolve(firstFile).getFacadeClassFqName().asString();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|