use of com.intellij.psi.PsiModifierList in project google-cloud-intellij by GoogleCloudPlatform.
the class ResourceParameterInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new EndpointPsiElementVisitor() {
private int resourceParameterCount = 0;
@Override
public void visitMethod(PsiMethod method) {
if (!EndpointUtilities.isEndpointClass(method)) {
return;
}
if (method.isConstructor()) {
return;
}
// Check if method is public or non-static
if (!EndpointUtilities.isApiMethod(method)) {
return;
}
Project project;
try {
project = method.getContainingFile().getProject();
if (project == null) {
return;
}
} catch (PsiInvalidElementAccessException ex) {
LOG.error("Error getting project with parameter " + method.getText(), ex);
return;
}
resourceParameterCount = 0;
for (PsiParameter param : method.getParameterList().getParameters()) {
validateMethodParameters(param, project);
}
// Check that there is no more than one resource (entity) parameter for this method.
if (resourceParameterCount > 1) {
holder.registerProblem(method, "Multiple entity parameters. There can only be a single entity parameter per method.", LocalQuickFix.EMPTY_ARRAY);
}
}
private void validateMethodParameters(PsiParameter psiParameter, Project project) {
// Check if parameter is of entity (resource) type which is not of parameter type or
// injected type.
PsiType type = psiParameter.getType();
if (!isEntityParameter(type, project)) {
return;
}
// Update count of resource (entity) parameters for this method
resourceParameterCount++;
// Check that parameter is not a collection or an array
if (type instanceof PsiArrayType || isCollectionType(type, project)) {
holder.registerProblem(psiParameter, "Illegal parameter type (\'" + psiParameter.getType().getPresentableText() + "\'). Arrays or collections of entity types are not allowed.", LocalQuickFix.EMPTY_ARRAY);
}
// Check that parameter does not have an @Named annotation
PsiModifierList modifierList = psiParameter.getModifierList();
PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named");
if (annotation == null) {
annotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED);
if (annotation == null) {
return;
}
}
holder.registerProblem(psiParameter, "Bad parameter name. Parameter is entity (resource)" + " type and should not be named.", LocalQuickFix.EMPTY_ARRAY);
}
};
}
use of com.intellij.psi.PsiModifierList in project google-cloud-intellij by GoogleCloudPlatform.
the class RestSignatureInspection method getResourceProperty.
@Nullable
private String getResourceProperty(PsiMethod psiMethod) {
PsiClass psiClass = psiMethod.getContainingClass();
PsiModifierList modifierList = psiClass.getModifierList();
String resource = null;
if (modifierList == null) {
return null;
}
// Get @ApiClass's resource attribute if it exists
for (PsiAnnotation annotation : modifierList.getAnnotations()) {
try {
resource = getAttributeFromAnnotation(annotation, GctConstants.APP_ENGINE_ANNOTATION_API_CLASS, "resource");
} catch (InvalidAnnotationException ex) {
// do nothing
} catch (MissingAttributeException ex) {
break;
}
// resource attribute is "" by default
if (resource != null && !resource.isEmpty()) {
return resource;
}
}
// Get @Api's resource attribute if it exists
for (PsiAnnotation annotation : modifierList.getAnnotations()) {
try {
resource = getAttributeFromAnnotation(annotation, GctConstants.APP_ENGINE_ANNOTATION_API, "resource");
} catch (InvalidAnnotationException ex) {
// do nothing
} catch (MissingAttributeException ex) {
break;
}
if (resource != null) {
// resource attribute is "" by default
if (resource.isEmpty()) {
break;
} else {
return resource;
}
}
}
return null;
}
use of com.intellij.psi.PsiModifierList in project google-cloud-intellij by GoogleCloudPlatform.
the class EndpointPsiElementVisitor method getNamedAnnotationValue.
/**
* Returns the value for @Named if it exists for <code>psiParameter</code> or null if it does not
* exist.
*
* @param psiParameter The parameter whose @Named value is to be returned.
* @return The @Named value if it exists for <code>psiParameter</code> or null if it does not
* exist.
*/
@Nullable
public PsiAnnotationMemberValue getNamedAnnotationValue(PsiParameter psiParameter) {
PsiModifierList modifierList = psiParameter.getModifierList();
if (modifierList == null) {
return null;
}
PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named");
if (annotation == null) {
annotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED);
if (annotation == null) {
return null;
}
}
PsiNameValuePair[] nameValuePairs = annotation.getParameterList().getAttributes();
if (nameValuePairs.length != 1) {
return null;
}
if (nameValuePairs[0] == null) {
return null;
}
return nameValuePairs[0].getValue();
}
use of com.intellij.psi.PsiModifierList in project google-cloud-intellij by GoogleCloudPlatform.
the class EndpointPsiElementVisitor method hasTransformer.
/**
* Returns true if the class containing <code>psiElement</code> has a transformer specified by
* using the @ApiTransformer annotation on a class or by using the transformer attribute of the
*
* @return True if the class containing <code>psiElement</code> has a transformer and false
* otherwise. @Api annotation. Returns false otherwise.
*/
public boolean hasTransformer(PsiElement psiElement) {
PsiClass psiClass = PsiUtils.findClass(psiElement);
if (psiClass == null) {
return false;
}
PsiModifierList modifierList = psiClass.getModifierList();
if (modifierList == null) {
return false;
}
// Check if class has @ApiTransformer to specify a transformer
PsiAnnotation apiTransformerAnnotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_API_TRANSFORMER);
if (apiTransformerAnnotation != null) {
return true;
}
// Check if class utilizes the transformer attribute of the @Api annotation
// to specify its transformer
PsiAnnotation apiAnnotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_API);
if (apiAnnotation != null) {
PsiAnnotationMemberValue transformerMember = apiAnnotation.findAttributeValue(API_TRANSFORMER_ATTRIBUTE);
if (transformerMember != null && !transformerMember.getText().equals("{}")) {
return true;
}
}
return false;
}
use of com.intellij.psi.PsiModifierList in project google-cloud-intellij by GoogleCloudPlatform.
the class ApiParameterInspection method hasParameterName.
private boolean hasParameterName(PsiParameter psiParameter) {
PsiModifierList modifierList = psiParameter.getModifierList();
if (modifierList == null) {
return false;
}
PsiAnnotation annotation = modifierList.findAnnotation("javax.inject.Named");
if (annotation != null) {
return true;
}
annotation = modifierList.findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NAMED);
if (annotation != null) {
return true;
}
return false;
}
Aggregations