Search in sources :

Example 11 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class StdlibTest method testStdlib.

public void testStdlib() throws ClassNotFoundException {
    GenerationState state = KotlinToJVMBytecodeCompiler.INSTANCE.analyzeAndGenerate(getEnvironment());
    if (state == null) {
        fail("There were compilation errors");
    }
    classLoader = new GeneratedClassLoader(state.getFactory(), ForTestCompileRuntime.runtimeAndReflectJarClassLoader()) {

        @Override
        public Class<?> loadClass(@NotNull String name) throws ClassNotFoundException {
            if (name.startsWith("junit.") || name.startsWith("org.junit.")) {
                return StdlibTest.class.getClassLoader().loadClass(name);
            }
            return super.loadClass(name);
        }
    };
    TestSuite tests = new TestSuite("Standard Library Tests");
    for (KtFile file : getEnvironment().getSourceFiles()) {
        // Skip JS tests
        if (file.getVirtualFile().getPath().contains("/js/"))
            continue;
        for (KtDeclaration declaration : file.getDeclarations()) {
            if (!(declaration instanceof KtClass))
                continue;
            ClassDescriptor descriptor = (ClassDescriptor) BindingContextUtils.getNotNull(state.getBindingContext(), BindingContext.DECLARATION_TO_DESCRIPTOR, declaration);
            Test test = createTest(classLoader, state.getTypeMapper().mapClass(descriptor).getClassName());
            if (test != null) {
                tests.addTest(test);
            }
        }
    }
    TestResult result = new TestRunner(System.err).doRun(tests);
    if (!result.wasSuccessful()) {
        fail("Some stdlib tests failed, see stderr for details");
    }
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) TestRunner(junit.textui.TestRunner) KtClass(org.jetbrains.kotlin.psi.KtClass) GenerationState(org.jetbrains.kotlin.codegen.state.GenerationState) KtDeclaration(org.jetbrains.kotlin.psi.KtDeclaration) KtClass(org.jetbrains.kotlin.psi.KtClass) KtFile(org.jetbrains.kotlin.psi.KtFile)

Example 12 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class ScriptCodegen method genConstructor.

private void genConstructor(@NotNull ScriptDescriptor scriptDescriptor, @NotNull ClassBuilder classBuilder, @NotNull MethodContext methodContext) {
    JvmMethodSignature jvmSignature = typeMapper.mapScriptSignature(scriptDescriptor, context.getEarlierScripts());
    if (state.getReplSpecific().getShouldGenerateScriptResultValue()) {
        FieldInfo resultFieldInfo = context.getResultFieldInfo();
        classBuilder.newField(JvmDeclarationOrigin.NO_ORIGIN, ACC_PUBLIC | ACC_FINAL, resultFieldInfo.getFieldName(), resultFieldInfo.getFieldType().getDescriptor(), null, null);
    }
    MethodVisitor mv = classBuilder.newMethod(JvmDeclarationOriginKt.OtherOrigin(scriptDeclaration, scriptDescriptor.getUnsubstitutedPrimaryConstructor()), ACC_PUBLIC, jvmSignature.getAsmMethod().getName(), jvmSignature.getAsmMethod().getDescriptor(), null, null);
    if (state.getClassBuilderMode().generateBodies) {
        mv.visitCode();
        InstructionAdapter iv = new InstructionAdapter(mv);
        Type classType = typeMapper.mapType(scriptDescriptor);
        ClassDescriptor superclass = DescriptorUtilsKt.getSuperClassNotAny(scriptDescriptor);
        if (superclass == null) {
            iv.load(0, classType);
            iv.invokespecial("java/lang/Object", "<init>", "()V", false);
        } else {
            ConstructorDescriptor ctorDesc = superclass.getUnsubstitutedPrimaryConstructor();
            if (ctorDesc == null)
                throw new RuntimeException("Primary constructor not found for script template " + superclass.toString());
            iv.load(0, classType);
            int valueParamStart = context.getEarlierScripts().size() + 1;
            List<ValueParameterDescriptor> valueParameters = scriptDescriptor.getUnsubstitutedPrimaryConstructor().getValueParameters();
            for (ValueParameterDescriptor superclassParam : ctorDesc.getValueParameters()) {
                ValueParameterDescriptor valueParam = null;
                for (ValueParameterDescriptor vpd : valueParameters) {
                    if (vpd.getName().equals(superclassParam.getName())) {
                        valueParam = vpd;
                        break;
                    }
                }
                assert valueParam != null;
                iv.load(valueParam.getIndex() + valueParamStart, typeMapper.mapType(valueParam.getType()));
            }
            CallableMethod ctorMethod = typeMapper.mapToCallableMethod(ctorDesc, false);
            String sig = ctorMethod.getAsmMethod().getDescriptor();
            iv.invokespecial(typeMapper.mapSupertype(superclass.getDefaultType(), null).getInternalName(), "<init>", sig, false);
        }
        iv.load(0, classType);
        FrameMap frameMap = new FrameMap();
        frameMap.enterTemp(OBJECT_TYPE);
        for (ScriptDescriptor importedScript : context.getEarlierScripts()) {
            frameMap.enter(importedScript, OBJECT_TYPE);
        }
        int offset = 1;
        for (ScriptDescriptor earlierScript : context.getEarlierScripts()) {
            Type earlierClassType = typeMapper.mapClass(earlierScript);
            iv.load(0, classType);
            iv.load(offset, earlierClassType);
            offset += earlierClassType.getSize();
            iv.putfield(classType.getInternalName(), context.getScriptFieldName(earlierScript), earlierClassType.getDescriptor());
        }
        final ExpressionCodegen codegen = new ExpressionCodegen(mv, frameMap, Type.VOID_TYPE, methodContext, state, this);
        generateInitializers(new Function0<ExpressionCodegen>() {

            @Override
            public ExpressionCodegen invoke() {
                return codegen;
            }
        });
        iv.areturn(Type.VOID_TYPE);
    }
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ConstructorDescriptor(org.jetbrains.kotlin.descriptors.ConstructorDescriptor) ScriptDescriptor(org.jetbrains.kotlin.descriptors.ScriptDescriptor) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) JvmMethodSignature(org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature) Type(org.jetbrains.org.objectweb.asm.Type) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)

