use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class BaseScriptAnnotationChecker method checkApplicability.
@Override
public boolean checkApplicability(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
if (GroovyCommonClassNames.GROOVY_TRANSFORM_BASE_SCRIPT.equals(annotation.getQualifiedName())) {
PsiFile file = annotation.getContainingFile();
if (file instanceof GroovyFile && !(((GroovyFile) file).isScript())) {
holder.createErrorAnnotation(annotation, GroovyBundle.message("base.script.annotation.is.allowed.only.inside.scripts"));
return true;
}
PsiElement pparent = annotation.getParent().getParent();
if (pparent instanceof GrVariableDeclaration) {
GrTypeElement typeElement = ((GrVariableDeclaration) pparent).getTypeElementGroovy();
PsiType type = typeElement != null ? typeElement.getType() : null;
if (!InheritanceUtil.isInheritor(type, GroovyCommonClassNames.GROOVY_LANG_SCRIPT)) {
String typeText = type != null ? type.getCanonicalText() : CommonClassNames.JAVA_LANG_OBJECT;
holder.createErrorAnnotation(annotation, GroovyBundle.message("declared.type.0.have.to.extend.script", typeText));
return true;
}
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class FieldAnnotationChecker method checkApplicability.
@Override
public boolean checkApplicability(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
final String qname = annotation.getQualifiedName();
if (!GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD.equals(qname))
return false;
checkScriptField(holder, annotation);
PsiElement annoParent = annotation.getParent();
PsiElement ownerToUse = annoParent instanceof PsiModifierList ? annoParent.getParent() : annoParent;
if (!(ownerToUse instanceof GrVariableDeclaration)) {
return false;
} else {
GrVariableDeclaration declaration = (GrVariableDeclaration) ownerToUse;
if (declaration.getVariables().length != 1 || !PsiUtil.isLocalVariable(declaration.getVariables()[0])) {
return false;
}
}
if (!GrAnnotationImpl.isAnnotationApplicableTo(annotation, PsiAnnotation.TargetType.LOCAL_VARIABLE)) {
GrCodeReferenceElement ref = annotation.getClassReference();
String target = JavaErrorMessages.message("annotation.target.LOCAL_VARIABLE");
String description = JavaErrorMessages.message("annotation.not.applicable", ref.getText(), target);
holder.createErrorAnnotation(ref, description);
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GrUnnecessaryPublicModifierInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new PsiElementVisitor() {
@Override
public void visitElement(PsiElement modifier) {
if (modifier.getNode().getElementType() != GroovyTokenTypes.kPUBLIC)
return;
PsiElement list = modifier.getParent();
if (!(list instanceof GrModifierList))
return;
PsiElement parent = list.getParent();
// It may be put there explicitly to prevent getter/setter generation.
if (parent instanceof GrVariableDeclaration)
return;
holder.registerProblem(modifier, GroovyInspectionBundle.message("unnecessary.modifier.description", PsiModifier.PUBLIC), ProblemHighlightType.LIKE_UNUSED_SYMBOL, FIX);
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.
private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
PsiElement parent = element.getParent();
if (parent instanceof GrAnonymousClassDefinition) {
parent = parent.getParent();
}
if (parent instanceof GrNewExpression) {
return newExpressionProcessor.process((GrNewExpression) parent);
}
if (parent instanceof GrTypeElement) {
final GrTypeElement typeElement = (GrTypeElement) parent;
final PsiElement grandpa = typeElement.getParent();
if (grandpa instanceof GrVariableDeclaration) {
final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
if (vars.length == 1) {
final GrVariable variable = vars[0];
if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
return false;
}
}
} else if (grandpa instanceof GrMethod) {
final GrMethod method = (GrMethod) grandpa;
if (typeElement == method.getReturnTypeElementGroovy()) {
ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {
@Override
public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
return false;
}
return true;
}
});
}
} else if (grandpa instanceof GrTypeCastExpression) {
final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
} else if (grandpa instanceof GrSafeCastExpression) {
final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration in project intellij-community by JetBrains.
the class GroovyTypeCheckVisitor method visitVariable.
@Override
public void visitVariable(@NotNull GrVariable variable) {
super.visitVariable(variable);
final PsiType varType = variable.getType();
final PsiElement parent = variable.getParent();
if (variable instanceof GrParameter && ((GrParameter) variable).getDeclarationScope() instanceof GrMethod || parent instanceof GrForInClause) {
return;
} else if (parent instanceof GrVariableDeclaration && ((GrVariableDeclaration) parent).isTuple()) {
//check tuple assignment: def (int x, int y) = foo()
final GrVariableDeclaration tuple = (GrVariableDeclaration) parent;
final GrExpression initializer = tuple.getTupleInitializer();
if (initializer == null)
return;
if (!(initializer instanceof GrListOrMap)) {
PsiType type = initializer.getType();
if (type == null)
return;
PsiType valueType = extractIterableTypeParameter(type, false);
processAssignment(varType, valueType, tuple, variable.getNameIdentifierGroovy());
return;
}
}
GrExpression initializer = variable.getInitializerGroovy();
if (initializer == null)
return;
processAssignment(varType, initializer, variable.getNameIdentifierGroovy(), "cannot.assign", variable, ApplicableTo.ASSIGNMENT);
}
Aggregations