use of com.intellij.psi.PsiAnnotation in project kotlin by JetBrains.
the class ResourceEvaluator method getTypesFromAnnotations.
@Nullable
private EnumSet<ResourceType> getTypesFromAnnotations(PsiModifierListOwner owner) {
if (mEvaluator == null) {
return null;
}
for (PsiAnnotation annotation : mEvaluator.getAllAnnotations(owner)) {
String signature = annotation.getQualifiedName();
if (signature == null) {
continue;
}
if (signature.equals(COLOR_INT_ANNOTATION)) {
return EnumSet.of(COLOR_INT_MARKER_TYPE);
}
if (signature.equals(PX_ANNOTATION)) {
return EnumSet.of(PX_MARKER_TYPE);
}
if (signature.endsWith(RES_SUFFIX) && signature.startsWith(SUPPORT_ANNOTATIONS_PREFIX)) {
String typeString = signature.substring(SUPPORT_ANNOTATIONS_PREFIX.length(), signature.length() - RES_SUFFIX.length()).toLowerCase(Locale.US);
ResourceType type = ResourceType.getEnum(typeString);
if (type != null) {
return EnumSet.of(type);
} else if (typeString.equals("any")) {
// @AnyRes
return getAnyRes();
}
}
}
return null;
}
use of com.intellij.psi.PsiAnnotation in project kotlin by JetBrains.
the class CallSuperDetector method getRequiredSuperMethod.
/**
* Checks whether the given method overrides a method which requires the super method
* to be invoked, and if so, returns it (otherwise returns null)
*/
@Nullable
private static PsiMethod getRequiredSuperMethod(@NonNull JavaContext context, @NonNull PsiMethod method) {
JavaEvaluator evaluator = context.getEvaluator();
PsiMethod directSuper = evaluator.getSuperMethod(method);
if (directSuper == null) {
return null;
}
String name = method.getName();
if (ON_DETACHED_FROM_WINDOW.equals(name)) {
// compileSdkVersion >= N).
if (!evaluator.isMemberInSubClassOf(method, CLASS_VIEW, false)) {
return null;
}
return directSuper;
} else if (ON_VISIBILITY_CHANGED.equals(name)) {
// the support library
if (!evaluator.isMemberInSubClassOf(method, "android.support.wearable.watchface.WatchFaceService.Engine", false)) {
return null;
}
return directSuper;
}
// Look up annotations metadata
PsiMethod superMethod = directSuper;
while (superMethod != null) {
PsiAnnotation[] annotations = superMethod.getModifierList().getAnnotations();
annotations = filterRelevantAnnotations(context.getEvaluator(), annotations);
for (PsiAnnotation annotation : annotations) {
String signature = annotation.getQualifiedName();
if (CALL_SUPER_ANNOTATION.equals(signature)) {
return directSuper;
} else if (signature != null && signature.endsWith(".OverrideMustInvoke")) {
// Handle findbugs annotation on the fly too
return directSuper;
}
}
superMethod = evaluator.getSuperMethod(superMethod);
}
return null;
}
use of com.intellij.psi.PsiAnnotation 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.PsiAnnotation 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.PsiAnnotation 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();
}
Aggregations