use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class DelegateTransformationSupport method applyTransformation.
@Override
public void applyTransformation(@NotNull TransformationContext context) {
for (GrField field : context.getFields()) {
final PsiAnnotation annotation = PsiImplUtil.getAnnotation(field, GroovyCommonClassNames.GROOVY_LANG_DELEGATE);
if (annotation == null)
continue;
final PsiType type = field.getDeclaredType();
if (!(type instanceof PsiClassType))
continue;
final PsiClassType.ClassResolveResult delegateResult = ((PsiClassType) type).resolveGenerics();
final PsiClass delegate = delegateResult.getElement();
if (delegate == null)
continue;
DelegateProcessor processor = new DelegateProcessor(context, delegate, annotation);
delegate.processDeclarations(processor, ResolveState.initial().put(PsiSubstitutor.KEY, delegateResult.getSubstitutor()), null, context.getCodeClass());
if (!processor.myInterfaces)
continue;
Set<PsiClass> visited = ContainerUtil.newHashSet();
Queue<Pair<PsiClass, PsiSubstitutor>> queue = ContainerUtil.newLinkedList(Pair.create(delegate, delegateResult.getSubstitutor()));
while (!queue.isEmpty()) {
Pair<PsiClass, PsiSubstitutor> pair = queue.poll();
PsiClass currentClass = pair.first;
PsiSubstitutor substitutor = pair.second;
if (visited.add(currentClass) && currentClass.isInterface()) {
context.addInterface(new PsiImmediateClassType(currentClass, substitutor));
continue;
}
for (PsiClassType superType : currentClass.getSuperTypes()) {
PsiClassType.ClassResolveResult resolveResult = superType.resolveGenerics();
PsiClass superClass = resolveResult.getElement();
if (superClass != null) {
queue.offer(Pair.create(superClass, TypeConversionUtil.getSuperClassSubstitutor(superClass, currentClass, substitutor)));
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GroovyPostHighlightingPass method doCollectInformation.
@Override
public void doCollectInformation(@NotNull final ProgressIndicator progress) {
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
VirtualFile virtualFile = myFile.getViewProvider().getVirtualFile();
if (!fileIndex.isInContent(virtualFile)) {
return;
}
final InspectionProfile profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
final HighlightDisplayKey unusedDefKey = HighlightDisplayKey.find(GroovyUnusedDeclarationInspection.SHORT_NAME);
final boolean deadCodeEnabled = profile.isToolEnabled(unusedDefKey, myFile);
final UnusedDeclarationInspectionBase deadCodeInspection = (UnusedDeclarationInspectionBase) profile.getUnwrappedTool(UnusedDeclarationInspectionBase.SHORT_NAME, myFile);
final GlobalUsageHelper usageHelper = new GlobalUsageHelper() {
@Override
public boolean isCurrentFileAlreadyChecked() {
return false;
}
@Override
public boolean isLocallyUsed(@NotNull PsiNamedElement member) {
return false;
}
@Override
public boolean shouldCheckUsages(@NotNull PsiMember member) {
return deadCodeInspection == null || !deadCodeInspection.isEntryPoint(member);
}
};
final List<HighlightInfo> unusedDeclarations = new ArrayList<>();
final Map<GrParameter, Boolean> usedParams = new HashMap<>();
myFile.accept(new PsiRecursiveElementWalkingVisitor() {
@Override
public void visitElement(PsiElement element) {
if (element instanceof GrReferenceExpression && !((GrReferenceElement) element).isQualified()) {
GroovyResolveResult[] results = ((GrReferenceExpression) element).multiResolve(false);
if (results.length == 0) {
results = ((GrReferenceExpression) element).multiResolve(true);
}
for (GroovyResolveResult result : results) {
PsiElement resolved = result.getElement();
if (resolved instanceof GrParameter && resolved.getContainingFile() == myFile) {
usedParams.put((GrParameter) resolved, Boolean.TRUE);
}
}
}
if (deadCodeEnabled && element instanceof GrNamedElement && element instanceof PsiModifierListOwner && !UnusedSymbolUtil.isImplicitUsage(element.getProject(), (PsiModifierListOwner) element, progress) && !GroovySuppressableInspectionTool.isElementToolSuppressedIn(element, GroovyUnusedDeclarationInspection.SHORT_NAME)) {
PsiElement nameId = ((GrNamedElement) element).getNameIdentifierGroovy();
if (nameId.getNode().getElementType() == GroovyTokenTypes.mIDENT) {
String name = ((GrNamedElement) element).getName();
if (element instanceof GrTypeDefinition && !UnusedSymbolUtil.isClassUsed(myProject, element.getContainingFile(), (GrTypeDefinition) element, progress, usageHelper)) {
HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, "Class " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL);
QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(element), unusedDefKey);
ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
} else if (element instanceof GrMethod) {
GrMethod method = (GrMethod) element;
if (!UnusedSymbolUtil.isMethodReferenced(method.getProject(), method.getContainingFile(), method, progress, usageHelper)) {
String message = (method.isConstructor() ? "Constructor" : "Method") + " " + name + " is unused";
HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, message, HighlightInfoType.UNUSED_SYMBOL);
QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(method), unusedDefKey);
ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
}
} else if (element instanceof GrField && isFieldUnused((GrField) element, progress, usageHelper)) {
HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(nameId, "Property " + name + " is unused", HighlightInfoType.UNUSED_SYMBOL);
QuickFixAction.registerQuickFixAction(highlightInfo, QuickFixFactory.getInstance().createSafeDeleteFix(element), unusedDefKey);
ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
} else if (element instanceof GrParameter) {
if (!usedParams.containsKey(element)) {
usedParams.put((GrParameter) element, Boolean.FALSE);
}
}
}
}
super.visitElement(element);
}
});
final Set<GrImportStatement> unusedImports = new HashSet<>(PsiUtil.getValidImportStatements(myFile));
unusedImports.removeAll(GroovyImportUtil.findUsedImports(myFile));
myUnusedImports = unusedImports;
if (deadCodeEnabled) {
for (GrParameter parameter : usedParams.keySet()) {
if (usedParams.get(parameter))
continue;
PsiElement scope = parameter.getDeclarationScope();
if (scope instanceof GrMethod) {
GrMethod method = (GrMethod) scope;
if (methodMayHaveUnusedParameters(method)) {
PsiElement identifier = parameter.getNameIdentifierGroovy();
HighlightInfo highlightInfo = UnusedSymbolUtil.createUnusedSymbolInfo(identifier, "Parameter " + parameter.getName() + " is unused", HighlightInfoType.UNUSED_SYMBOL);
QuickFixAction.registerQuickFixAction(highlightInfo, GroovyQuickFixFactory.getInstance().createRemoveUnusedGrParameterFix(parameter), unusedDefKey);
ContainerUtil.addIfNotNull(unusedDeclarations, highlightInfo);
}
} else if (scope instanceof GrClosableBlock) {
//todo Max Medvedev
}
}
}
myUnusedDeclarations = unusedDeclarations;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GroovyGenerateConstructorHandler method chooseOriginalMembers.
@Override
@Nullable
protected ClassMember[] chooseOriginalMembers(PsiClass aClass, Project project) {
final ClassMember[] classMembers = chooseOriginalMembersImpl(aClass, project);
if (classMembers == null)
return null;
List<ClassMember> res = new ArrayList<>();
final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
for (ClassMember classMember : classMembers) {
if (classMember instanceof PsiMethodMember) {
final PsiMethod method = ((PsiMethodMember) classMember).getElement();
PsiMethod copy = factory.createMethodFromText(GroovyToJavaGenerator.generateMethodStub(method), method);
if (method instanceof GrMethod) {
GrParameter[] parameters = ((GrMethod) method).getParameterList().getParameters();
PsiParameter[] copyParameters = copy.getParameterList().getParameters();
for (int i = 0; i < parameters.length; i++) {
if (parameters[i].getTypeElementGroovy() == null) {
copyParameters[i].setName(DEF_PSEUDO_ANNO + parameters[i].getName());
}
}
}
res.add(new PsiMethodMember(copy));
} else if (classMember instanceof PsiFieldMember) {
final PsiField field = ((PsiFieldMember) classMember).getElement();
String prefix = field instanceof GrField && ((GrField) field).getTypeElementGroovy() == null ? DEF_PSEUDO_ANNO : "";
res.add(new PsiFieldMember(factory.createFieldFromText(field.getType().getCanonicalText() + " " + prefix + field.getName(), aClass)));
}
}
return res.toArray(new ClassMember[res.size()]);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class DefaultTransformationSupport method applyTransformation.
@Override
public void applyTransformation(@NotNull TransformationContext context) {
for (GrField field : context.getFields()) {
if (!field.isProperty())
continue;
final String fieldName = field.getName();
String nameNonBoolean = getGetterNameNonBoolean(fieldName);
if (!hasContradictingMethods(context, nameNonBoolean, false)) {
context.addMethod(new GrAccessorMethodImpl(field, false, nameNonBoolean));
if (PsiType.BOOLEAN.equals(field.getDeclaredType())) {
String nameBoolean = getGetterNameBoolean(fieldName);
if (!hasContradictingMethods(context, nameBoolean, false)) {
context.addMethod(new GrAccessorMethodImpl(field, false, nameBoolean));
}
}
}
if (!field.hasModifierProperty(PsiModifier.FINAL)) {
String setterName = getSetterName(fieldName);
if (!hasContradictingMethods(context, setterName, true)) {
context.addMethod(new GrAccessorMethodImpl(field, true, setterName));
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class ResolverProcessor method isAccessible.
protected boolean isAccessible(@NotNull PsiNamedElement namedElement) {
if (namedElement instanceof GrField) {
final GrField field = (GrField) namedElement;
if (PsiUtil.isAccessible(myPlace, field)) {
return true;
}
for (GrAccessorMethod method : field.getGetters()) {
if (PsiUtil.isAccessible(myPlace, method)) {
return true;
}
}
final GrAccessorMethod setter = field.getSetter();
if (setter != null && PsiUtil.isAccessible(myPlace, setter)) {
return true;
}
return false;
}
return !(namedElement instanceof PsiMember) || PsiUtil.isAccessible(myPlace, ((PsiMember) namedElement));
}
Aggregations