Search in sources :

Example 1 with KtClass

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

the class ReferenceUtils method renderAsGotoImplementation.

public static String renderAsGotoImplementation(@NotNull PsiElement element) {
    PsiElement navigationElement = element.getNavigationElement();
    if (navigationElement instanceof KtObjectDeclaration && ((KtObjectDeclaration) navigationElement).isCompanion()) {
        //default presenter return null for companion object
        KtClass containingClass = PsiTreeUtil.getParentOfType(navigationElement, KtClass.class);
        assert containingClass != null;
        return "companion object of " + renderAsGotoImplementation(containingClass);
    }
    if (navigationElement instanceof KtStringTemplateExpression) {
        return KtPsiUtilKt.getPlainContent((KtStringTemplateExpression) navigationElement);
    }
    Assert.assertTrue(navigationElement instanceof NavigationItem);
    ItemPresentation presentation = ((NavigationItem) navigationElement).getPresentation();
    if (presentation == null) {
        String elementText = element.getText();
        return elementText != null ? elementText : navigationElement.getText();
    }
    String presentableText = presentation.getPresentableText();
    String locationString = presentation.getLocationString();
    if (locationString == null && element.getParent() instanceof PsiAnonymousClass) {
        locationString = "<anonymous>";
    }
    return locationString == null || navigationElement instanceof PsiPackage ? // for PsiPackage, presentableText is FQ name of current package
    presentableText : locationString + "." + presentableText;
}
Also used : NavigationItem(com.intellij.navigation.NavigationItem) KtClass(org.jetbrains.kotlin.psi.KtClass) KtStringTemplateExpression(org.jetbrains.kotlin.psi.KtStringTemplateExpression) ItemPresentation(com.intellij.navigation.ItemPresentation) KtObjectDeclaration(org.jetbrains.kotlin.psi.KtObjectDeclaration)

Example 2 with KtClass

use of org.jetbrains.kotlin.psi.KtClass 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 KtClass

use of org.jetbrains.kotlin.psi.KtClass 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)

Example 4 with KtClass

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

the class KotlinStubsTest method testClassIsTrait.

public void testClassIsTrait() {
    PsiFile psiFile = myFixture.configureByText("foo.kt", "interface Test { }");
    List<KtDeclaration> declarations = ((KtFile) psiFile).getDeclarations();
    KtClass ktClass = (KtClass) declarations.get(0);
    KotlinClassStub stub = KtStubElementTypes.CLASS.createStub(ktClass, null);
    assertEquals(true, stub.isInterface());
}
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)

Example 5 with KtClass

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

the class KotlinFindClassUsagesDialog method getRepresentingPsiClass.

@NotNull
private static PsiClass getRepresentingPsiClass(@NotNull KtClassOrObject classOrObject) {
    PsiClass lightClass = toLightClass(classOrObject);
    if (lightClass != null)
        return lightClass;
    // TODO: Remove this code when light classes are generated for builtins
    PsiElementFactory factory = PsiElementFactory.SERVICE.getInstance(classOrObject.getProject());
    String name = classOrObject.getName();
    if (name == null || name.isEmpty()) {
        name = "Anonymous";
    }
    PsiClass javaClass;
    if (classOrObject instanceof KtClass) {
        KtClass klass = (KtClass) classOrObject;
        javaClass = !klass.isInterface() ? factory.createClass(name) : klass.isAnnotation() ? factory.createAnnotationType(name) : factory.createInterface(name);
    } else {
        javaClass = factory.createClass(name);
    }
    //noinspection ConstantConditions
    javaClass.getModifierList().setModifierProperty(PsiModifier.FINAL, !(classOrObject instanceof KtClass && KtPsiUtilKt.isInheritable((KtClass) classOrObject)));
    javaClass.putUserData(ORIGINAL_CLASS, classOrObject);
    return javaClass;
}
Also used : KtClass(org.jetbrains.kotlin.psi.KtClass) PsiClass(com.intellij.psi.PsiClass) PsiElementFactory(com.intellij.psi.PsiElementFactory) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

KtClass (org.jetbrains.kotlin.psi.KtClass)6 KtDeclaration (org.jetbrains.kotlin.psi.KtDeclaration)4 KtFile (org.jetbrains.kotlin.psi.KtFile)3 PsiFile (com.intellij.psi.PsiFile)2 NotNull (org.jetbrains.annotations.NotNull)2 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)2 KotlinClassStub (org.jetbrains.kotlin.psi.stubs.KotlinClassStub)2 ItemPresentation (com.intellij.navigation.ItemPresentation)1 NavigationItem (com.intellij.navigation.NavigationItem)1 PsiClass (com.intellij.psi.PsiClass)1 PsiElementFactory (com.intellij.psi.PsiElementFactory)1 TestRunner (junit.textui.TestRunner)1 GenerationState (org.jetbrains.kotlin.codegen.state.GenerationState)1 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)1 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)1 KtObjectDeclaration (org.jetbrains.kotlin.psi.KtObjectDeclaration)1 KtStringTemplateExpression (org.jetbrains.kotlin.psi.KtStringTemplateExpression)1 MemberScope (org.jetbrains.kotlin.resolve.scopes.MemberScope)1