Search in sources :

Example 1 with KtDeclaration

use of org.jetbrains.kotlin.psi.KtDeclaration in project kotlin by JetBrains.

the class AbstractPsiCheckerTest method checkResolveToDescriptor.

void checkResolveToDescriptor() {
    KtFile file = (KtFile) myFixture.getFile();
    file.accept(new KtTreeVisitorVoid() {

        @Override
        public void visitDeclaration(@NotNull KtDeclaration dcl) {
            if (areDescriptorsCreatedForDeclaration(dcl)) {
                // check for exceptions
                ResolutionUtils.resolveToDescriptor(dcl, BodyResolveMode.FULL);
            }
            dcl.acceptChildren(this, null);
        }
    });
}
Also used : KtDeclaration(org.jetbrains.kotlin.psi.KtDeclaration) KtTreeVisitorVoid(org.jetbrains.kotlin.psi.KtTreeVisitorVoid) KtFile(org.jetbrains.kotlin.psi.KtFile)

Example 2 with KtDeclaration

use of org.jetbrains.kotlin.psi.KtDeclaration 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 3 with KtDeclaration

use of org.jetbrains.kotlin.psi.KtDeclaration 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 4 with KtDeclaration

use of org.jetbrains.kotlin.psi.KtDeclaration in project kotlin by JetBrains.

the class KtFileTreeNode method getChildrenImpl.

@Override
public Collection<AbstractTreeNode> getChildrenImpl() {
    KtFile file = (KtFile) getValue();
    if (file == null)
        return Collections.emptyList();
    ArrayList<AbstractTreeNode> result = new ArrayList<AbstractTreeNode>();
    if (getSettings().isShowMembers()) {
        List<KtDeclaration> declarations = file.getDeclarations();
        for (KtDeclaration declaration : declarations) {
            if (declaration instanceof KtClassOrObject) {
                result.add(new KtClassOrObjectTreeNode(file.getProject(), (KtClassOrObject) declaration, getSettings()));
            } else if (getSettings().isShowMembers()) {
                result.add(new KtDeclarationTreeNode(getProject(), declaration, getSettings()));
            }
        }
    }
    return result;
}
Also used : KtClassOrObject(org.jetbrains.kotlin.psi.KtClassOrObject) KtDeclaration(org.jetbrains.kotlin.psi.KtDeclaration) ArrayList(java.util.ArrayList) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) KtFile(org.jetbrains.kotlin.psi.KtFile)

Example 5 with KtDeclaration

use of org.jetbrains.kotlin.psi.KtDeclaration in project kotlin by JetBrains.

the class KotlinStubsTest method testSuperclassNames.

public void testSuperclassNames() {
    PsiFile psiFile = myFixture.configureByText("foo.kt", "import java.util.ArrayList as alist\nclass C(): alist() { }");
    List<KtDeclaration> declarations = ((KtFile) psiFile).getDeclarations();
    KtClass ktClass = (KtClass) declarations.get(0);
    KotlinClassStub stub = KtStubElementTypes.CLASS.createStub(ktClass, null);
    List<String> names = stub.getSuperNames();
    assertSameElements(names, "ArrayList", "alist");
}
Also used : KtDeclaration(org.jetbrains.kotlin.psi.KtDeclaration) KtClass(org.jetbrains.kotlin.psi.KtClass) KotlinClassStub(org.jetbrains.kotlin.psi.stubs.KotlinClassStub) PsiFile(com.intellij.psi.PsiFile) KtFile(org.jetbrains.kotlin.psi.KtFile)

Aggregations

KtDeclaration (org.jetbrains.kotlin.psi.KtDeclaration)7 KtFile (org.jetbrains.kotlin.psi.KtFile)6 KtClass (org.jetbrains.kotlin.psi.KtClass)4 PsiFile (com.intellij.psi.PsiFile)3 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)2 KotlinClassStub (org.jetbrains.kotlin.psi.stubs.KotlinClassStub)2 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 ArrayList (java.util.ArrayList)1 TestRunner (junit.textui.TestRunner)1 NotNull (org.jetbrains.annotations.NotNull)1 GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)1 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)1 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)1 KtClassOrObject (org.jetbrains.kotlin.psi.KtClassOrObject)1 KtElement (org.jetbrains.kotlin.psi.KtElement)1 KtTreeVisitorVoid (org.jetbrains.kotlin.psi.KtTreeVisitorVoid)1 Diagnostics (org.jetbrains.kotlin.resolve.diagnostics.Diagnostics)1 MemberScope (org.jetbrains.kotlin.resolve.scopes.MemberScope)1