use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant in project intellij-community by JetBrains.
the class MoveGroovyMemberHandler method doMove.
@Override
@NotNull
public PsiMember doMove(@NotNull MoveMembersOptions options, @NotNull PsiMember member, PsiElement anchor, @NotNull PsiClass targetClass) {
GroovyChangeContextUtil.encodeContextInfo(member);
final PsiDocComment docComment;
if (member instanceof PsiDocCommentOwner) {
docComment = ((PsiDocCommentOwner) member).getDocComment();
} else {
docComment = null;
}
PsiMember moved;
if (options.makeEnumConstant() && member instanceof GrVariable && EnumConstantsUtil.isSuitableForEnumConstant(((PsiVariable) member).getType(), targetClass)) {
final GrEnumConstant prototype = createEnumConstant(member.getName(), ((GrVariable) member).getInitializerGroovy(), member.getProject());
moved = (PsiMember) addEnumConstant(targetClass, prototype, anchor);
member.delete();
} else if (member instanceof GrEnumConstant) {
moved = (PsiMember) addEnumConstant(targetClass, (GrEnumConstant) member, null);
} else if (member instanceof GrField) {
if (anchor != null)
anchor = anchor.getParent();
final GrVariableDeclaration parent = (GrVariableDeclaration) member.getParent();
GrVariableDeclaration movedDeclaration = (GrVariableDeclaration) targetClass.addAfter(parent, anchor);
int number = ArrayUtil.find(parent.getMembers(), member);
final GrMember[] members = movedDeclaration.getMembers();
for (int i = 0; i < number; i++) {
members[i].delete();
}
for (int i = number + 1; i < members.length; i++) {
members[i].delete();
}
if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
//might need to make modifiers explicit, see IDEADEV-11416
final PsiModifierList list = movedDeclaration.getModifierList();
VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
}
moved = movedDeclaration.getMembers()[0];
} else if (member instanceof GrMethod) {
moved = (PsiMember) targetClass.addAfter(member, anchor);
if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
//might need to make modifiers explicit, see IDEADEV-11416
final PsiModifierList list = moved.getModifierList();
assert list != null;
list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
}
} else {
moved = (PsiMember) targetClass.addAfter(member, anchor);
}
if (docComment != null) {
PsiElement insertedDocComment = targetClass.addBefore(docComment, moved);
PsiElement prevSibling = insertedDocComment.getPrevSibling();
addLineFeedIfNeeded(prevSibling);
docComment.delete();
}
member.delete();
return moved;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant in project intellij-community by JetBrains.
the class GrEnumConstantInfo method getElementToHighlight.
@NotNull
@Override
public PsiElement getElementToHighlight() {
GrEnumConstant constant = getCall();
GrArgumentList argList = constant.getArgumentList();
if (argList != null)
return argList;
return constant.getNameIdentifierGroovy();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant in project intellij-community by JetBrains.
the class GroovyConstructorUsagesSearcher method processConstructorUsages.
static void processConstructorUsages(final PsiMethod constructor, final SearchScope searchScope, final Processor<PsiReference> consumer, final SearchRequestCollector collector, final boolean includeOverloads) {
if (!constructor.isConstructor())
return;
final PsiClass clazz = constructor.getContainingClass();
if (clazz == null)
return;
SearchScope onlyGroovy = GroovyScopeUtil.restrictScopeToGroovyFiles(searchScope, GroovyScopeUtil.getEffectiveScope(constructor));
Set<PsiClass> processed = collector.getSearchSession().getUserData(LITERALLY_CONSTRUCTED_CLASSES);
if (processed == null) {
collector.getSearchSession().putUserData(LITERALLY_CONSTRUCTED_CLASSES, processed = ContainerUtil.newConcurrentSet());
}
if (!processed.add(clazz))
return;
if (clazz.isEnum() && clazz instanceof GroovyPsiElement) {
for (PsiField field : clazz.getFields()) {
if (field instanceof GrEnumConstant) {
final PsiReference ref = field.getReference();
if (ref != null && ref.isReferenceTo(constructor)) {
if (!consumer.process(ref))
return;
}
}
}
}
final LiteralConstructorSearcher literalProcessor = new LiteralConstructorSearcher(constructor, consumer, includeOverloads);
final Processor<GrNewExpression> newExpressionProcessor = grNewExpression -> {
final PsiMethod resolvedConstructor = grNewExpression.resolveMethod();
if (includeOverloads || constructor.getManager().areElementsEquivalent(resolvedConstructor, constructor)) {
return consumer.process(grNewExpression.getReferenceElement());
}
return true;
};
processGroovyClassUsages(clazz, searchScope, collector, newExpressionProcessor, literalProcessor);
//this()
if (clazz instanceof GrTypeDefinition) {
if (!processConstructors(constructor, consumer, clazz, true)) {
return;
}
}
//super()
DirectClassInheritorsSearch.search(clazz, onlyGroovy).forEach(new ReadActionProcessor<PsiClass>() {
@Override
public boolean processInReadAction(PsiClass inheritor) {
if (inheritor instanceof GrTypeDefinition) {
if (!processConstructors(constructor, consumer, inheritor, false))
return false;
}
return true;
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant in project intellij-community by JetBrains.
the class ClassGenerator method writeMembers.
public void writeMembers(StringBuilder text, PsiClass typeDefinition) {
if (typeDefinition instanceof GrEnumTypeDefinition) {
final GrEnumConstant[] enumConstants = ((GrEnumTypeDefinition) typeDefinition).getEnumConstants();
for (GrEnumConstant constant : enumConstants) {
classItemGenerator.writeEnumConstant(text, constant);
text.append(',');
}
if (enumConstants.length > 0) {
//text.removeFromTheEnd(1).append(";\n");
text.delete(text.length() - 1, text.length());
}
text.append(";\n");
}
writeAllMethods(text, classItemGenerator.collectMethods(typeDefinition), typeDefinition);
if (typeDefinition instanceof GrTypeDefinition) {
for (GrMembersDeclaration declaration : ((GrTypeDefinition) typeDefinition).getMemberDeclarations()) {
if (declaration instanceof GrVariableDeclaration) {
classItemGenerator.writeVariableDeclarations(text, (GrVariableDeclaration) declaration);
}
}
for (PsiClass inner : typeDefinition.getInnerClasses()) {
writeTypeDefinition(text, inner, false, false);
text.append('\n');
}
}
classItemGenerator.writePostponed(text, typeDefinition);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant in project intellij-community by JetBrains.
the class GrFinalVariableAccessInspection method isFieldInitialized.
private static boolean isFieldInitialized(@NotNull GrField field) {
if (field instanceof GrEnumConstant)
return true;
if (field.getInitializerGroovy() != null)
return true;
if (isImmutableField(field))
return true;
final boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
final GrTypeDefinition aClass = ((GrTypeDefinition) field.getContainingClass());
if (aClass == null)
return true;
GrClassInitializer[] initializers = aClass.getInitializers();
for (GrClassInitializer initializer : initializers) {
if (initializer.isStatic() != isStatic)
continue;
final GrOpenBlock block = initializer.getBlock();
final Instruction[] initializerFlow = buildFlowForField(block);
if (VariableInitializationChecker.isVariableDefinitelyInitializedCached(field, block, initializerFlow)) {
return true;
}
}
if (isStatic)
return false;
final GrMethod[] constructors = aClass.getCodeConstructors();
if (constructors.length == 0)
return false;
Set<GrMethod> initializedConstructors = ContainerUtil.newHashSet();
Set<GrMethod> notInitializedConstructors = ContainerUtil.newHashSet();
NEXT_CONSTR: for (GrMethod constructor : constructors) {
if (constructor.getBlock() == null)
return false;
final List<GrMethod> chained = getChainedConstructors(constructor);
NEXT_CHAINED: for (GrMethod method : chained) {
if (initializedConstructors.contains(method)) {
continue NEXT_CONSTR;
} else if (notInitializedConstructors.contains(method)) {
continue NEXT_CHAINED;
}
final GrOpenBlock block = method.getBlock();
assert block != null;
final boolean initialized = VariableInitializationChecker.isVariableDefinitelyInitializedCached(field, block, buildFlowForField(block));
if (initialized) {
initializedConstructors.add(method);
continue NEXT_CONSTR;
} else {
notInitializedConstructors.add(method);
}
}
return false;
}
return true;
}
Aggregations