提交 541fc8ec 编写于 作者: S Stepan Koltsov

Psi* wrappers to work in jet-signature annotations

refactoring to reduce amount of code in JavaDescriptorResolver
上级 200fd785
package org.jetbrains.jet.lang.resolve.java;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiParameter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.java.kt.JetConstructorAnnotation;
import org.jetbrains.jet.lang.resolve.java.kt.JetMethodAnnotation;
import org.jetbrains.jet.lang.resolve.java.kt.JetPropertyAnnotation;
import java.util.ArrayList;
import java.util.List;
/**
* @author Stepan Koltsov
*/
public class PsiMethodWrapper {
@NotNull
private final PsiMethod psiMethod;
public PsiMethodWrapper(@NotNull PsiMethod psiMethod) {
this.psiMethod = psiMethod;
}
private List<PsiParameterWrapper> parameters;
@NotNull
public List<PsiParameterWrapper> getParameters() {
if (parameters == null) {
PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
parameters = new ArrayList<PsiParameterWrapper>(psiParameters.length);
for (int i = 0; i < psiParameters.length; ++i) {
parameters.add(new PsiParameterWrapper(psiParameters[i]));
}
}
return parameters;
}
@NotNull
public PsiParameterWrapper getParameter(int i) {
return getParameters().get(i);
}
public boolean isStatic() {
return psiMethod.getModifierList().hasExplicitModifier(PsiModifier.STATIC);
}
public boolean isPrivate() {
return psiMethod.hasModifierProperty(PsiModifier.PRIVATE);
}
private JetMethodAnnotation jetMethod;
@NotNull
public JetMethodAnnotation getJetMethod() {
if (jetMethod == null) {
jetMethod = JetMethodAnnotation.get(psiMethod);
}
return jetMethod;
}
private JetConstructorAnnotation jetConstructor;
@NotNull
public JetConstructorAnnotation getJetConstructor() {
if (jetConstructor == null) {
jetConstructor = JetConstructorAnnotation.get(psiMethod);
}
return jetConstructor;
}
private JetPropertyAnnotation jetProperty;
@NotNull
public JetPropertyAnnotation getJetProperty() {
if (jetProperty == null) {
jetProperty = JetPropertyAnnotation.get(psiMethod);
}
return jetProperty;
}
}
package org.jetbrains.jet.lang.resolve.java;
import com.intellij.psi.PsiParameter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.java.kt.JetTypeParameterAnnotation;
import org.jetbrains.jet.lang.resolve.java.kt.JetValueParameterAnnotation;
/**
* @author Stepan Koltsov
*/
public class PsiParameterWrapper {
private final PsiParameter psiParameter;
public PsiParameterWrapper(@NotNull PsiParameter psiParameter) {
this.psiParameter = psiParameter;
this.jetValueParameter = JetValueParameterAnnotation.get(psiParameter);
this.jetTypeParameter = JetTypeParameterAnnotation.get(psiParameter);
}
private JetValueParameterAnnotation jetValueParameter;
private JetTypeParameterAnnotation jetTypeParameter;
@NotNull
public PsiParameter getPsiParameter() {
return psiParameter;
}
@NotNull
public JetValueParameterAnnotation getJetValueParameter() {
return jetValueParameter;
}
@NotNull
public JetTypeParameterAnnotation getJetTypeParameter() {
return jetTypeParameter;
}
}
package org.jetbrains.jet.lang.resolve.java.kt;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
/**
* @author Stepan Koltsov
*/
public class JetClassAnnotation extends PsiAnnotationWrapper {
public JetClassAnnotation(@Nullable PsiAnnotation psiAnnotation) {
super(psiAnnotation);
}
private String signature;
public String signature() {
if (signature == null) {
signature = getStringAttribute(JvmStdlibNames.JET_CLASS_SIGNATURE, "");
}
return signature;
}
@NotNull
public static JetClassAnnotation get(PsiClass psiClass) {
return new JetClassAnnotation(psiClass.getModifierList().findAnnotation(JvmStdlibNames.JET_CLASS.getFqName()));
}
}
package org.jetbrains.jet.lang.resolve.java.kt;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
/**
* @author Stepan Koltsov
*/
public class JetConstructorAnnotation extends PsiAnnotationWrapper {
public JetConstructorAnnotation(@Nullable PsiAnnotation psiAnnotation) {
super(psiAnnotation);
}
private boolean hidden;
private boolean hiddenInitialized = false;
/** @deprecated */
public boolean hidden() {
if (!hiddenInitialized) {
hidden = getBooleanAttribute(JvmStdlibNames.JET_CONSTRUCTOR_HIDDEN_FIELD, false);
hiddenInitialized = true;
}
return hidden;
}
public static JetConstructorAnnotation get(PsiMethod constructor) {
return new JetConstructorAnnotation(constructor.getModifierList().findAnnotation(JvmStdlibNames.JET_CONSTRUCTOR.getFqName()));
}
}
package org.jetbrains.jet.lang.resolve.java.kt;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
/**
* @author Stepan Koltsov
*/
public class JetMethodAnnotation extends PsiAnnotationWrapper {
public JetMethodAnnotation(@Nullable PsiAnnotation psiAnnotation) {
super(psiAnnotation);
}
private String typeParameters;
@NotNull
public String typeParameters() {
if (typeParameters == null) {
typeParameters = getStringAttribute(JvmStdlibNames.JET_METHOD_TYPE_PARAMETERS_FIELD, "");
}
return typeParameters;
}
private String returnType;
@NotNull
public String returnType() {
if (returnType == null) {
returnType = getStringAttribute(JvmStdlibNames.JET_METHOD_RETURN_TYPE_FIELD, "");
}
return returnType;
}
private boolean returnTypeNullable;
private boolean returnTypeNullableInitialized;
@NotNull
public boolean returnTypeNullable() {
if (!returnTypeNullableInitialized) {
returnTypeNullable = getBooleanAttribute(JvmStdlibNames.JET_METHOD_NULLABLE_RETURN_TYPE_FIELD, false);
returnTypeNullableInitialized = true;
}
return returnTypeNullable;
}
public static JetMethodAnnotation get(PsiMethod psiMethod) {
return new JetMethodAnnotation(psiMethod.getModifierList().findAnnotation(JvmStdlibNames.JET_METHOD.getFqName()));
}
}
package org.jetbrains.jet.lang.resolve.java.kt;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiMethod;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
/**
* @author Stepan Koltsov
*/
public class JetPropertyAnnotation extends PsiAnnotationWrapper {
protected JetPropertyAnnotation(@Nullable PsiAnnotation psiAnnotation) {
super(psiAnnotation);
}
@NotNull
public static JetPropertyAnnotation get(@NotNull PsiMethod psiMethod) {
return new JetPropertyAnnotation(psiMethod.getModifierList().findAnnotation(JvmStdlibNames.JET_PROPERTY.getFqName()));
}
}
package org.jetbrains.jet.lang.resolve.java.kt;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiParameter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
/**
* @author Stepan Koltsov
*/
public class JetTypeParameterAnnotation extends PsiAnnotationWrapper {
protected JetTypeParameterAnnotation(@Nullable PsiAnnotation psiAnnotation) {
super(psiAnnotation);
}
@NotNull
public static JetTypeParameterAnnotation get(@NotNull PsiParameter psiParameter) {
return new JetTypeParameterAnnotation(psiParameter.getModifierList().findAnnotation(JvmStdlibNames.JET_TYPE_PARAMETER.getFqName()));
}
}
package org.jetbrains.jet.lang.resolve.java.kt;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiParameter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.JvmStdlibNames;
/**
* @author Stepan Koltsov
*/
public class JetValueParameterAnnotation extends PsiAnnotationWrapper {
public JetValueParameterAnnotation(@Nullable PsiAnnotation psiAnnotation) {
super(psiAnnotation);
}
private String name;
@NotNull
public String name() {
if (name == null) {
name = getStringAttribute(JvmStdlibNames.JET_VALUE_PARAMETER_NAME_FIELD, "");
}
return name;
}
private String type;
@NotNull
public String type() {
if (type == null) {
type = getStringAttribute(JvmStdlibNames.JET_VALUE_PARAMETER_TYPE_FIELD, "");
}
return type;
}
private boolean nullable;
private boolean nullableInitialized = false;
public boolean nullable() {
if (!nullableInitialized) {
nullable = getBooleanAttribute(JvmStdlibNames.JET_VALUE_PARAMETER_NULLABLE_FIELD, false);
nullableInitialized = true;
}
return nullable;
}
private boolean receiver;
private boolean receiverInitialized = false;
public boolean receiver() {
if (!receiverInitialized) {
receiver = getBooleanAttribute(JvmStdlibNames.JET_VALUE_PARAMETER_RECEIVER_FIELD, false);
receiverInitialized = true;
}
return receiver;
}
private boolean hasDefaultValue;
private boolean hasDefaultValueInitialized = false;
public boolean hasDefaultValue() {
if (!hasDefaultValueInitialized) {
hasDefaultValue = getBooleanAttribute(JvmStdlibNames.JET_VALUE_PARAMETER_HAS_DEFAULT_VALUE_FIELD, false);
hasDefaultValueInitialized = true;
}
return hasDefaultValue;
}
public static JetValueParameterAnnotation get(PsiParameter psiParameter) {
return new JetValueParameterAnnotation(psiParameter.getModifierList().findAnnotation(JvmStdlibNames.JET_VALUE_PARAMETER.getFqName()));
}
}
package org.jetbrains.jet.lang.resolve.java.kt;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiLiteralExpression;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Stepan Koltsov
*/
public class PsiAnnotationUtils {
@NotNull
public static String getStringAttribute(@Nullable PsiAnnotation annotation, @NotNull String field, @NotNull String defaultValue) {
return getAttribute(annotation, field, defaultValue);
}
public static boolean getBooleanAttribute(@Nullable PsiAnnotation annotation, @NotNull String field, boolean defaultValue) {
return getAttribute(annotation, field, defaultValue);
}
private static <T> T getAttribute(@Nullable PsiAnnotation annotation, @NotNull String field, @NotNull T defaultValue) {
if (annotation == null) {
return defaultValue;
} else {
PsiLiteralExpression attributeValue = (PsiLiteralExpression) annotation.findAttributeValue(field);
if (attributeValue != null) {
return (T) attributeValue.getValue();
} else {
return defaultValue;
}
}
}
}
package org.jetbrains.jet.lang.resolve.java.kt;
import com.intellij.psi.PsiAnnotation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Stepan Koltsov
*/
public abstract class PsiAnnotationWrapper {
@Nullable
private PsiAnnotation psiAnnotation;
protected PsiAnnotationWrapper(@Nullable PsiAnnotation psiAnnotation) {
this.psiAnnotation = psiAnnotation;
}
@Nullable
public PsiAnnotation getPsiAnnotation() {
return psiAnnotation;
}
public boolean isDefined() {
return psiAnnotation != null;
}
@NotNull
protected String getStringAttribute(String name, String defaultValue) {
return PsiAnnotationUtils.getStringAttribute(psiAnnotation, name, defaultValue);
}
protected boolean getBooleanAttribute(String name, boolean defaultValue) {
return PsiAnnotationUtils.getBooleanAttribute(psiAnnotation, name, defaultValue);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册