From 1ab90c78148558eb00c916bf5e6040b4eaa8cd0f Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 9 Jun 2015 16:34:38 +0300 Subject: [PATCH] J2K: Convert constructor PSI classes to Kotlin --- .../jetbrains/kotlin/psi/JetConstructor.kt | 199 ++++-------------- .../kotlin/psi/JetPrimaryConstructor.kt | 60 ++---- .../kotlin/psi/JetSecondaryConstructor.kt | 74 ++----- 3 files changed, 87 insertions(+), 246 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetConstructor.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetConstructor.kt index 49640f433e4..43c26470378 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetConstructor.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetConstructor.kt @@ -14,188 +14,79 @@ * limitations under the License. */ -package org.jetbrains.kotlin.psi; +package org.jetbrains.kotlin.psi -import com.intellij.lang.ASTNode; -import com.intellij.navigation.ItemPresentation; -import com.intellij.navigation.ItemPresentationProviders; -import com.intellij.psi.PsiElement; -import com.intellij.psi.stubs.IStubElementType; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.lexer.JetTokens; -import org.jetbrains.kotlin.name.FqName; -import org.jetbrains.kotlin.name.Name; -import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub; -import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes; +import com.intellij.lang.ASTNode +import com.intellij.navigation.ItemPresentationProviders +import com.intellij.psi.PsiElement +import com.intellij.util.IncorrectOperationException +import org.jetbrains.kotlin.lexer.JetTokens +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub +import org.jetbrains.kotlin.psi.stubs.elements.JetPlaceHolderStubElementType +import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes -import java.util.Collections; -import java.util.List; +public abstract class JetConstructor> : JetDeclarationStub>, JetFunction { + protected constructor(node: ASTNode) : super(node) + protected constructor(stub: KotlinPlaceHolderStub, nodeType: JetPlaceHolderStubElementType) : super(stub, nodeType) -public abstract class JetConstructor> extends JetDeclarationStub> implements JetFunction { - protected JetConstructor(@NotNull ASTNode node) { - super(node); - } + public abstract fun getClassOrObject(): JetClassOrObject - protected JetConstructor(@NotNull KotlinPlaceHolderStub stub, @NotNull IStubElementType nodeType) { - super(stub, nodeType); - } + override fun isLocal() = false - @NotNull - public abstract JetClassOrObject getClassOrObject(); + override fun getValueParameterList() = getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST) - @Override - public boolean isLocal() { - return false; - } + override fun getValueParameters() = getValueParameterList()?.getParameters() ?: emptyList() - @Override - @Nullable - public JetParameterList getValueParameterList() { - return getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST); - } + override fun getReceiverTypeReference() = null - @Override - @NotNull - public List getValueParameters() { - JetParameterList list = getValueParameterList(); - return list != null ? list.getParameters() : Collections.emptyList(); - } + override fun getTypeReference() = null - @Nullable - @Override - public JetTypeReference getReceiverTypeReference() { - return null; - } + throws(IncorrectOperationException::class) + override fun setTypeReference(typeRef: JetTypeReference?) = throw IncorrectOperationException("setTypeReference to constructor") - @Nullable - @Override - public JetTypeReference getTypeReference() { - return null; - } + override fun getColon() = findChildByType(JetTokens.COLON) - @Nullable - @Override - public JetTypeReference setTypeReference(@Nullable JetTypeReference typeRef) { - return null; - } + override fun getBodyExpression(): JetBlockExpression? = null - @Nullable - @Override - public PsiElement getColon() { - return findChildByType(JetTokens.COLON); - } + override fun getEqualsToken() = null - @Nullable - @Override - public JetBlockExpression getBodyExpression() { - return null; - } + override fun hasBlockBody() = true - @Nullable - @Override - public PsiElement getEqualsToken() { - return null; - } + override fun hasBody() = getBodyExpression() != null - @Override - public boolean hasBlockBody() { - return true; - } + override fun hasDeclaredReturnType() = false - @Override - public boolean hasBody() { - return getBodyExpression() != null; - } + override fun getTypeParameterList() = null - @Override - public boolean hasDeclaredReturnType() { - return false; - } + override fun getTypeConstraintList() = null - @Nullable - @Override - public JetTypeParameterList getTypeParameterList() { - return null; - } + override fun getTypeConstraints() = emptyList() - @Nullable - @Override - public JetTypeConstraintList getTypeConstraintList() { - return null; - } + override fun getTypeParameters() = emptyList() - @NotNull - @Override - public List getTypeConstraints() { - return Collections.emptyList(); - } + override fun getName(): String = getClassOrObject().getName()!! - @NotNull - @Override - public List getTypeParameters() { - return Collections.emptyList(); - } + override fun getNameAsSafeName() = Name.identifier(getName()) - @Override - @NotNull - public String getName() { - return getClassOrObject().getName(); - } + override fun getFqName() = null - @NotNull - @Override - public Name getNameAsSafeName() { - return Name.identifier(getName()); - } + override fun getNameAsName() = getNameAsSafeName() - @Nullable - @Override - public FqName getFqName() { - return null; - } + override fun getNameIdentifier() = null - @Nullable - @Override - public Name getNameAsName() { - return getNameAsSafeName(); - } + throws(IncorrectOperationException::class) + override fun setName(name: String): PsiElement = throw IncorrectOperationException("setName to constructor") - @Nullable - @Override - public PsiElement getNameIdentifier() { - return null; - } + override fun getPresentation() = ItemPresentationProviders.getItemPresentation(this) - @Override - public PsiElement setName(@NotNull String name) throws IncorrectOperationException { - throw new IncorrectOperationException("setName to constructor"); - } + public open fun getConstructorKeyword(): PsiElement? = findChildByType(JetTokens.CONSTRUCTOR_KEYWORD) - @Override - public ItemPresentation getPresentation() { - return ItemPresentationProviders.getItemPresentation(this); - } + public fun hasConstructorKeyword(): Boolean = getStub() != null || getConstructorKeyword() != null - @Nullable - public PsiElement getConstructorKeyword() { - return findChildByType(JetTokens.CONSTRUCTOR_KEYWORD); - } - - public boolean hasConstructorKeyword() { - if (getStub() != null) return true; - return getConstructorKeyword() != null; - } - - @Override - public int getTextOffset() { - PsiElement keyword = getConstructorKeyword(); - if (keyword != null) return keyword.getTextOffset(); - - JetParameterList parameterList = getValueParameterList(); - if (parameterList != null) return parameterList.getTextOffset(); - - return super.getTextOffset(); + override fun getTextOffset(): Int { + return getConstructorKeyword()?.getTextOffset() + ?: getValueParameterList()?.getTextOffset() + ?: super.getTextOffset() } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPrimaryConstructor.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPrimaryConstructor.kt index df983abf0b9..1612da84e73 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPrimaryConstructor.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetPrimaryConstructor.kt @@ -14,55 +14,35 @@ * limitations under the License. */ -package org.jetbrains.kotlin.psi; +package org.jetbrains.kotlin.psi -import com.intellij.lang.ASTNode; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.lexer.JetModifierKeywordToken; -import org.jetbrains.kotlin.lexer.JetTokens; -import org.jetbrains.kotlin.psi.addRemoveModifier.AddRemoveModifierPackage; -import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub; -import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes; +import com.intellij.lang.ASTNode +import org.jetbrains.kotlin.lexer.JetModifierKeywordToken +import org.jetbrains.kotlin.lexer.JetTokens +import org.jetbrains.kotlin.psi.addRemoveModifier.* +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub +import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes -public class JetPrimaryConstructor extends JetConstructor { - public JetPrimaryConstructor(@NotNull ASTNode node) { - super(node); - } +public class JetPrimaryConstructor : JetConstructor { + public constructor(node: ASTNode) : super(node) + public constructor(stub: KotlinPlaceHolderStub) : super(stub, JetStubElementTypes.PRIMARY_CONSTRUCTOR) - public JetPrimaryConstructor(@NotNull KotlinPlaceHolderStub stub) { - super(stub, JetStubElementTypes.PRIMARY_CONSTRUCTOR); - } + override fun accept(visitor: JetVisitor, data: D) = visitor.visitPrimaryConstructor(this, data) - @Override - public R accept(@NotNull JetVisitor visitor, D data) { - return visitor.visitPrimaryConstructor(this, data); - } + public fun getContainingClassOrObject(): JetClassOrObject = getParent() as JetClassOrObject - @NotNull - public JetClassOrObject getContainingClassOrObject() { - return (JetClassOrObject) getParent(); - } + override fun getClassOrObject() = getContainingClassOrObject() - @NotNull - @Override - public JetClassOrObject getClassOrObject() { - return getContainingClassOrObject(); - } - - @Override - public void addModifier(@NotNull JetModifierKeywordToken modifier) { - JetModifierList modifierList = getModifierList(); + override fun addModifier(modifier: JetModifierKeywordToken) { + val modifierList = getModifierList() if (modifierList != null) { - AddRemoveModifierPackage.addModifier(modifierList, modifier, JetTokens.PUBLIC_KEYWORD); + addModifier(modifierList, modifier, JetTokens.PUBLIC_KEYWORD) } else { - if (modifier == JetTokens.PUBLIC_KEYWORD) return; - - JetParameterList parameterList = getValueParameterList(); - assert parameterList != null; - JetPsiFactory psiFactory = new JetPsiFactory(getProject()); - JetModifierList newModifierList = psiFactory.createModifierList(modifier); - addBefore(newModifierList, parameterList); + if (modifier == JetTokens.PUBLIC_KEYWORD) return + val parameterList = getValueParameterList()!! + val newModifierList = JetPsiFactory(getProject()).createModifierList(modifier) + addBefore(newModifierList, parameterList) } } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetSecondaryConstructor.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetSecondaryConstructor.kt index de7c93a1e96..660054a67f7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetSecondaryConstructor.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetSecondaryConstructor.kt @@ -14,70 +14,40 @@ * limitations under the License. */ -package org.jetbrains.kotlin.psi; +package org.jetbrains.kotlin.psi -import com.intellij.lang.ASTNode; -import com.intellij.psi.PsiElement; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub; -import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes; +import com.intellij.lang.ASTNode +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub +import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes -public class JetSecondaryConstructor extends JetConstructor { - public JetSecondaryConstructor(@NotNull ASTNode node) { - super(node); - } +public class JetSecondaryConstructor : JetConstructor { + public constructor(node: ASTNode) : super(node) + public constructor(stub: KotlinPlaceHolderStub) : super(stub, JetStubElementTypes.SECONDARY_CONSTRUCTOR) - public JetSecondaryConstructor(@NotNull KotlinPlaceHolderStub stub) { - super(stub, JetStubElementTypes.SECONDARY_CONSTRUCTOR); - } + override fun accept(visitor: JetVisitor, data: D) = visitor.visitSecondaryConstructor(this, data) - @Override - public R accept(@NotNull JetVisitor visitor, D data) { - return visitor.visitSecondaryConstructor(this, data); - } + override fun getClassOrObject() = getParent().getParent() as JetClassOrObject - @Override - @NotNull - public JetClassOrObject getClassOrObject() { - return (JetClassOrObject) getParent().getParent(); - } + override fun getBodyExpression() = findChildByClass(javaClass()) - @Nullable - @Override - public JetBlockExpression getBodyExpression() { - return findChildByClass(JetBlockExpression.class); - } + override fun getConstructorKeyword() = notNullChild(super.getConstructorKeyword()) - @Override - @NotNull - public PsiElement getConstructorKeyword() { - //noinspection ConstantConditions - return notNullChild(super.getConstructorKeyword()); - } + public fun getDelegationCall(): JetConstructorDelegationCall = findNotNullChildByClass(javaClass()) - @NotNull - public JetConstructorDelegationCall getDelegationCall() { - return findNotNullChildByClass(JetConstructorDelegationCall.class); - } + public fun hasImplicitDelegationCall(): Boolean = getDelegationCall().isImplicit() - public boolean hasImplicitDelegationCall() { - return getDelegationCall().isImplicit(); - } + public fun replaceImplicitDelegationCallWithExplicit(isThis: Boolean): JetConstructorDelegationCall { + val psiFactory = JetPsiFactory(getProject()) + val current = getDelegationCall() - @NotNull - public JetConstructorDelegationCall replaceImplicitDelegationCallWithExplicit(boolean isThis) { - JetPsiFactory psiFactory = new JetPsiFactory(getProject()); - JetConstructorDelegationCall current = getDelegationCall(); + assert(current.isImplicit()) { "Method should not be called with explicit delegation call: " + getText() } + current.delete() - assert current.isImplicit() - : "Method should not be called with explicit delegation call: " + getText(); - current.delete(); + val colon = addAfter(psiFactory.createColon(), getValueParameterList()) - PsiElement colon = addAfter(psiFactory.createColon(), getValueParameterList()); + val delegationName = if (isThis) "this" else "super" - String delegationName = isThis ? "this" : "super"; - - return (JetConstructorDelegationCall) addAfter(psiFactory.createConstructorDelegationCall(delegationName + "()"), colon); + return addAfter(psiFactory.createConstructorDelegationCall(delegationName + "()"), colon) as JetConstructorDelegationCall } }