001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.psi;
018    
019    import com.intellij.lang.ASTNode;
020    import com.intellij.psi.PsiElement;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.kotlin.lexer.JetModifierKeywordToken;
024    import org.jetbrains.kotlin.lexer.JetTokens;
025    import org.jetbrains.kotlin.psi.addRemoveModifier.AddRemoveModifierPackage;
026    import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
027    import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
028    
029    import java.util.Collections;
030    import java.util.List;
031    
032    public class JetPrimaryConstructor extends JetDeclarationStub<KotlinPlaceHolderStub<JetPrimaryConstructor>> {
033        public JetPrimaryConstructor(@NotNull ASTNode node) {
034            super(node);
035        }
036    
037        public JetPrimaryConstructor(@NotNull KotlinPlaceHolderStub<JetPrimaryConstructor> stub) {
038            super(stub, JetStubElementTypes.PRIMARY_CONSTRUCTOR);
039        }
040    
041        @Override
042        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
043            return visitor.visitPrimaryConstructor(this, data);
044        }
045    
046        @Nullable
047        public JetParameterList getValueParameterList() {
048            return getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST);
049        }
050    
051        @NotNull
052        public List<JetParameter> getValueParameters() {
053            JetParameterList list = getValueParameterList();
054            return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
055        }
056    
057        @Override
058        public void addModifier(@NotNull JetModifierKeywordToken modifier) {
059            JetModifierList modifierList = getModifierList();
060            if (modifierList != null) {
061                AddRemoveModifierPackage.addModifier(modifierList, modifier, JetTokens.PUBLIC_KEYWORD);
062            }
063            else {
064                if (modifier == JetTokens.PUBLIC_KEYWORD) return;
065    
066                JetParameterList parameterList = getValueParameterList();
067                assert parameterList != null;
068                JetPsiFactory psiFactory = new JetPsiFactory(getProject());
069                JetModifierList newModifierList = psiFactory.createModifierList(modifier);
070                addBefore(newModifierList, parameterList);
071            }
072        }
073    
074        @NotNull
075        public JetClassOrObject getContainingClassOrObject() {
076            return (JetClassOrObject) getParent();
077        }
078    
079        public boolean hasConstructorKeyword() {
080            if (getStub() != null) return true;
081            return getConstructorKeyword() != null;
082        }
083    
084        @Nullable
085        public PsiElement getConstructorKeyword() {
086            return findChildByType(JetTokens.CONSTRUCTOR_KEYWORD);
087        }
088    }