use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField 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;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GrTypeDefinitionImpl method add.
@Override
public PsiElement add(@NotNull PsiElement psiElement) throws IncorrectOperationException {
final GrTypeDefinitionBody body = getBody();
if (body == null)
throw new IncorrectOperationException("Class must have body");
final PsiElement lBrace = body.getLBrace();
if (lBrace == null)
throw new IncorrectOperationException("No left brace");
PsiMember member = getAnyMember(psiElement);
PsiElement anchor = member != null ? getDefaultAnchor(body, member) : null;
if (anchor == null) {
anchor = lBrace.getNextSibling();
}
if (anchor != null) {
ASTNode node = anchor.getNode();
assert node != null;
if (GroovyTokenTypes.mSEMI.equals(node.getElementType())) {
anchor = anchor.getNextSibling();
}
if (psiElement instanceof GrField) {
//add field with modifiers which are in its parent
int i = ArrayUtilRt.find(((GrVariableDeclaration) psiElement.getParent()).getVariables(), psiElement);
psiElement = body.addBefore(psiElement.getParent(), anchor);
GrVariable[] vars = ((GrVariableDeclaration) psiElement).getVariables();
for (int j = 0; j < vars.length; j++) {
if (i != j)
vars[i].delete();
}
psiElement = vars[i];
} else {
psiElement = body.addBefore(psiElement, anchor);
}
} else {
psiElement = body.add(psiElement);
}
return psiElement;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GroovyPropertyUtils method generateSetterPrototype.
public static GrMethod generateSetterPrototype(PsiField field) {
Project project = field.getProject();
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
String name = field.getName();
boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
VariableKind kind = codeStyleManager.getVariableKind(field);
String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
String setName = getSetterName(field.getName());
final PsiClass containingClass = field.getContainingClass();
try {
GrMethod setMethod = factory.createMethod(setName, PsiType.VOID);
String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
final PsiType type = field instanceof GrField ? ((GrField) field).getDeclaredType() : field.getType();
GrParameter param = factory.createParameter(parameterName, type);
annotateWithNullableStuff(field, param);
setMethod.getParameterList().add(param);
PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
@NonNls StringBuilder builder = new StringBuilder();
if (name.equals(parameterName)) {
if (!isStatic) {
builder.append("this.");
} else {
String className = containingClass.getName();
if (className != null) {
builder.append(className);
builder.append(".");
}
}
}
builder.append(name);
builder.append("=");
builder.append(parameterName);
builder.append("\n");
GrCodeBlock body = factory.createMethodBodyFromText(builder.toString());
setMethod.getBlock().replace(body);
return setMethod;
} catch (IncorrectOperationException e) {
LOG.error(e);
return null;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class CollectClassMembersUtil method processClass.
private static void processClass(@NotNull PsiClass aClass, @NotNull Map<String, CandidateInfo> allFields, @NotNull Map<String, List<CandidateInfo>> allMethods, @NotNull Map<String, CandidateInfo> allInnerClasses, @NotNull Set<PsiClass> visitedClasses, @NotNull PsiSubstitutor substitutor, boolean includeSynthetic) {
PsiUtilCore.ensureValid(aClass);
if (!visitedClasses.add(aClass))
return;
if (visitedClasses.size() == 1 || !GrTraitUtil.isTrait(aClass)) {
for (PsiField field : getFields(aClass, includeSynthetic)) {
String name = field.getName();
if (!allFields.containsKey(name)) {
allFields.put(name, new CandidateInfo(field, substitutor));
} else if (hasExplicitVisibilityModifiers(field)) {
final CandidateInfo candidateInfo = allFields.get(name);
final PsiElement element = candidateInfo.getElement();
if (element instanceof GrField) {
final GrModifierList modifierList = ((GrField) element).getModifierList();
if ((modifierList == null || !modifierList.hasExplicitVisibilityModifiers()) && aClass == ((GrField) element).getContainingClass()) {
//replace property-field with field with explicit visibilityModifier
allFields.put(name, new CandidateInfo(field, substitutor));
}
}
}
}
}
for (PsiMethod method : getMethods(aClass, includeSynthetic)) {
addMethod(allMethods, method, substitutor);
}
for (final PsiClass inner : getInnerClasses(aClass, includeSynthetic)) {
final String name = inner.getName();
if (name != null && !allInnerClasses.containsKey(name)) {
allInnerClasses.put(name, new CandidateInfo(inner, substitutor));
}
}
for (PsiClass superClass : getSupers(aClass, includeSynthetic)) {
final PsiSubstitutor superSubstitutor = TypeConversionUtil.getSuperClassSubstitutor(superClass, aClass, substitutor);
processClass(superClass, allFields, allMethods, allInnerClasses, visitedClasses, superSubstitutor, includeSynthetic);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class SimpleBuilderStrategySupport method applyTransformation.
@Override
public void applyTransformation(@NotNull TransformationContext context) {
GrTypeDefinition typeDefinition = context.getCodeClass();
final PsiAnnotation annotation = PsiImplUtil.getAnnotation(typeDefinition, BUILDER_FQN);
if (!isApplicable(annotation, SIMPLE_STRATEGY_NAME))
return;
for (GrField field : typeDefinition.getCodeFields()) {
context.addMethod(createFieldSetter(typeDefinition, field, annotation));
}
}
Aggregations