use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GroovyLineMarkerProvider method collectOverridingMethods.
private static void collectOverridingMethods(@NotNull final Set<PsiMethod> methods, @NotNull Collection<LineMarkerInfo> result) {
final Set<PsiElement> overridden = new HashSet<>();
Set<PsiClass> classes = new THashSet<>();
for (PsiMethod method : methods) {
ProgressManager.checkCanceled();
final PsiClass parentClass = method.getContainingClass();
if (parentClass != null && !CommonClassNames.JAVA_LANG_OBJECT.equals(parentClass.getQualifiedName())) {
classes.add(parentClass);
}
}
for (final PsiClass aClass : classes) {
AllOverridingMethodsSearch.search(aClass).forEach(pair -> {
ProgressManager.checkCanceled();
final PsiMethod superMethod = pair.getFirst();
if (isCorrectTarget(superMethod) && isCorrectTarget(pair.getSecond())) {
if (methods.remove(superMethod)) {
overridden.add(PsiImplUtil.handleMirror(superMethod));
}
}
return !methods.isEmpty();
});
}
for (PsiElement element : overridden) {
final Icon icon = AllIcons.Gutter.OverridenMethod;
element = PsiImplUtil.handleMirror(element);
PsiElement range = element instanceof GrNamedElement ? ((GrNamedElement) element).getNameIdentifierGroovy() : element;
final MarkerType type = element instanceof GrField ? GroovyMarkerTypes.OVERRIDEN_PROPERTY_TYPE : GroovyMarkerTypes.GR_OVERRIDEN_METHOD;
LineMarkerInfo info = new LineMarkerInfo<>(range, range.getTextRange(), icon, Pass.LINE_MARKERS, type.getTooltip(), type.getNavigationHandler(), GutterIconRenderer.Alignment.RIGHT);
result.add(info);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GroovyCreateFieldFromUsageHelper method setupTemplateImpl.
@Override
public Template setupTemplateImpl(PsiField f, Object expectedTypes, PsiClass targetClass, Editor editor, PsiElement context, boolean createConstantField, PsiSubstitutor substitutor) {
GrVariableDeclaration fieldDecl = (GrVariableDeclaration) f.getParent();
GrField field = (GrField) fieldDecl.getVariables()[0];
TemplateBuilderImpl builder = new TemplateBuilderImpl(fieldDecl);
Project project = context.getProject();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
if (expectedTypes instanceof TypeConstraint[]) {
GrTypeElement typeElement = fieldDecl.getTypeElementGroovy();
assert typeElement != null;
ChooseTypeExpression expr = new ChooseTypeExpression((TypeConstraint[]) expectedTypes, PsiManager.getInstance(project), typeElement.getResolveScope());
builder.replaceElement(typeElement, expr);
} else if (expectedTypes instanceof ExpectedTypeInfo[]) {
new GuessTypeParameters(factory).setupTypeElement(field.getTypeElement(), (ExpectedTypeInfo[]) expectedTypes, substitutor, builder, context, targetClass);
}
if (createConstantField) {
field.setInitializerGroovy(factory.createExpressionFromText("0", null));
builder.replaceElement(field.getInitializerGroovy(), new EmptyExpression());
}
fieldDecl = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(fieldDecl);
Template template = builder.buildTemplate();
TextRange range = fieldDecl.getTextRange();
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
if (expectedTypes instanceof ExpectedTypeInfo[]) {
if (((ExpectedTypeInfo[]) expectedTypes).length > 1)
template.setToShortenLongNames(false);
}
return template;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField 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.statements.GrField in project intellij-community by JetBrains.
the class GrMemberInfo method extractClassMembers.
public static void extractClassMembers(PsiClass subclass, List<GrMemberInfo> result, Filter<GrMember> filter, final boolean extractInterfacesDeep) {
if (!(subclass instanceof GrTypeDefinition))
return;
if (extractInterfacesDeep) {
extractSuperInterfaces(subclass, filter, result, ContainerUtil.<PsiClass>newHashSet());
} else {
PsiClass[] interfaces = subclass.getInterfaces();
GrReferenceList sourceRefList = subclass.isInterface() ? ((GrTypeDefinition) subclass).getExtendsClause() : ((GrTypeDefinition) subclass).getImplementsClause();
for (PsiClass anInterface : interfaces) {
if (anInterface instanceof GrTypeDefinition && filter.includeMember((GrMember) anInterface)) {
result.add(new GrMemberInfo((GrMember) anInterface, true, sourceRefList));
}
}
}
PsiClass[] innerClasses = subclass.getInnerClasses();
for (PsiClass innerClass : innerClasses) {
if (innerClass instanceof GrTypeDefinition && filter.includeMember((GrMember) innerClass)) {
result.add(new GrMemberInfo((GrMember) innerClass));
}
}
GrMethod[] methods = ((GrTypeDefinition) subclass).getCodeMethods();
for (GrMethod method : methods) {
if (!method.isConstructor() && filter.includeMember(method)) {
result.add(new GrMemberInfo(method));
}
}
GrField[] fields = ((GrTypeDefinition) subclass).getCodeFields();
for (final GrField field : fields) {
if (filter.includeMember(field)) {
result.add(new GrMemberInfo(field));
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class ExpressionGenerator method createVarByInitializer.
private String createVarByInitializer(@NotNull GrExpression initializer) {
GrExpression inner = initializer;
while (inner instanceof GrParenthesizedExpression) inner = ((GrParenthesizedExpression) inner).getOperand();
if (inner != null)
initializer = inner;
if (initializer instanceof GrReferenceExpression) {
final GrExpression qualifier = ((GrReferenceExpression) initializer).getQualifier();
if (qualifier == null) {
final PsiElement resolved = ((GrReferenceExpression) initializer).resolve();
if (resolved instanceof GrVariable && !(resolved instanceof GrField)) {
//don't create new var. it is already exists
return ((GrVariable) resolved).getName();
}
}
}
final String name = GenerationUtil.suggestVarName(initializer, context);
final StringBuilder builder = new StringBuilder();
builder.append("final ");
TypeWriter.writeType(builder, initializer.getType(), initializer);
builder.append(' ').append(name).append(" = ");
initializer.accept(new ExpressionGenerator(builder, context));
builder.append(';');
context.myStatements.add(builder.toString());
return name;
}
Aggregations