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