use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method run.
@Nullable
public GrVariable run() {
PsiElement scope = myContext.getScope();
final PsiClass targetClass = scope instanceof GroovyFileBase ? ((GroovyFileBase) scope).getScriptClass() : (PsiClass) scope;
if (targetClass == null)
return null;
final GrVariableDeclaration declaration = insertField(targetClass);
final GrVariable field = declaration.getVariables()[0];
if (mySettings.removeLocalVar()) {
myLocalVariable = GrIntroduceHandlerBase.resolveLocalVar(myContext);
assert myLocalVariable != null : myContext.getExpression() + ", " + myContext.getVar() + ", " + myContext.getStringPart();
}
GrExpression originalInitializer = getInitializer();
myInitializer = originalInitializer == null ? null : (GrExpression) originalInitializer.copy();
List<PsiElement> replaced = processOccurrences(targetClass, field);
switch(mySettings.initializeIn()) {
case CUR_METHOD:
initializeInMethod(field, replaced);
break;
case FIELD_DECLARATION:
field.setInitializerGroovy(myInitializer);
break;
case CONSTRUCTOR:
initializeInConstructor(field, replaced);
break;
case SETUP_METHOD:
initializeInSetup(field, replaced);
break;
}
JavaCodeStyleManager.getInstance(declaration.getProject()).shortenClassReferences(declaration);
if (mySettings.removeLocalVar()) {
GrIntroduceHandlerBase.deleteLocalVar(myLocalVariable);
}
return field;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class FieldConflictsResolver method fixInitializer.
public GrExpression fixInitializer(GrExpression initializer) {
if (myField == null)
return initializer;
final GrReferenceExpression[] replacedRef = { null };
initializer.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
final GrExpression qualifierExpression = expression.getQualifier();
if (qualifierExpression != null) {
qualifierExpression.accept(this);
} else {
final PsiElement result = expression.resolve();
if (expression.getManager().areElementsEquivalent(result, myField)) {
try {
replacedRef[0] = qualifyReference(expression, myField, myQualifyingClass);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
}
});
if (!initializer.isValid())
return replacedRef[0];
return initializer;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrInplaceParameterIntroducer method createExpressionWrapper.
@NotNull
private static GrExpressionWrapper createExpressionWrapper(@NotNull GrIntroduceContext context) {
GrExpression expression = context.getExpression();
GrVariable var = context.getVar();
assert expression != null || var != null;
GrExpression initializer = expression != null ? expression : var.getInitializerGroovy();
return new GrExpressionWrapper(initializer);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrIntroduceConstantProcessor method run.
@Nullable
public GrField run() {
final PsiClass targetClass = settings.getTargetClass();
if (targetClass == null)
return null;
if (checkErrors(targetClass)) {
return null;
}
final GrVariableDeclaration declaration = addDeclaration(targetClass);
final GrField field = (GrField) declaration.getVariables()[0];
GrVariable localVar = GrIntroduceHandlerBase.resolveLocalVar(context);
if (localVar != null) {
assert localVar.getInitializerGroovy() != null : "initializer should exist: " + localVar.getText();
GrIntroduceHandlerBase.deleteLocalVar(localVar);
if (settings.replaceAllOccurrences()) {
processOccurrences(field);
} else {
replaceOccurrence(field, localVar.getInitializerGroovy(), isEscalateVisibility());
}
} else if (context.getStringPart() != null) {
final GrExpression ref = context.getStringPart().replaceLiteralWithConcatenation(field.getName());
final PsiElement element = replaceOccurrence(field, ref, isEscalateVisibility());
updateCaretPosition(element);
} else if (context.getExpression() != null) {
if (settings.replaceAllOccurrences()) {
processOccurrences(field);
} else {
replaceOccurrence(field, context.getExpression(), isEscalateVisibility());
}
}
return field;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrIntroduceConstantProcessor method getInitializer.
@NotNull
protected GrExpression getInitializer() {
GrVariable var = GrIntroduceHandlerBase.resolveLocalVar(context);
GrExpression expression = context.getExpression();
if (var != null) {
return var.getInitializerGroovy();
} else if (expression != null) {
return expression;
} else {
return context.getStringPart().createLiteralFromSelected();
}
}
Aggregations