use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class StubGenerator method writeConstructor.
@Override
public void writeConstructor(final StringBuilder text, PsiMethod constructor, boolean isEnum) {
LOG.assertTrue(constructor.isConstructor());
if (!isEnum) {
text.append("public ");
//writeModifiers(text, constructor.getModifierList(), JAVA_MODIFIERS);
}
/************* name **********/
//append constructor name
text.append(constructor.getName());
/************* parameters **********/
GenerationUtil.writeParameterList(text, constructor.getParameterList().getParameters(), classNameProvider, null);
final Set<String> throwsTypes = collectThrowsTypes(constructor, new THashSet<>());
if (!throwsTypes.isEmpty()) {
text.append("throws ").append(StringUtil.join(throwsTypes, ", ")).append(' ');
}
/************* body **********/
text.append("{\n");
if (constructor instanceof GrReflectedMethod) {
constructor = ((GrReflectedMethod) constructor).getBaseMethod();
}
if (constructor instanceof GrMethod) {
final GrConstructorInvocation invocation = PsiImplUtil.getChainingConstructorInvocation((GrMethod) constructor);
if (invocation != null) {
final GroovyResolveResult resolveResult = resolveChainingConstructor((GrMethod) constructor);
if (resolveResult != null) {
text.append(invocation.isSuperCall() ? "super(" : "this(");
writeStubConstructorInvocation(text, (PsiMethod) resolveResult.getElement(), resolveResult.getSubstitutor(), invocation);
text.append(");");
}
} else if (constructor instanceof LightElement) {
writeStubConstructorInvocation(constructor, text);
}
}
text.append("\n}\n");
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GroovyEncapsulateFieldHelper method processUsage.
@Override
public boolean processUsage(@NotNull EncapsulateFieldUsageInfo usage, @NotNull EncapsulateFieldsDescriptor descriptor, PsiMethod setter, PsiMethod getter) {
final PsiElement element = usage.getElement();
if (!(element instanceof GrReferenceExpression))
return false;
final FieldDescriptor fieldDescriptor = usage.getFieldDescriptor();
PsiField field = fieldDescriptor.getField();
boolean processGet = descriptor.isToEncapsulateGet();
boolean processSet = descriptor.isToEncapsulateSet() && !field.hasModifierProperty(PsiModifier.FINAL);
if (!processGet && !processSet)
return true;
final GrReferenceExpression expr = (GrReferenceExpression) element;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(descriptor.getTargetClass().getProject());
try {
if (!descriptor.isToUseAccessorsWhenAccessible()) {
PsiModifierList newModifierList = JavaEncapsulateFieldHelper.createNewModifierList(descriptor);
PsiClass accessObjectClass = getAccessObject(expr);
final PsiResolveHelper helper = JavaPsiFacade.getInstance((expr).getProject()).getResolveHelper();
if (helper.isAccessible(fieldDescriptor.getField(), newModifierList, expr, accessObjectClass, null)) {
GroovyResolveResult[] results = expr.multiResolve(false);
if (ContainerUtil.or(results, it -> it.isValidResult() && it.getElement() instanceof PsiMethod)) {
addMemberOperator(expr, field);
}
return true;
}
}
final PsiElement parent = expr.getParent();
if (parent instanceof GrAssignmentExpression && expr.equals(((GrAssignmentExpression) parent).getLValue())) {
GrAssignmentExpression assignment = (GrAssignmentExpression) parent;
if (assignment.getRValue() != null) {
PsiElement opSign = assignment.getOperationToken();
IElementType opType = assignment.getOperationTokenType();
if (opType == GroovyTokenTypes.mASSIGN) {
if (!processSet || (checkSetterIsSimple(field, setter) && checkFieldIsInaccessible(field, expr)))
return true;
final GrExpression setterArgument = assignment.getRValue();
GrMethodCallExpression methodCall = createSetterCall(fieldDescriptor, setterArgument, expr, descriptor.getTargetClass(), setter);
if (methodCall != null) {
tryToSimplify((GrMethodCallExpression) assignment.replaceWithExpression(methodCall, true));
}
//TODO: check if value is used!!!
} else {
// Q: side effects of qualifier??!
if (checkAccessorsAreSimpleAndFieldIsInaccessible(field, setter, getter, expr)) {
return true;
}
String opName = opSign.getText();
LOG.assertTrue(StringUtil.endsWithChar(opName, '='));
opName = opName.substring(0, opName.length() - 1);
GrExpression getExpr = expr;
if (processGet) {
final GrExpression getterCall = createGetterCall(fieldDescriptor, expr, descriptor.getTargetClass(), getter);
if (getterCall != null) {
getExpr = getterCall;
}
}
@NonNls String text = "a" + opName + "b";
GrBinaryExpression binExpr = (GrBinaryExpression) factory.createExpressionFromText(text, expr);
tryToSimplify((GrMethodCallExpression) binExpr.getLeftOperand().replaceWithExpression(getExpr, true));
binExpr.getRightOperand().replaceWithExpression(assignment.getRValue(), true);
GrExpression setExpr;
if (processSet) {
setExpr = createSetterCall(fieldDescriptor, binExpr, expr, descriptor.getTargetClass(), setter);
} else {
text = "a = b";
GrAssignmentExpression newAssignment = (GrAssignmentExpression) factory.createExpressionFromText(text, null);
newAssignment.getLValue().replaceWithExpression(expr, true);
newAssignment.getRValue().replaceWithExpression(binExpr, true);
setExpr = newAssignment;
}
tryToSimplify((GrMethodCallExpression) assignment.replaceWithExpression(setExpr, true));
//TODO: check if value is used!!!
}
}
} else if (parent instanceof GrUnaryExpression && (((GrUnaryExpression) parent).getOperationTokenType() == GroovyTokenTypes.mINC || ((GrUnaryExpression) parent).getOperationTokenType() == GroovyTokenTypes.mDEC)) {
if (checkAccessorsAreSimpleAndFieldIsInaccessible(field, setter, getter, expr)) {
return true;
}
IElementType sign = ((GrUnaryExpression) parent).getOperationTokenType();
GrExpression getExpr = expr;
if (processGet) {
final GrExpression getterCall = createGetterCall(fieldDescriptor, expr, descriptor.getTargetClass(), getter);
if (getterCall != null) {
getExpr = getterCall;
}
}
@NonNls String text = sign == GroovyTokenTypes.mINC ? "a+1" : "a-1";
GrBinaryExpression binExpr = (GrBinaryExpression) factory.createExpressionFromText(text, parent);
tryToSimplify((GrMethodCallExpression) binExpr.getLeftOperand().replaceWithExpression(getExpr, true));
GrExpression setExpr;
if (processSet) {
setExpr = createSetterCall(fieldDescriptor, binExpr, expr, descriptor.getTargetClass(), setter);
} else {
text = "a = b";
GrAssignmentExpression assignment = (GrAssignmentExpression) factory.createExpressionFromText(text, null);
assignment.getLValue().replaceWithExpression(expr, true);
assignment.getRValue().replaceWithExpression(binExpr, true);
setExpr = assignment;
}
tryToSimplify((GrMethodCallExpression) ((GrUnaryExpression) parent).replaceWithExpression(setExpr, true));
} else {
if (!processGet || (checkGetterIsSimple(field, getter) && checkFieldIsInaccessible(field, expr)))
return true;
GrExpression methodCall = createGetterCall(fieldDescriptor, expr, descriptor.getTargetClass(), getter);
if (methodCall != null) {
tryToSimplify(((GrMethodCallExpression) expr.replaceWithExpression(methodCall, true)));
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class GrUnresolvableLocalCollisionDetector method visitUpstreamCollisions.
private static void visitUpstreamCollisions(PsiElement element, String newName, GroovyPsiElement place, CollidingVariableVisitor visitor) {
final GrReferenceExpression refExpr = GroovyPsiElementFactory.getInstance(place.getProject()).createReferenceExpressionFromText(newName, place);
final GroovyResolveResult[] results = refExpr.multiResolve(false);
for (GroovyResolveResult result : results) {
final PsiElement resolved = result.getElement();
if (resolved instanceof GrParameter || (resolved instanceof GrVariable && !(resolved instanceof GrField))) {
final PsiElement parent = PsiTreeUtil.findCommonParent(resolved, element);
if (parent != null) {
PsiElement current = element;
while (current != null && current != parent) {
if (current instanceof PsiMethod || current instanceof PsiClass || current instanceof GrClosableBlock) {
return;
}
current = current.getParent();
}
}
if (!place.getManager().areElementsEquivalent(element, resolved)) {
visitor.visitCollidingVariable((PsiVariable) resolved);
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ExpressionGenerator method visitNewExpression.
@Override
public void visitNewExpression(@NotNull GrNewExpression newExpression) {
boolean hasFieldInitialization = hasFieldInitialization(newExpression);
StringBuilder builder;
final PsiType type = newExpression.getType();
final String varName;
if (hasFieldInitialization) {
builder = new StringBuilder();
varName = GenerationUtil.suggestVarName(type, newExpression, context);
TypeWriter.writeType(builder, type, newExpression);
builder.append(' ').append(varName).append(" = ");
} else {
varName = null;
builder = this.builder;
}
final GrTypeElement typeElement = newExpression.getTypeElement();
final GrArrayDeclaration arrayDeclaration = newExpression.getArrayDeclaration();
final GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
builder.append("new ");
if (typeElement != null) {
final PsiType builtIn = typeElement.getType();
LOG.assertTrue(builtIn instanceof PsiPrimitiveType);
final PsiType boxed = TypesUtil.boxPrimitiveType(builtIn, newExpression.getManager(), newExpression.getResolveScope());
TypeWriter.writeTypeForNew(builder, boxed, newExpression);
} else if (referenceElement != null) {
GenerationUtil.writeCodeReferenceElement(builder, referenceElement);
}
final GrArgumentList argList = newExpression.getArgumentList();
if (argList != null) {
GrClosureSignature signature = null;
final GroovyResolveResult resolveResult = newExpression.advancedResolve();
final PsiElement constructor = resolveResult.getElement();
if (constructor instanceof PsiMethod) {
signature = GrClosureSignatureUtil.createSignature((PsiMethod) constructor, resolveResult.getSubstitutor());
} else if (referenceElement != null) {
final GroovyResolveResult clazzResult = referenceElement.advancedResolve();
final PsiElement clazz = clazzResult.getElement();
if (clazz instanceof PsiClass && ((PsiClass) clazz).getConstructors().length == 0) {
signature = GrClosureSignatureUtil.createSignature(PsiParameter.EMPTY_ARRAY, null);
}
}
final GrNamedArgument[] namedArgs = hasFieldInitialization ? GrNamedArgument.EMPTY_ARRAY : argList.getNamedArguments();
new ArgumentListGenerator(builder, context).generate(signature, argList.getExpressionArguments(), namedArgs, GrClosableBlock.EMPTY_ARRAY, newExpression);
}
final GrAnonymousClassDefinition anonymous = newExpression.getAnonymousClassDefinition();
if (anonymous != null) {
writeTypeBody(builder, anonymous);
}
if (arrayDeclaration != null) {
final GrExpression[] boundExpressions = arrayDeclaration.getBoundExpressions();
for (GrExpression boundExpression : boundExpressions) {
builder.append('[');
boundExpression.accept(this);
builder.append(']');
}
if (boundExpressions.length == 0) {
builder.append("[]");
}
}
if (hasFieldInitialization) {
builder.append(';');
context.myStatements.add(builder.toString());
final GrNamedArgument[] namedArguments = argList.getNamedArguments();
for (GrNamedArgument namedArgument : namedArguments) {
final String fieldName = namedArgument.getLabelName();
if (fieldName == null) {
//todo try to initialize field
final GrArgumentLabel label = namedArgument.getLabel();
LOG.info("cannot initialize field " + (label == null ? "<null>" : label.getText()));
} else {
final GroovyResolveResult resolveResult = referenceElement.advancedResolve();
final PsiElement resolved = resolveResult.getElement();
LOG.assertTrue(resolved instanceof PsiClass);
initializeField(varName, type, ((PsiClass) resolved), resolveResult.getSubstitutor(), fieldName, namedArgument.getExpression());
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult in project intellij-community by JetBrains.
the class ClassItemGeneratorImpl method writeEnumConstant.
@Override
public void writeEnumConstant(StringBuilder builder, GrEnumConstant constant) {
GenerationUtil.writeDocComment(builder, constant, false);
builder.append(constant.getName());
final GrArgumentList argumentList = constant.getArgumentList();
if (argumentList != null) {
final GroovyResolveResult resolveResult = constant.advancedResolve();
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(resolveResult);
new ArgumentListGenerator(builder, context.extend()).generate(signature, argumentList.getExpressionArguments(), argumentList.getNamedArguments(), GrClosableBlock.EMPTY_ARRAY, constant);
}
final GrEnumConstantInitializer anonymousBlock = constant.getInitializingClass();
if (anonymousBlock != null) {
builder.append("{\n");
new ClassGenerator(classNameProvider, this).writeMembers(builder, anonymousBlock);
builder.append("\n}");
}
}
Aggregations