j2k: actual convert KtLightClassForAnonymousDeclaration

This commit is contained in:
Nikolay Krasko
2016-06-08 18:04:01 +03:00
parent 81b94d0988
commit 4679ae60ab
@@ -14,99 +14,84 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.asJava; package org.jetbrains.kotlin.asJava
import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project
import com.intellij.psi.*; import com.intellij.psi.*
import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.search.GlobalSearchScope
import com.intellij.reference.SoftReference; import com.intellij.reference.SoftReference
import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.annotations.Nullable; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor; import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.psi.KtClassOrObject; import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.types.KotlinType;
import java.util.Collection; internal open class KtLightClassForAnonymousDeclaration(name: FqName, classOrObject: KtClassOrObject) : KtLightClassForExplicitDeclaration(name, classOrObject), PsiAnonymousClass {
class KtLightClassForAnonymousDeclaration extends KtLightClassForExplicitDeclaration implements PsiAnonymousClass { private var cachedBaseType: SoftReference<PsiClassType>? = null
private static final Logger LOG = Logger.getInstance(KtLightClassForAnonymousDeclaration.class);
private SoftReference<PsiClassType> cachedBaseType = null; override fun getBaseClassReference(): PsiJavaCodeReferenceElement {
return JavaPsiFacade.getElementFactory(classOrObject.project).createReferenceElementByType(baseClassType)
KtLightClassForAnonymousDeclaration(@NotNull FqName name, @NotNull KtClassOrObject classOrObject) {
super(name, classOrObject);
} }
@NotNull override fun getContainingClass(): PsiClass? {
@Override return delegate.containingClass
public PsiJavaCodeReferenceElement getBaseClassReference() {
return JavaPsiFacade.getElementFactory(getClassOrObject().getProject()).createReferenceElementByType(getBaseClassType());
} }
@Nullable private // return java.lang.Object for recovery
@Override val firstSupertypeFQName: String
public PsiClass getContainingClass() { get() {
return getDelegate().getContainingClass(); val descriptor = getDescriptor() ?: return CommonClassNames.JAVA_LANG_OBJECT
}
@NotNull val superTypes = descriptor.typeConstructor.supertypes
private String getFirstSupertypeFQName() {
ClassDescriptor descriptor = getDescriptor();
if (descriptor == null) return CommonClassNames.JAVA_LANG_OBJECT;
Collection<KotlinType> superTypes = descriptor.getTypeConstructor().getSupertypes(); if (superTypes.isEmpty()) return CommonClassNames.JAVA_LANG_OBJECT
if (superTypes.isEmpty()) return CommonClassNames.JAVA_LANG_OBJECT; val superType = superTypes.iterator().next()
val superClassDescriptor = superType.constructor.declarationDescriptor
KotlinType superType = superTypes.iterator().next(); if (superClassDescriptor == null) {
DeclarationDescriptor superClassDescriptor = superType.getConstructor().getDeclarationDescriptor(); LOG.error("No declaration descriptor for supertype " + superType + " of " + getDescriptor())
return CommonClassNames.JAVA_LANG_OBJECT
}
if (superClassDescriptor == null) { return DescriptorUtils.getFqName(superClassDescriptor).asString()
LOG.error("No declaration descriptor for supertype " + superType + " of " + getDescriptor());
// return java.lang.Object for recovery
return CommonClassNames.JAVA_LANG_OBJECT;
} }
return DescriptorUtils.getFqName(superClassDescriptor).asString(); @Synchronized override fun getBaseClassType(): PsiClassType {
} var type: PsiClassType? = null
if (cachedBaseType != null) type = cachedBaseType!!.get()
if (type != null && type.isValid) return type
@NotNull val firstSupertypeFQName = firstSupertypeFQName
@Override for (superType in superTypes) {
public synchronized PsiClassType getBaseClassType() { val superClass = superType.resolve()
PsiClassType type = null; if (superClass != null && firstSupertypeFQName == superClass.qualifiedName) {
if (cachedBaseType != null) type = cachedBaseType.get(); type = superType
if (type != null && type.isValid()) return type; break
String firstSupertypeFQName = getFirstSupertypeFQName();
for (PsiClassType superType : getSuperTypes()) {
PsiClass superClass = superType.resolve();
if (superClass != null && firstSupertypeFQName.equals(superClass.getQualifiedName())) {
type = superType;
break;
} }
} }
if (type == null) { if (type == null) {
Project project = getClassOrObject().getProject(); val project = classOrObject.project
type = PsiType.getJavaLangObject(PsiManager.getInstance(project), GlobalSearchScope.allScope(project)); type = PsiType.getJavaLangObject(PsiManager.getInstance(project), GlobalSearchScope.allScope(project))
} }
cachedBaseType = new SoftReference<PsiClassType>(type); cachedBaseType = SoftReference<PsiClassType>(type)
return type; return type
} }
@Nullable override fun getArgumentList(): PsiExpressionList? {
@Override return null
public PsiExpressionList getArgumentList() {
return null;
} }
@Override override fun isInQualifiedNew(): Boolean {
public boolean isInQualifiedNew() { return false
return false; }
companion object {
private val LOG = Logger.getInstance(KtLightClassForAnonymousDeclaration::class.java)
} }
} }