use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.
the class GrStubUtils method getAnnotationNames.
public static String[] getAnnotationNames(PsiModifierListOwner psi) {
List<String> annoNames = ContainerUtil.newArrayList();
final PsiModifierList modifierList = psi.getModifierList();
if (modifierList instanceof GrModifierList) {
for (GrAnnotation annotation : ((GrModifierList) modifierList).getRawAnnotations()) {
final String name = annotation.getShortName();
if (StringUtil.isNotEmpty(name)) {
annoNames.add(name);
}
}
}
return ArrayUtil.toStringArray(annoNames);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.
the class ResolveUtil method isScriptField.
public static boolean isScriptField(GrVariable var) {
GrModifierList list = var.getModifierList();
if (list == null)
return false;
PsiFile containingFile = var.getContainingFile();
if (!(containingFile instanceof GroovyFile) || !((GroovyFile) containingFile).isScript())
return false;
GrMember member = PsiTreeUtil.getParentOfType(var, GrTypeDefinition.class, GrMethod.class);
if (member != null)
return false;
for (GrAnnotation annotation : list.getAnnotations()) {
String qualifiedName = annotation.getQualifiedName();
if (qualifiedName == null)
continue;
if (qualifiedName.equals(GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD))
return true;
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.
the class LoggingContributor method applyTransformation.
@Override
public void applyTransformation(@NotNull TransformationContext context) {
GrModifierList modifierList = context.getCodeClass().getModifierList();
if (modifierList == null)
return;
for (GrAnnotation annotation : modifierList.getAnnotations()) {
String qname = annotation.getQualifiedName();
String logger = ourLoggers.get(qname);
if (logger != null) {
String fieldName = PsiUtil.getAnnoAttributeValue(annotation, "value", "log");
GrLightField field = new GrLightField(fieldName, logger, context.getCodeClass());
field.setNavigationElement(annotation);
field.getModifierList().setModifiers(PsiModifier.PRIVATE, PsiModifier.FINAL, PsiModifier.STATIC);
field.setOriginInfo("created by @" + annotation.getShortName());
context.addField(field);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.
the class DelegatesToInspection method buildVisitor.
@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitAnnotation(@NotNull GrAnnotation annotation) {
checkTarget(annotation);
checkDelegatesTo(annotation);
}
private void checkTarget(GrAnnotation annotation) {
if (!GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO_TARGET.equals(annotation.getQualifiedName()))
return;
final PsiElement owner = annotation.getParent().getParent();
if (!(owner instanceof GrParameter))
return;
final boolean isTargetDeclared = annotation.findDeclaredAttributeValue("value") != null;
String targetName = GrAnnotationUtil.inferStringAttribute(annotation, "value");
final GrParameterList parameterList = DefaultGroovyMethods.asType(owner.getParent(), GrParameterList.class);
for (GrParameter parameter : parameterList.getParameters()) {
final PsiAnnotation delegatesTo = parameter.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO);
if (delegatesTo != null) {
if (isTargetDeclared) {
final String curTarget = GrAnnotationUtil.inferStringAttribute(delegatesTo, "target");
if (curTarget != null && curTarget.equals(targetName)) {
//target is used
return;
}
} else {
if (delegatesTo.findDeclaredAttributeValue("target") == null && delegatesTo.findDeclaredAttributeValue("value") == null) {
// target is used
return;
}
}
}
}
registerError(annotation.getClassReference(), GroovyInspectionBundle.message("target.annotation.is.unused"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
private void checkDelegatesTo(GrAnnotation annotation) {
if (!GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO.equals(annotation.getQualifiedName()))
return;
final PsiElement owner = annotation.getParent().getParent();
if (!(owner instanceof GrParameter))
return;
final PsiAnnotationMemberValue targetPair = annotation.findDeclaredAttributeValue("target");
if (targetPair == null)
return;
String targetName = GrAnnotationUtil.inferStringAttribute(annotation, "target");
final GrParameterList parameterList = DefaultGroovyMethods.asType(owner.getParent(), GrParameterList.class);
for (GrParameter parameter : parameterList.getParameters()) {
final PsiAnnotation target = parameter.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_LANG_DELEGATES_TO_TARGET);
if (target != null) {
final String curTarget = GrAnnotationUtil.inferStringAttribute(target, "value");
if (curTarget != null && curTarget.equals(targetName)) {
//target is used
return;
}
}
}
registerError(targetPair, GroovyInspectionBundle.message("target.0.does.not.exist", targetName != null ? targetName : "?"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation in project intellij-community by JetBrains.
the class SuppressForMemberFix method addSuppressAnnotation.
private static void addSuppressAnnotation(final Project project, final GrModifierList modifierList, final String id) throws IncorrectOperationException {
PsiAnnotation annotation = modifierList.findAnnotation(BatchSuppressManager.SUPPRESS_INSPECTIONS_ANNOTATION_NAME);
final GrExpression toAdd = GroovyPsiElementFactory.getInstance(project).createExpressionFromText("\"" + id + "\"");
if (annotation != null) {
final PsiAnnotationMemberValue value = annotation.findDeclaredAttributeValue(null);
if (value instanceof GrAnnotationArrayInitializer) {
value.add(toAdd);
} else if (value != null) {
GrAnnotation anno = GroovyPsiElementFactory.getInstance(project).createAnnotationFromText("@A([])");
final GrAnnotationArrayInitializer list = (GrAnnotationArrayInitializer) anno.findDeclaredAttributeValue(null);
list.add(value);
list.add(toAdd);
annotation.setDeclaredAttributeValue(null, list);
}
} else {
modifierList.addAnnotation(BatchSuppressManager.SUPPRESS_INSPECTIONS_ANNOTATION_NAME).setDeclaredAttributeValue(null, toAdd);
}
}
Aggregations