001 /*
002 * Copyright 2010-2013 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.jet.lang.psi;
018
019 import com.intellij.lang.ASTNode;
020 import com.intellij.psi.PsiElement;
021 import com.intellij.psi.util.PsiTreeUtil;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024 import org.jetbrains.jet.lang.psi.stubs.PsiJetPropertyAccessorStub;
025 import org.jetbrains.jet.lang.psi.stubs.elements.JetStubElementTypes;
026 import org.jetbrains.jet.lexer.JetTokens;
027
028 import java.util.Collections;
029 import java.util.List;
030
031 public class JetPropertyAccessor extends JetDeclarationStub<PsiJetPropertyAccessorStub>
032 implements JetDeclarationWithBody, JetModifierListOwner, JetWithExpressionInitializer {
033 public JetPropertyAccessor(@NotNull ASTNode node) {
034 super(node);
035 }
036
037 public JetPropertyAccessor(@NotNull PsiJetPropertyAccessorStub stub) {
038 super(stub, JetStubElementTypes.PROPERTY_ACCESSOR);
039 }
040
041 @Override
042 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
043 return visitor.visitPropertyAccessor(this, data);
044 }
045
046 public boolean isSetter() {
047 PsiJetPropertyAccessorStub stub = getStub();
048 if (stub != null) {
049 return !stub.isGetter();
050 }
051 return findChildByType(JetTokens.SET_KEYWORD) != null;
052 }
053
054 public boolean isGetter() {
055 PsiJetPropertyAccessorStub stub = getStub();
056 if (stub != null) {
057 return stub.isGetter();
058 }
059 return findChildByType(JetTokens.GET_KEYWORD) != null;
060 }
061
062 @Nullable
063 public JetParameter getParameter() {
064 JetParameterList parameterList = getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST);
065 if (parameterList == null) return null;
066 List<JetParameter> parameters = parameterList.getParameters();
067 if (parameters.isEmpty()) return null;
068 return parameters.get(0);
069 }
070
071 @NotNull
072 @Override
073 public List<JetParameter> getValueParameters() {
074 JetParameter parameter = getParameter();
075 if (parameter == null) {
076 return Collections.emptyList();
077 }
078 return Collections.singletonList(parameter);
079 }
080
081 @Nullable
082 @Override
083 public JetExpression getBodyExpression() {
084 return findChildByClass(JetExpression.class);
085 }
086
087 @Override
088 public boolean hasBlockBody() {
089 return getEqualsToken() == null;
090 }
091
092 @Override
093 public boolean hasBody() {
094 PsiJetPropertyAccessorStub stub = getStub();
095 if (stub != null) {
096 return stub.hasBody();
097 }
098 return getBodyExpression() != null;
099 }
100
101 @Nullable
102 public PsiElement getEqualsToken() {
103 return findChildByType(JetTokens.EQ);
104 }
105
106 @Override
107 public boolean hasDeclaredReturnType() {
108 return true;
109 }
110
111 @Nullable
112 public JetTypeReference getReturnTypeReference() {
113 return getStubOrPsiChild(JetStubElementTypes.TYPE_REFERENCE);
114 }
115
116 @NotNull
117 public PsiElement getNamePlaceholder() {
118 PsiElement get = findChildByType(JetTokens.GET_KEYWORD);
119 if (get != null) {
120 return get;
121 }
122 return findChildByType(JetTokens.SET_KEYWORD);
123 }
124
125 @Nullable
126 public ASTNode getRightParenthesis() {
127 return getNode().findChildByType(JetTokens.RPAR);
128 }
129
130 @Nullable
131 @Override
132 public JetExpression getInitializer() {
133 return PsiTreeUtil.getNextSiblingOfType(getEqualsToken(), JetExpression.class);
134 }
135
136 @Override
137 public boolean hasInitializer() {
138 return getInitializer() != null;
139 }
140 }