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