Example 13 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class ResolveSessionUtils method findClassByRelativePath.

@Nullable
public static ClassDescriptor findClassByRelativePath(@NotNull MemberScope packageScope, @NotNull FqName path) {
    if (path.isRoot())
        return null;
    MemberScope scope = packageScope;
    ClassifierDescriptor classifier = null;
    for (Name name : path.pathSegments()) {
        classifier = scope.getContributedClassifier(name, NoLookupLocation.WHEN_FIND_BY_FQNAME);
        if (!(classifier instanceof ClassDescriptor))
            return null;
        scope = ((ClassDescriptor) classifier).getUnsubstitutedInnerClassesScope();
    }
    return (ClassDescriptor) classifier;
}
Also used : ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class JetTestFunctionDetector method getTestFunctions.

@NotNull
private static List<FunctionDescriptor> getTestFunctions(@NotNull BindingContext bindingContext, @NotNull List<KtDeclaration> declarations) {
    List<FunctionDescriptor> answer = Lists.newArrayList();
    for (KtDeclaration declaration : declarations) {
        MemberScope scope = null;
        if (declaration instanceof KtClass) {
            KtClass klass = (KtClass) declaration;
            ClassDescriptor classDescriptor = BindingUtils.getClassDescriptor(bindingContext, klass);
            if (classDescriptor.getModality() != Modality.ABSTRACT) {
                scope = classDescriptor.getDefaultType().getMemberScope();
            }
        }
        if (scope != null) {
            Collection<DeclarationDescriptor> allDescriptors = DescriptorUtils.getAllDescriptors(scope);
            List<FunctionDescriptor> testFunctions = ContainerUtil.mapNotNull(allDescriptors, new Function<DeclarationDescriptor, FunctionDescriptor>() {

                @Override
                public FunctionDescriptor fun(DeclarationDescriptor descriptor) {
                    if (descriptor instanceof FunctionDescriptor) {
                        FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
                        if (isTest(functionDescriptor))
                            return functionDescriptor;
                    }
                    return null;
                }
            });
            answer.addAll(testFunctions);
        }
    }
    return answer;
}
Also used : KtDeclaration(org.jetbrains.kotlin.psi.KtDeclaration) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) KtClass(org.jetbrains.kotlin.psi.KtClass) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with ClassDescriptor

use of org.jetbrains.kotlin.descriptors.ClassDescriptor in project kotlin by JetBrains.

the class AssignmentTranslator method isReferenceToBackingFieldFromConstructor.

private static boolean isReferenceToBackingFieldFromConstructor(@Nullable DeclarationDescriptor descriptor, @NotNull TranslationContext context) {
    if (!(descriptor instanceof PropertyDescriptor))
        return false;
    PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
    if (!(context.getDeclarationDescriptor() instanceof ClassDescriptor))
        return false;
    ClassDescriptor classDescriptor = (ClassDescriptor) context.getDeclarationDescriptor();
    if (classDescriptor != propertyDescriptor.getContainingDeclaration())
        return false;
    return !propertyDescriptor.isVar();
}
Also used : PropertyDescriptor(org.jetbrains.kotlin.descriptors.PropertyDescriptor) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor)

Aggregations

ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)17 NotNull (org.jetbrains.annotations.NotNull)5 ClassifierDescriptor (org.jetbrains.kotlin.descriptors.ClassifierDescriptor)5 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)3 MemberScope (org.jetbrains.kotlin.resolve.scopes.MemberScope)3 ArrayList (java.util.ArrayList)2 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)2 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)2 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)2 KtClass (org.jetbrains.kotlin.psi.KtClass)2 KtDeclaration (org.jetbrains.kotlin.psi.KtDeclaration)2 KtFile (org.jetbrains.kotlin.psi.KtFile)2 Type (org.jetbrains.org.objectweb.asm.Type)2 Disposable (com.intellij.openapi.Disposable)1 PsiMethod (com.intellij.psi.PsiMethod)1 File (java.io.File)1 ZipEntry (java.util.zip.ZipEntry)1 TestRunner (junit.textui.TestRunner)1 Unit (kotlin.Unit)1 Nullable (org.jetbrains.annotations.Nullable)1