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 com.intellij.util.IncorrectOperationException;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024 import org.jetbrains.kotlin.lexer.JetTokens;
025 import org.jetbrains.kotlin.name.FqName;
026 import org.jetbrains.kotlin.name.Name;
027 import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
028 import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
029
030 import java.util.Collections;
031 import java.util.List;
032
033 public class JetSecondaryConstructor extends JetDeclarationStub<KotlinPlaceHolderStub<JetSecondaryConstructor>> implements JetFunction {
034 public JetSecondaryConstructor(@NotNull ASTNode node) {
035 super(node);
036 }
037
038 public JetSecondaryConstructor(@NotNull KotlinPlaceHolderStub<JetSecondaryConstructor> stub) {
039 super(stub, JetStubElementTypes.SECONDARY_CONSTRUCTOR);
040 }
041
042 @Override
043 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
044 return visitor.visitSecondaryConstructor(this, data);
045 }
046
047 @Override
048 public boolean isLocal() {
049 return false;
050 }
051
052 @Override
053 @Nullable
054 public JetParameterList getValueParameterList() {
055 return getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST);
056 }
057
058 @Override
059 @NotNull
060 public List<JetParameter> getValueParameters() {
061 JetParameterList list = getValueParameterList();
062 return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
063 }
064
065 @Nullable
066 @Override
067 public JetTypeReference getReceiverTypeReference() {
068 return null;
069 }
070
071 @Nullable
072 @Override
073 public JetTypeReference getTypeReference() {
074 return null;
075 }
076
077 @Nullable
078 @Override
079 public JetTypeReference setTypeReference(@Nullable JetTypeReference typeRef) {
080 return null;
081 }
082
083 @Nullable
084 @Override
085 public PsiElement getColon() {
086 return findChildByType(JetTokens.COLON);
087 }
088
089 @Nullable
090 @Override
091 public JetBlockExpression getBodyExpression() {
092 return findChildByClass(JetBlockExpression.class);
093 }
094
095 @Nullable
096 @Override
097 public PsiElement getEqualsToken() {
098 return null;
099 }
100
101 @Override
102 public boolean hasBlockBody() {
103 return true;
104 }
105
106 @Override
107 public boolean hasBody() {
108 return getBodyExpression() != null;
109 }
110
111 @Override
112 public boolean hasDeclaredReturnType() {
113 return false;
114 }
115
116 @Override
117 @NotNull
118 public String getName() {
119 return getClassOrObject().getName();
120 }
121
122 @Nullable
123 @Override
124 public JetTypeParameterList getTypeParameterList() {
125 return null;
126 }
127
128 @Nullable
129 @Override
130 public JetTypeConstraintList getTypeConstraintList() {
131 return null;
132 }
133
134 @NotNull
135 @Override
136 public List<JetTypeConstraint> getTypeConstraints() {
137 return Collections.emptyList();
138 }
139
140 @NotNull
141 @Override
142 public List<JetTypeParameter> getTypeParameters() {
143 return Collections.emptyList();
144 }
145
146 @NotNull
147 @Override
148 public Name getNameAsSafeName() {
149 return Name.identifier(getName());
150 }
151
152 @Nullable
153 @Override
154 public FqName getFqName() {
155 return null;
156 }
157
158 @Nullable
159 @Override
160 public Name getNameAsName() {
161 return getNameAsSafeName();
162 }
163
164 @Nullable
165 @Override
166 public PsiElement getNameIdentifier() {
167 return null;
168 }
169
170 @Override
171 public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
172 throw new IncorrectOperationException("setName to constructor");
173 }
174
175 @NotNull
176 public JetConstructorDelegationCall getDelegationCall() {
177 return findChildByClass(JetConstructorDelegationCall.class);
178 }
179
180 public boolean hasImplicitDelegationCall() {
181 return getDelegationCall().isImplicit();
182 }
183
184 @NotNull
185 public JetClassOrObject getClassOrObject() {
186 return (JetClassOrObject) getParent().getParent();
187 }
188
189 @NotNull
190 public JetConstructorDelegationCall replaceImplicitDelegationCallWithExplicit(boolean isThis) {
191 JetPsiFactory psiFactory = new JetPsiFactory(getProject());
192 JetConstructorDelegationCall current = getDelegationCall();
193
194 assert current.isImplicit()
195 : "Method should not be called with explicit delegation call: " + getText();
196 current.delete();
197
198 PsiElement colon = addAfter(psiFactory.createColon(), getValueParameterList());
199
200 String delegationName = isThis ? "this" : "super";
201
202 return (JetConstructorDelegationCall) addAfter(psiFactory.createConstructorDelegationCall(delegationName + "()"), colon);
203 }
204
205 @NotNull
206 public PsiElement getConstructorKeyword() {
207 return findNotNullChildByType(JetTokens.CONSTRUCTOR_KEYWORD);
208 }
209
210 @Override
211 public int getTextOffset() {
212 return getConstructorKeyword().getTextRange().getStartOffset();
213 }
214 }