001 /* 002 * Copyright 2010-2016 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.asJava; 018 019 import com.intellij.lang.Language; 020 import com.intellij.openapi.util.TextRange; 021 import com.intellij.psi.*; 022 import com.intellij.psi.search.GlobalSearchScope; 023 import com.intellij.psi.search.SearchScope; 024 import com.intellij.util.ArrayUtil; 025 import com.intellij.util.IncorrectOperationException; 026 import org.jetbrains.annotations.NonNls; 027 import org.jetbrains.annotations.NotNull; 028 import org.jetbrains.annotations.Nullable; 029 import org.jetbrains.kotlin.idea.KotlinLanguage; 030 import org.jetbrains.kotlin.psi.*; 031 import org.jetbrains.kotlin.psi.psiUtil.KtPsiUtilKt; 032 033 import java.util.List; 034 035 public class KtLightParameter extends LightParameter implements KtLightDeclaration<KtParameter, PsiParameter> { 036 private static String getName(PsiParameter delegate, int index) { 037 String name = delegate.getName(); 038 return name != null ? name : "p" + index; 039 } 040 041 private final PsiModifierList modifierList; 042 private final PsiParameter delegate; 043 private final int index; 044 private final KtLightMethod method; 045 private KtLightIdentifier lightIdentifier = null; 046 047 public KtLightParameter(final PsiParameter delegate, int index, KtLightMethod method) { 048 super(getName(delegate, index), delegate.getType(), method, KotlinLanguage.INSTANCE); 049 050 this.delegate = delegate; 051 this.index = index; 052 this.method = method; 053 054 if (method.getLightMethodOrigin() instanceof LightMemberOriginForDeclaration) { 055 this.modifierList = new KtLightModifierListWithExplicitModifiers(this, ArrayUtil.EMPTY_STRING_ARRAY) { 056 @Override 057 public PsiAnnotationOwner getDelegate() { 058 return delegate.getModifierList(); 059 } 060 }; 061 } 062 else { 063 this.modifierList = super.getModifierList(); 064 } 065 } 066 067 @NotNull 068 @Override 069 public PsiModifierList getModifierList() { 070 return modifierList; 071 } 072 073 @NotNull 074 @Override 075 public PsiParameter getClsDelegate() { 076 return delegate; 077 } 078 079 @Nullable 080 @Override 081 public KtParameter getKotlinOrigin() { 082 KtDeclaration declaration = method.getKotlinOrigin(); 083 if (declaration == null) return null; 084 085 int jetIndex = KtPsiUtilKt.isExtensionDeclaration(declaration) ? index - 1 : index; 086 if (jetIndex < 0) return null; 087 088 if (declaration instanceof KtFunction) { 089 List<KtParameter> paramList = ((KtFunction) declaration).getValueParameters(); 090 return jetIndex < paramList.size() ? paramList.get(jetIndex) : null; 091 } 092 093 if (jetIndex != 0) return null; 094 095 KtPropertyAccessor setter = null; 096 if (declaration instanceof KtPropertyAccessor) { 097 KtPropertyAccessor accessor = (KtPropertyAccessor) declaration; 098 setter = accessor.isSetter() ? accessor : null; 099 } 100 else if (declaration instanceof KtProperty) { 101 setter = ((KtProperty) declaration).getSetter(); 102 } 103 else if (declaration instanceof KtParameter) { 104 return (KtParameter) declaration; 105 } 106 107 return setter != null ? setter.getParameter() : null; 108 } 109 110 @NotNull 111 @Override 112 public PsiElement getNavigationElement() { 113 KtParameter origin = getKotlinOrigin(); 114 return origin != null ? origin : super.getNavigationElement(); 115 } 116 117 @Override 118 public boolean isValid() { 119 return method.isValid(); 120 } 121 122 @Override 123 public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException { 124 KtParameter origin = getKotlinOrigin(); 125 if (origin != null) { 126 origin.setName(name); 127 } 128 return this; 129 } 130 131 @Override 132 public PsiFile getContainingFile() { 133 return method.getContainingFile(); 134 } 135 136 @NotNull 137 @Override 138 public Language getLanguage() { 139 return KotlinLanguage.INSTANCE; 140 } 141 142 @NotNull 143 @Override 144 public SearchScope getUseScope() { 145 KtParameter origin = getKotlinOrigin(); 146 return origin != null ? origin.getUseScope() : GlobalSearchScope.EMPTY_SCOPE; 147 } 148 149 public KtLightMethod getMethod() { 150 return method; 151 } 152 153 @Override 154 public String getText() { 155 return ""; 156 } 157 158 @Override 159 public TextRange getTextRange() { 160 KtParameter origin = getKotlinOrigin(); 161 return origin != null ? origin.getTextRange() : TextRange.EMPTY_RANGE; 162 } 163 164 @Override 165 public PsiIdentifier getNameIdentifier() { 166 if (lightIdentifier == null) { 167 lightIdentifier = new KtLightIdentifier(this, getKotlinOrigin()); 168 } 169 return lightIdentifier; 170 } 171 172 @Override 173 public PsiElement getParent() { 174 return getMethod().getParameterList(); 175 } 176 177 @Override 178 public boolean isEquivalentTo(PsiElement another) { 179 KtParameter kotlinOrigin = getKotlinOrigin(); 180 if (another instanceof KtLightParameter && kotlinOrigin != null) { 181 KtLightParameter anotherParam = (KtLightParameter) another; 182 return kotlinOrigin.equals(anotherParam.getKotlinOrigin()) && getClsDelegate().equals(anotherParam.getClsDelegate()); 183 } 184 return super.isEquivalentTo(another); 185 } 186 }