use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod 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.typedef.members.GrMethod in project intellij-community by JetBrains.
the class SpockMemberContributor method processDynamicElements.
@Override
public void processDynamicElements(@NotNull PsiType qualifierType, PsiClass aClass, @NotNull PsiScopeProcessor processor, @NotNull PsiElement place, @NotNull ResolveState state) {
ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);
if (ResolveUtil.shouldProcessProperties(classHint)) {
GrMethod method = PsiTreeUtil.getParentOfType(place, GrMethod.class);
if (method == null)
return;
if (aClass != method.getContainingClass())
return;
Map<String, SpockVariableDescriptor> cachedValue = SpockUtils.getVariableMap(method);
String nameHint = ResolveUtil.getNameHint(processor);
if (nameHint == null) {
for (SpockVariableDescriptor spockVar : cachedValue.values()) {
if (!processor.execute(spockVar.getVariable(), state))
return;
}
} else {
SpockVariableDescriptor spockVar = cachedValue.get(nameHint);
if (spockVar != null && spockVar.getNavigationElement() != place) {
if (!processor.execute(spockVar.getVariable(), state))
return;
}
}
}
if (ResolveUtil.shouldProcessMethods(classHint)) {
String nameHint = ResolveUtil.getNameHint(processor);
if (nameHint == null) {
nameHint = place instanceof GrReferenceExpression ? ((GrReferenceExpression) place).getReferenceName() : null;
if (nameHint != null)
nameHint = GroovyPropertyUtils.getGetterNameNonBoolean(nameHint);
}
if ("get_".equals(nameHint)) {
GrLightMethodBuilder m = new GrLightMethodBuilder(aClass.getManager(), "get_");
m.setReturnType(null);
if (!processor.execute(m, state))
return;
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class SpockUnrollReferenceProvider method getReferencesByElement.
@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
GrAnnotationNameValuePair nvp = (GrAnnotationNameValuePair) element.getParent();
String name = nvp.getName();
if (name != null && !name.equals("value"))
return PsiReference.EMPTY_ARRAY;
PsiElement argumentList = nvp.getParent();
if (!(argumentList instanceof GrAnnotationArgumentList))
return PsiReference.EMPTY_ARRAY;
PsiElement eAnnotation = argumentList.getParent();
if (!(eAnnotation instanceof GrAnnotation))
return PsiReference.EMPTY_ARRAY;
GrAnnotation annotation = (GrAnnotation) eAnnotation;
String shortName = annotation.getShortName();
if (!shortName.equals("Unroll") && !shortName.equals("spock.lang.Unroll"))
return PsiReference.EMPTY_ARRAY;
PsiElement modifierList = annotation.getParent();
if (!(modifierList instanceof GrModifierList))
return PsiReference.EMPTY_ARRAY;
PsiElement eMethod = modifierList.getParent();
if (!(eMethod instanceof GrMethod))
return PsiReference.EMPTY_ARRAY;
final GrMethod method = (GrMethod) eMethod;
ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(element);
TextRange rangeInElement = manipulator.getRangeInElement(element);
String text = rangeInElement.substring(element.getText());
final List<SpockVariableReference> references = new ArrayList<>();
Matcher matcher = PATTERN.matcher(text);
while (matcher.find()) {
TextRange range = new TextRange(rangeInElement.getStartOffset() + matcher.start(1), rangeInElement.getStartOffset() + matcher.end(1));
references.add(new SpockVariableReference(element, range, references, method));
}
return references.toArray(new PsiReference[references.size()]);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class SpockUtils method isTestMethod.
public static boolean isTestMethod(PsiElement element) {
if (!(element instanceof GrMethod))
return false;
GrMethod method = ((GrMethod) element);
PsiClass clazz = method.getContainingClass();
if (!isSpecification(clazz))
return false;
if (isFixtureMethod(method))
return false;
return isFeatureMethod(method);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod 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