use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable 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.GrVariable in project intellij-community by JetBrains.
the class UnusedDefInspection method isUsedInTopLevelFlowOnly.
private static boolean isUsedInTopLevelFlowOnly(PsiElement element) {
GrVariable var = null;
if (element instanceof GrVariable) {
var = (GrVariable) element;
} else if (element instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) element).resolve();
if (resolved instanceof GrVariable)
var = (GrVariable) resolved;
}
if (var != null) {
final GroovyPsiElement scope = ControlFlowUtils.findControlFlowOwner(var);
if (scope == null) {
PsiFile file = var.getContainingFile();
if (file == null) {
LOG.error("no file??? var of type" + var.getClass().getCanonicalName());
return false;
} else {
TextRange range = var.getTextRange();
LOG.error("var: " + var.getName() + ", offset:" + (range != null ? range.getStartOffset() : -1));
return false;
}
}
return ReferencesSearch.search(var, var.getUseScope()).forEach(ref -> ControlFlowUtils.findControlFlowOwner(ref.getElement()) == scope);
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrDocCommentUtil method findDocComment.
@Nullable
public static GrDocComment findDocComment(GrDocCommentOwner owner) {
if (owner.getFirstChild() instanceof GrDocComment) {
return ((GrDocComment) owner.getFirstChild());
}
PsiElement element = owner instanceof GrVariable && owner.getParent() instanceof GrVariableDeclaration ? owner.getParent() : owner;
element = skipWhiteSpacesAndStopOnDoc(element, false);
if (element instanceof GrDocComment)
return (GrDocComment) element;
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class ReachingDefinitionsCollector method addClosureUsages.
private static void addClosureUsages(final Map<String, VariableInfo> imap, final Map<String, VariableInfo> omap, final GrStatement first, final GrStatement last, GrControlFlowOwner flowOwner) {
flowOwner.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
addUsagesInClosure(imap, omap, closure, first, last);
super.visitClosure(closure);
}
private void addUsagesInClosure(final Map<String, VariableInfo> imap, final Map<String, VariableInfo> omap, final GrClosableBlock closure, final GrStatement first, final GrStatement last) {
closure.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression refExpr) {
if (refExpr.isQualified()) {
return;
}
PsiElement resolved = refExpr.resolve();
if (!(resolved instanceof GrVariable)) {
return;
}
GrVariable variable = (GrVariable) resolved;
if (PsiTreeUtil.isAncestor(closure, variable, true)) {
return;
}
if (variable instanceof ClosureSyntheticParameter && PsiTreeUtil.isAncestor(closure, ((ClosureSyntheticParameter) variable).getClosure(), false)) {
return;
}
String name = variable.getName();
if (!(variable instanceof GrField)) {
if (!isInFragment(first, last, resolved)) {
if (isInFragment(first, last, closure)) {
addVariable(name, imap, variable.getManager(), variable.getType());
}
} else {
if (!isInFragment(first, last, closure)) {
addVariable(name, omap, variable.getManager(), variable.getType());
}
}
}
}
});
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable in project intellij-community by JetBrains.
the class GrReferenceExpressionImpl method calculateType.
@Nullable
private static PsiType calculateType(@NotNull GrReferenceExpressionImpl refExpr, boolean forceRValue) {
final GroovyResolveResult[] results = refExpr.multiResolve(false, forceRValue);
final GroovyResolveResult result = PsiImplUtil.extractUniqueResult(results);
final PsiElement resolved = result.getElement();
for (GrExpressionTypeCalculator calculator : GrExpressionTypeCalculator.EP_NAME.getExtensions()) {
PsiType type = calculator.calculateType(refExpr, resolved);
if (type != null)
return type;
}
if (ResolveUtil.isClassReference(refExpr)) {
GrExpression qualifier = refExpr.getQualifier();
LOG.assertTrue(qualifier != null);
return qualifier.getType();
}
if (PsiUtil.isCompileStatic(refExpr)) {
final PsiType type;
if (resolved instanceof GrField) {
type = ((GrField) resolved).getType();
} else if (resolved instanceof GrVariable) {
type = ((GrVariable) resolved).getDeclaredType();
} else if (resolved instanceof GrAccessorMethod) {
type = ((GrAccessorMethod) resolved).getProperty().getType();
} else {
type = null;
}
if (type != null) {
return result.getSubstitutor().substitute(type);
}
}
final PsiType nominal = refExpr.getNominalType(forceRValue);
Boolean reassigned = GrReassignedLocalVarsChecker.isReassignedVar(refExpr);
if (reassigned != null && reassigned.booleanValue()) {
return GrReassignedLocalVarsChecker.getReassignedVarType(refExpr, true);
}
final PsiType inferred = getInferredTypes(refExpr, resolved);
if (inferred == null) {
if (nominal == null) {
//inside nested closure we could still try to infer from variable initializer. Not sound, but makes sense
if (resolved instanceof GrVariable) {
LOG.assertTrue(resolved.isValid());
return ((GrVariable) resolved).getTypeGroovy();
}
}
return nominal;
}
if (nominal == null)
return inferred;
if (!TypeConversionUtil.isAssignable(TypeConversionUtil.erasure(nominal), inferred, false)) {
if (resolved instanceof GrVariable && ((GrVariable) resolved).getTypeElementGroovy() != null) {
return nominal;
}
}
return inferred;
}
Aggregations