use of com.intellij.psi.PsiAnnotation 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.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.
the class InvalidParameterAnnotationsInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new EndpointPsiElementVisitor() {
@Override
public void visitMethod(PsiMethod method) {
if (!EndpointUtilities.isEndpointClass(method)) {
return;
}
if (!EndpointUtilities.isApiMethod(method)) {
return;
}
// Get all the method parameters
PsiParameterList parameterList = method.getParameterList();
if (parameterList.getParametersCount() == 0) {
return;
}
// Check for @ApiMethod
PsiAnnotation apiMethodAnnotation = method.getModifierList().findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_API_METHOD);
if (apiMethodAnnotation == null) {
return;
}
// path has a default value of ""
PsiAnnotationMemberValue pathMember = apiMethodAnnotation.findAttributeValue("path");
String path = EndpointUtilities.removeBeginningAndEndingQuotes(pathMember.getText());
// Check for path parameter, @ApiMethod(path="xys/{xy}")
Collection<String> pathParameters = getPathParameters(path);
if (pathParameters.size() == 0) {
return;
}
// parameters has the @Nullable or @DefaultValue
for (PsiParameter psiParameter : parameterList.getParameters()) {
PsiAnnotationMemberValue namedAnnotation = getNamedAnnotationValue(psiParameter);
if (namedAnnotation == null) {
continue;
}
String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(namedAnnotation.getText());
// Check is @Named value is in list of path parameters
if (!pathParameters.contains(nameValue)) {
continue;
}
// Check if @Named parameter also has @Nullable or @DefaultValue
if ((psiParameter.getModifierList().findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_NULLABLE) != null) || (psiParameter.getModifierList().findAnnotation("javax.annotation.Nullable") != null) || (psiParameter.getModifierList().findAnnotation(GctConstants.APP_ENGINE_ANNOTATION_DEFAULT_VALUE) != null)) {
holder.registerProblem(psiParameter, "Invalid parameter configuration. " + "A parameter in the method path should not be marked @Nullable or " + "@DefaultValue.", new MyQuickFix());
}
}
}
/**
* Gets the parameters in <code>path</code>
*
* @param path The path whose parameters are to be parsed.
* @return A collection of the parameter in <code>path</code>
*/
private Collection<String> getPathParameters(String path) {
Pattern pathPattern = Pattern.compile("\\{([^\\}]*)\\}");
Matcher pathMatcher = pathPattern.matcher(path);
Collection<String> pathParameters = new HashSet<String>();
while (pathMatcher.find()) {
pathParameters.add(pathMatcher.group(1));
}
return pathParameters;
}
};
}
use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.
the class ApiNameInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final com.intellij.codeInspection.ProblemsHolder holder, boolean isOnTheFly) {
return new EndpointPsiElementVisitor() {
@Override
public void visitAnnotation(PsiAnnotation annotation) {
if (annotation == null) {
return;
}
if (!GctConstants.APP_ENGINE_ANNOTATION_API.equals(annotation.getQualifiedName())) {
return;
}
// Need to check for user added attributes because default values are used when not
// specified by user and we are only interested in the user specified values
PsiAnnotationParameterList parameterList = annotation.getParameterList();
if (parameterList.getAttributes().length == 0) {
return;
}
PsiAnnotationMemberValue annotationMemberValue = annotation.findAttributeValue(API_NAME_ATTRIBUTE);
if (annotationMemberValue == null) {
return;
}
String nameValueWithQuotes = annotationMemberValue.getText();
String nameValue = EndpointUtilities.removeBeginningAndEndingQuotes(nameValueWithQuotes);
// Empty API name is valid
if (nameValue.isEmpty()) {
return;
}
if (!API_NAME_PATTERN.matcher(nameValue).matches()) {
holder.registerProblem(annotationMemberValue, "Invalid api name: it must start with a lower case letter and consists only of " + "letter and digits", new MyQuickFix());
}
}
};
}
use of com.intellij.psi.PsiAnnotation 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;
}
use of com.intellij.psi.PsiAnnotation in project google-cloud-intellij by GoogleCloudPlatform.
the class NamedResourceInspectionTest method testQuickFix_duplicateParameter.
/**
* Tests that the NamedResourceInspection's quick fix flagged with {@link
* NamedResourceError#DUPLICATE_PARAMETER} for an @Named annotation updates the query name by
* adding "_1" as a suffix.
*/
public void testQuickFix_duplicateParameter() {
Project myProject = myFixture.getProject();
String annotationString = "@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + "(\"someName\")";
PsiAnnotation annotation = JavaPsiFacade.getInstance(myProject).getElementFactory().createAnnotationFromText(annotationString, null);
NamedResourceInspection.DuplicateNameQuickFix myQuickFix = new NamedResourceInspection().new DuplicateNameQuickFix();
MockProblemDescriptor problemDescriptor = new MockProblemDescriptor(annotation, "", ProblemHighlightType.ERROR);
myQuickFix.applyFix(myProject, problemDescriptor);
assertEquals("@" + GctConstants.APP_ENGINE_ANNOTATION_NAMED + "(\"someName_1\")", annotation.getText());
}
Aggregations