Search in sources :

Example 1 with ClassId

use of org.jetbrains.kotlin.name.ClassId in project kotlin by JetBrains.

the class InlineCodegen method doCreateMethodNodeFromCompiled.

@Nullable
private static SMAPAndMethodNode doCreateMethodNodeFromCompiled(@NotNull CallableMemberDescriptor callableDescriptor, @NotNull final GenerationState state, @NotNull Method asmMethod) {
    if (isBuiltInArrayIntrinsic(callableDescriptor)) {
        ClassId classId = IntrinsicArrayConstructorsKt.getClassId();
        byte[] bytes = InlineCacheKt.getOrPut(state.getInlineCache().getClassBytes(), classId, new Function0<byte[]>() {

            @Override
            public byte[] invoke() {
                return IntrinsicArrayConstructorsKt.getBytecode();
            }
        });
        return InlineCodegenUtil.getMethodNode(bytes, asmMethod.getName(), asmMethod.getDescriptor(), classId);
    }
    assert callableDescriptor instanceof DeserializedCallableMemberDescriptor : "Not a deserialized function or proper: " + callableDescriptor;
    KotlinTypeMapper.ContainingClassesInfo containingClasses = state.getTypeMapper().getContainingClassesForDeserializedCallable((DeserializedCallableMemberDescriptor) callableDescriptor);
    final ClassId containerId = containingClasses.getImplClassId();
    byte[] bytes = InlineCacheKt.getOrPut(state.getInlineCache().getClassBytes(), containerId, new Function0<byte[]>() {

        @Override
        public byte[] invoke() {
            VirtualFile file = InlineCodegenUtil.findVirtualFile(state, containerId);
            if (file == null) {
                throw new IllegalStateException("Couldn't find declaration file for " + containerId);
            }
            try {
                return file.contentsToByteArray();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
    return InlineCodegenUtil.getMethodNode(bytes, asmMethod.getName(), asmMethod.getDescriptor(), containerId);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DeserializedCallableMemberDescriptor(org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor) ClassId(org.jetbrains.kotlin.name.ClassId) IOException(java.io.IOException) KotlinTypeMapper(org.jetbrains.kotlin.codegen.state.KotlinTypeMapper) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ClassId

use of org.jetbrains.kotlin.name.ClassId in project kotlin by JetBrains.

the class JavaAnnotationImpl method computeClassId.

@Nullable
private static ClassId computeClassId(@NotNull PsiClass psiClass) {
    PsiClass container = psiClass.getContainingClass();
    if (container != null) {
        ClassId parentClassId = computeClassId(container);
        String name = psiClass.getName();
        return parentClassId == null || name == null ? null : parentClassId.createNestedClassId(Name.identifier(name));
    }
    String fqName = psiClass.getQualifiedName();
    return fqName == null ? null : ClassId.topLevel(new FqName(fqName));
}
Also used : FqName(org.jetbrains.kotlin.name.FqName) PsiClass(com.intellij.psi.PsiClass) ClassId(org.jetbrains.kotlin.name.ClassId) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ClassId

use of org.jetbrains.kotlin.name.ClassId in project kotlin by JetBrains.

the class FileBasedKotlinClass method resolveNameByInternalName.

@NotNull
private static ClassId resolveNameByInternalName(@NotNull String name, @NotNull InnerClassesInfo innerClasses) {
    if (!name.contains("$")) {
        return ClassId.topLevel(new FqName(name.replace('/', '.')));
    }
    List<String> classes = new ArrayList<String>(1);
    boolean local = false;
    while (true) {
        OuterAndInnerName outer = innerClasses.get(name);
        if (outer == null)
            break;
        if (outer.outerInternalName == null) {
            local = true;
            break;
        }
        classes.add(outer.innerSimpleName);
        name = outer.outerInternalName;
    }
    FqName outermostClassFqName = new FqName(name.replace('/', '.'));
    classes.add(outermostClassFqName.shortName().asString());
    Collections.reverse(classes);
    FqName packageFqName = outermostClassFqName.parent();
    FqName relativeClassName = FqName.fromSegments(classes);
    return new ClassId(packageFqName, relativeClassName, local);
}
Also used : FqName(org.jetbrains.kotlin.name.FqName) ClassId(org.jetbrains.kotlin.name.ClassId) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ClassId

use of org.jetbrains.kotlin.name.ClassId in project kotlin by JetBrains.

the class FileBasedKotlinClass method create.

// TODO public to be accessible in companion object of subclass, workaround for KT-3974
@Nullable
public static <T extends FileBasedKotlinClass> T create(@NotNull byte[] fileContents, @NotNull Function4<ClassId, Integer, KotlinClassHeader, InnerClassesInfo, T> factory) {
    final ReadKotlinClassHeaderAnnotationVisitor readHeaderVisitor = new ReadKotlinClassHeaderAnnotationVisitor();
    final Ref<String> classNameRef = Ref.create();
    final Ref<Integer> classVersion = Ref.create();
    final InnerClassesInfo innerClasses = new InnerClassesInfo();
    new ClassReader(fileContents).accept(new ClassVisitor(ASM5) {

        @Override
        public void visit(int version, int access, @NotNull String name, String signature, String superName, String[] interfaces) {
            classNameRef.set(name);
            classVersion.set(version);
        }

        @Override
        public void visitInnerClass(@NotNull String name, String outerName, String innerName, int access) {
            innerClasses.add(name, outerName, innerName);
        }

        @Override
        public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) {
            return convertAnnotationVisitor(readHeaderVisitor, desc, innerClasses);
        }

        @Override
        public void visitEnd() {
            readHeaderVisitor.visitEnd();
        }
    }, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
    String className = classNameRef.get();
    if (className == null)
        return null;
    KotlinClassHeader header = readHeaderVisitor.createHeader();
    if (header == null)
        return null;
    ClassId id = resolveNameByInternalName(className, innerClasses);
    return factory.invoke(id, classVersion.get(), header, innerClasses);
}
Also used : KotlinClassHeader(org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader) ClassId(org.jetbrains.kotlin.name.ClassId) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) ReadKotlinClassHeaderAnnotationVisitor(org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor) ReadKotlinClassHeaderAnnotationVisitor(org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ClassId

use of org.jetbrains.kotlin.name.ClassId in project kotlin by JetBrains.

the class InlineCodegenUtil method findVirtualFileImprecise.

@Nullable
private static VirtualFile findVirtualFileImprecise(@NotNull GenerationState state, @NotNull String internalClassName) {
    FqName packageFqName = JvmClassName.byInternalName(internalClassName).getPackageFqName();
    String classNameWithDollars = StringsKt.substringAfterLast(internalClassName, "/", internalClassName);
    // we construct valid.package.name/RelativeClassNameAsSingleName that should work in compiler, but fails for inner classes in IDE
    return findVirtualFile(state, new ClassId(packageFqName, Name.identifier(classNameWithDollars)));
}
Also used : FqName(org.jetbrains.kotlin.name.FqName) ClassId(org.jetbrains.kotlin.name.ClassId) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ClassId (org.jetbrains.kotlin.name.ClassId)5 Nullable (org.jetbrains.annotations.Nullable)4 FqName (org.jetbrains.kotlin.name.FqName)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiClass (com.intellij.psi.PsiClass)1 IOException (java.io.IOException)1 NotNull (org.jetbrains.annotations.NotNull)1 KotlinTypeMapper (org.jetbrains.kotlin.codegen.state.KotlinTypeMapper)1 KotlinClassHeader (org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader)1 ReadKotlinClassHeaderAnnotationVisitor (org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor)1 DeserializedCallableMemberDescriptor (org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor)1 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)1 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)1