001    /*
002     * Copyright 2010-2014 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.asJava;
018    
019    import com.intellij.lang.Language;
020    import com.intellij.psi.PsiAnnotationOwner;
021    import com.intellij.psi.PsiModifierList;
022    import com.intellij.psi.PsiParameter;
023    import com.intellij.util.ArrayUtil;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.annotations.Nullable;
026    import org.jetbrains.jet.lang.psi.*;
027    import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
028    import org.jetbrains.jet.plugin.JetLanguage;
029    
030    import java.util.List;
031    
032    public class KotlinLightParameter extends LightParameter implements KotlinLightElement<JetParameter, PsiParameter> {
033        private static String getName(PsiParameter delegate, int index) {
034            String name = delegate.getName();
035            return name != null ? name : "p" + index;
036        }
037    
038        private final PsiModifierList modifierList;
039        private final PsiParameter delegate;
040        private final int index;
041        private final KotlinLightMethod method;
042    
043        public KotlinLightParameter(final PsiParameter delegate, int index, KotlinLightMethod method) {
044            super(getName(delegate, index), delegate.getType(), method, JetLanguage.INSTANCE);
045    
046            this.delegate = delegate;
047            this.index = index;
048            this.method = method;
049    
050            this.modifierList = new KotlinLightModifierList(method.getManager(), ArrayUtil.EMPTY_STRING_ARRAY) {
051                @Override
052                public PsiAnnotationOwner getDelegate() {
053                    return delegate.getModifierList();
054                }
055            };
056        }
057    
058        @NotNull
059        @Override
060        public PsiModifierList getModifierList() {
061            return modifierList;
062        }
063    
064        @NotNull
065        @Override
066        public PsiParameter getDelegate() {
067            return delegate;
068        }
069    
070        @Nullable
071        @Override
072        public JetParameter getOrigin() {
073            JetDeclaration declaration = method.getOrigin();
074            if (declaration == null) return null;
075    
076            int jetIndex = PsiUtilPackage.isExtensionDeclaration(declaration) ? index - 1 : index;
077            if (jetIndex < 0) return null;
078    
079            if (declaration instanceof JetNamedFunction) {
080                List<JetParameter> paramList = ((JetNamedFunction) declaration).getValueParameters();
081                return jetIndex < paramList.size() ? paramList.get(jetIndex) : null;
082            }
083    
084            if (declaration instanceof JetClass) {
085                List<JetParameter> paramList = ((JetClass) declaration).getPrimaryConstructorParameters();
086                return jetIndex < paramList.size() ? paramList.get(jetIndex) : null;
087            }
088    
089            if (jetIndex != 0) return null;
090    
091            JetPropertyAccessor setter = null;
092            if (declaration instanceof JetPropertyAccessor) {
093                JetPropertyAccessor accessor = (JetPropertyAccessor) declaration;
094                setter = accessor.isSetter() ? accessor : null;
095            }
096            else if (declaration instanceof JetProperty) {
097                setter = ((JetProperty) declaration).getSetter();
098            }
099    
100            return setter != null ? setter.getParameter() : null;
101        }
102    
103        @NotNull
104        @Override
105        public Language getLanguage() {
106            return JetLanguage.INSTANCE;
107        }
108    }