public static Query<PsiClass> search(final PsiClass aClass, SearchScope scope, boolean includeAnonymous, final boolean checkInheritance) { final Query<PsiClass> raw = INSTANCE.createUniqueResultsQuery(new SearchParameters(aClass, scope, includeAnonymous, checkInheritance)); if (!includeAnonymous) { return new FilteredQuery<PsiClass>(raw, new Condition<PsiClass>() { @Override public boolean value(final PsiClass psiClass) { return !(psiClass instanceof PsiAnonymousClass); } }); } return raw; }
/** * Override in case if your JVMNames slightly different then it can be provided by getJvmSignature method. * * @param stepTarget * @return SmartStepFilter */ @Nullable protected MethodFilter createMethodFilter(SmartStepTarget stepTarget) { if (stepTarget instanceof MethodSmartStepTarget) { final PsiMethod method = ((MethodSmartStepTarget)stepTarget).getMethod(); if (stepTarget.needsBreakpointRequest()) { return Registry.is("debugger.async.smart.step.into") && method.getContainingClass() instanceof PsiAnonymousClass ? new ClassInstanceMethodFilter(method, stepTarget.getCallingExpressionLines()) : new AnonymousClassMethodFilter(method, stepTarget.getCallingExpressionLines()); } else { return new BasicStepMethodFilter(method, stepTarget.getCallingExpressionLines()); } } if (stepTarget instanceof LambdaSmartStepTarget) { final LambdaSmartStepTarget lambdaTarget = (LambdaSmartStepTarget)stepTarget; return new LambdaMethodFilter(lambdaTarget.getLambda(), lambdaTarget.getOrdinal(), stepTarget.getCallingExpressionLines()); } return null; }
@NotNull @Override public Collection<JavaAnonymousClassTreeElement> provideNodes(@NotNull TreeElement node) { if (node instanceof PsiMethodTreeElement || node instanceof PsiFieldTreeElement || node instanceof ClassInitializerTreeElement) { final PsiElement el = ((PsiTreeElementBase)node).getElement(); if (el != null) { for (AnonymousElementProvider provider : Extensions.getExtensions(AnonymousElementProvider.EP_NAME)) { final PsiElement[] elements = provider.getAnonymousElements(el); if (elements.length > 0) { List<JavaAnonymousClassTreeElement> result = new ArrayList<JavaAnonymousClassTreeElement>(elements.length); for (PsiElement element : elements) { result.add(new JavaAnonymousClassTreeElement((PsiAnonymousClass)element, new HashSet<PsiClass>())); } return result; } } } } return Collections.emptyList(); }
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) { int offset = editor.getCaretModel().getOffset(); editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); PsiElement element = file.findElementAt(offset); while (true) { if (element == null || element instanceof PsiFile) { String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.class")); CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.TURN_REFS_TO_SUPER); return; } if (element instanceof PsiClass && !(element instanceof PsiAnonymousClass)) { invoke(project, new PsiElement[]{element}, dataContext); return; } element = element.getParent(); } }
@Override public void doFix(@NotNull final Project project, ProblemDescriptor descriptor) { final PsiElement nameElement = descriptor.getPsiElement(); final PsiAnonymousClass aClass = (PsiAnonymousClass)nameElement.getParent(); final JavaRefactoringActionHandlerFactory factory = JavaRefactoringActionHandlerFactory.getInstance(); final RefactoringActionHandler anonymousToInner = factory.createAnonymousToInnerHandler(); final DataManager dataManager = DataManager.getInstance(); final DataContext dataContext = dataManager.getDataContext(); final Runnable runnable = new Runnable() { @Override public void run() { anonymousToInner.invoke(project, new PsiElement[]{aClass}, dataContext); } }; if (ApplicationManager.getApplication().isUnitTestMode()) { runnable.run(); } else { ApplicationManager.getApplication().invokeLater(runnable, project.getDisposed()); } }
@Override public Collection<JavaAnonymousClassTreeElement> provideNodes(TreeElement node) { if (node instanceof PsiMethodTreeElement || node instanceof PsiFieldTreeElement || node instanceof ClassInitializerTreeElement) { final PsiElement el = ((PsiTreeElementBase)node).getElement(); for (AnonymousElementProvider provider : Extensions.getExtensions(AnonymousElementProvider.EP_NAME)) { final PsiElement[] elements = provider.getAnonymousElements(el); if (elements != null && elements.length > 0) { List<JavaAnonymousClassTreeElement> result = new ArrayList<JavaAnonymousClassTreeElement>(elements.length); for (PsiElement element : elements) { result.add(new JavaAnonymousClassTreeElement((PsiAnonymousClass)element, new HashSet<PsiClass>())); } return result; } } } return Collections.emptyList(); }
@Nullable @Override protected PsiClass getCustomTopLevelParentClass(PsiClass psiClass) { if (!(psiClass instanceof GrTypeDefinition)) return null; PsiClass enclosing = PsiTreeUtil.getParentOfType(psiClass, PsiClass.class, true); while (enclosing != null) { psiClass = enclosing; enclosing = PsiTreeUtil.getParentOfType(enclosing, PsiClass.class, true); } if (psiClass instanceof PsiAnonymousClass) { final PsiFile file = psiClass.getContainingFile(); if (file instanceof GroovyFile) { return ((GroovyFile)file).getScriptClass(); } } return psiClass; }
public boolean validateBaseClassConstructor(@NotNull PsiClass psiClass, @NotNull ProblemBuilder builder) { if (psiClass instanceof PsiAnonymousClass || psiClass.isEnum()) { return true; } PsiClass baseClass = psiClass.getSuperClass(); if (baseClass == null) { return true; } PsiMethod[] constructors = baseClass.getConstructors(); if (constructors.length == 0) { return true; } for (PsiMethod constructor : constructors) { final int parametersCount = constructor.getParameterList().getParametersCount(); if (parametersCount == 0 || parametersCount == 1 && constructor.isVarArgs()) { return true; } } builder.addError("Lombok needs a default constructor in the base class"); return false; }
public static String getLambdaPrefix(@NotNull PsiLambdaExpression lambdaExpression) { PsiMember member = PsiTreeUtil.getParentOfType(lambdaExpression, PsiMethod.class, PsiClass.class, PsiField.class); final String methodPrefix; if(member instanceof PsiMethod) { methodPrefix = member.getContainingClass() instanceof PsiAnonymousClass ? "" : "$" + member.getName(); } else if(member instanceof PsiField && member.getContainingClass() instanceof PsiAnonymousClass) { methodPrefix = ""; } else { //inside class initializer everywhere or field in a named class methodPrefix = "$new"; } return methodPrefix; }
private static boolean isUnusedInAnonymousClass(@NotNull PsiMethod method) { PsiClass containingClass = method.getContainingClass(); if(!(containingClass instanceof PsiAnonymousClass)) { return false; } if(containingClass.getParent() instanceof PsiNewExpression && containingClass.getParent().getParent() instanceof PsiVariable && !method.getHierarchicalMethodSignature().getSuperSignatures() .isEmpty()) { // references outside anonymous class can still resolve to this method, see com.intellij.psi.scope.util.PsiScopesUtil.setupAndRunProcessor() return false; } return MethodReferencesSearch.search(method, new LocalSearchScope(containingClass), false).findFirst() == null; }
private static Set<String> getContainingClassNames(PsiElement position) { Set<String> result = ContainerUtil.newLinkedHashSet(); boolean add = false; while(position != null) { if(position instanceof PsiAnonymousClass) { add = true; } else if(add && position instanceof PsiClass) { ContainerUtil.addIfNotNull(result, ((PsiClass) position).getName()); } position = position.getParent(); } return result; }
public static boolean isAvailableForElement(PsiElement element) { if (Extensions.getExtensions(TestFramework.EXTENSION_NAME).length == 0) return false; if (element == null) return false; PsiClass psiClass = getContainingClass(element); if (psiClass == null) return false; Module srcModule = ModuleUtilCore.findModuleForPsiElement(psiClass); if (srcModule == null) return false; if (psiClass.isAnnotationType() || psiClass instanceof PsiAnonymousClass || PsiTreeUtil.getParentOfType(psiClass, PsiClass.class) != null || // inner isUnderTestSources(psiClass)) { return false; } return true; }
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) { editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE); int offset = editor.getCaretModel().getOffset(); PsiElement element = file.findElementAt(offset); while (true) { if (element == null || element instanceof PsiFile) { String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.class")); CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.INHERITANCE_TO_DELEGATION); return; } if (element instanceof PsiClass && !(element instanceof PsiAnonymousClass)) { invoke(project, new PsiElement[]{element}, dataContext); return; } element = element.getParent(); } }
public void doFix(@NotNull final Project project, ProblemDescriptor descriptor) { final PsiElement nameElement = descriptor.getPsiElement(); final PsiAnonymousClass aClass = (PsiAnonymousClass)nameElement.getParent(); final JavaRefactoringActionHandlerFactory factory = JavaRefactoringActionHandlerFactory.getInstance(); final RefactoringActionHandler anonymousToInner = factory.createAnonymousToInnerHandler(); final DataManager dataManager = DataManager.getInstance(); final DataContext dataContext = dataManager.getDataContext(); final Runnable runnable = new Runnable() { public void run() { anonymousToInner.invoke(project, new PsiElement[]{aClass}, dataContext); } }; if (ApplicationManager.getApplication().isUnitTestMode()) { runnable.run(); } else { ApplicationManager.getApplication().invokeLater(runnable, project.getDisposed()); } }
/** * Override in case if your JVMNames slightly different then it can be provided by getJvmSignature method. * * @param stepTarget * @return SmartStepFilter */ @Nullable protected MethodFilter createMethodFilter(SmartStepTarget stepTarget) { if(stepTarget instanceof MethodSmartStepTarget) { final PsiMethod method = ((MethodSmartStepTarget) stepTarget).getMethod(); if(stepTarget.needsBreakpointRequest()) { return method.getContainingClass() instanceof PsiAnonymousClass ? new ClassInstanceMethodFilter(method, stepTarget.getCallingExpressionLines()) : new AnonymousClassMethodFilter (method, stepTarget.getCallingExpressionLines()); } else { return new BasicStepMethodFilter(method, stepTarget.getCallingExpressionLines()); } } if(stepTarget instanceof LambdaSmartStepTarget) { final LambdaSmartStepTarget lambdaTarget = (LambdaSmartStepTarget) stepTarget; return new LambdaMethodFilter(lambdaTarget.getLambda(), lambdaTarget.getOrdinal(), stepTarget.getCallingExpressionLines()); } return null; }
public static PsiMethod findMethod(PsiElement element) { PsiMethod method = (element instanceof PsiMethod) ? (PsiMethod) element : PsiTreeUtil.getParentOfType(element, PsiMethod.class); if (method != null && method.getContainingClass() instanceof PsiAnonymousClass) { return findMethod(method.getParent()); } return method; }
private static boolean cannotBeOverriden(final PsiMethod method) { final PsiClass parentClass = method.getContainingClass(); return parentClass == null || method.isConstructor() || method.hasModifierProperty(PsiModifier.STATIC) || method.hasModifierProperty(PsiModifier.FINAL) || method.hasModifierProperty(PsiModifier.PRIVATE) || parentClass instanceof PsiAnonymousClass || parentClass.hasModifierProperty(PsiModifier.FINAL); }
public static boolean isRunnableClass(final PsiClass aClass, final boolean mustBePublic, boolean mustNotBeAbstract) { if (aClass instanceof PsiAnonymousClass) return false; if (aClass.isInterface()) return false; if (mustBePublic && !aClass.hasModifierProperty(PsiModifier.PUBLIC)) return false; if (aClass.hasModifierProperty(PsiModifier.PRIVATE)) return false; if (mustNotBeAbstract && aClass.hasModifierProperty(PsiModifier.ABSTRACT)) return false; return aClass.getContainingClass() == null || aClass.hasModifierProperty(PsiModifier.STATIC); }
public static TargetClassFilter createClassFilter(PsiClass psiClass) { if (psiClass instanceof PsiAnonymousClass) { return TargetClassFilter.ALL; } if (PsiUtil.isLocalClass(psiClass)) { return new LocalClassFilter(psiClass.getName()); } final String name = JVMNameUtil.getNonAnonymousClassName(psiClass); return name != null? new FQNameClassFilter(name) : TargetClassFilter.ALL; }
@Override public String getPresentableText() { if (myName != null) return myName; final PsiClass element = getElement(); if (element != null) { myName = JavaAnonymousClassesHelper.getName((PsiAnonymousClass)element); if (myName != null) return myName; } return "Anonymous"; }
@Override public String getLocationString() { if (myBaseName == null) { PsiAnonymousClass anonymousClass = (PsiAnonymousClass)getElement(); if (anonymousClass != null) { myBaseName = anonymousClass.getBaseClassType().getClassName(); } } return myBaseName; }
@Override protected PsiClass getTargetClass(Editor editor, PsiFile file) { final PsiClass targetClass = super.getTargetClass(editor, file); if (targetClass == null || targetClass instanceof PsiAnonymousClass || targetClass.isEnum()) return null; return targetClass; }
@Nullable public static PsiClass getPreselection(Collection<PsiClass> classes, PsiClass firstClass) { if (firstClass instanceof PsiAnonymousClass && !PropertiesComponent.getInstance().getBoolean(PRESELECT_ANONYMOUS, true)) { for (PsiClass aClass : classes) { if (!(aClass instanceof PsiAnonymousClass)) { return aClass; } } } return null; }
@Override public Result calculateResult(@NotNull Expression[] params, final ExpressionContext context) { PsiElement place = context.getPsiElementAtStartOffset(); while(place != null){ if (place instanceof PsiClass && !(place instanceof PsiAnonymousClass) && !(place instanceof PsiTypeParameter)){ final PsiClass psiClass = ((PsiClass)place); return new TextResult(psiClass.getQualifiedName()); } place = place.getParent(); } return null; }
protected boolean isAvailableOnElementInEditorAndFile(@NotNull final PsiElement element, @NotNull final Editor editor, @NotNull PsiFile file, @NotNull DataContext context) { final PsiElement targetElement = file.findElementAt(editor.getCaretModel().getOffset()); if (PsiTreeUtil.getParentOfType(targetElement, PsiAnonymousClass.class) != null) { return true; } if (PsiTreeUtil.getParentOfType(element, PsiAnonymousClass.class) != null) { return true; } final PsiNewExpression newExpression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class); return newExpression != null && newExpression.getAnonymousClass() != null; }
@Override public boolean canMove(PsiElement[] elements, @Nullable PsiElement targetContainer) { for (PsiElement element : elements) { if (!(element instanceof PsiAnonymousClass)) return false; } return super.canMove(elements, targetContainer); }
public boolean tryToMove(final PsiElement element, final Project project, final DataContext dataContext, final PsiReference reference, final Editor editor) { if (element instanceof PsiAnonymousClass) { new AnonymousToInnerHandler().invoke(project, editor, (PsiAnonymousClass)element); return true; } return false; }
@Override protected InspectionGadgetsFix buildFix(Object... infos) { final PsiClass aClass = (PsiClass)infos[1]; // skip inheritance with delegation for anonymous classes // or better suggest to replace anonymous with inner and then replace with delegation if (aClass instanceof PsiAnonymousClass) { return null; } return new ReplaceInheritanceWithDelegationFix(); }
/** * @see com.siyeh.ig.inheritance.ExtendsConcreteCollectionInspection#buildFix(java.lang.Object...) */ @Override protected InspectionGadgetsFix buildFix(Object... infos) { final PsiClass aClass = (PsiClass)infos[0]; if (aClass instanceof PsiAnonymousClass) { return null; } return new ReplaceInheritanceWithDelegationFix(); }
@Override public void visitField(@NotNull PsiField field) { if (field.hasModifierProperty(PsiModifier.TRANSIENT) || field.hasModifierProperty(PsiModifier.STATIC)) { return; } final PsiClass aClass = field.getContainingClass(); if (aClass == null) { return; } if (ignoreAnonymousInnerClasses && aClass instanceof PsiAnonymousClass) { return; } if (!SerializationUtils.isSerializable(aClass)) { return; } if (SerializationUtils.isProbablySerializable(field.getType())) { return; } final boolean hasWriteObject = SerializationUtils.hasWriteObject(aClass); if (hasWriteObject) { return; } if (isIgnoredSubclass(aClass)) { return; } if (AnnotationUtil.isAnnotated(field, ignorableAnnotations)) { return; } registerFieldError(field, field); }
@Override @NotNull protected InspectionGadgetsFix[] buildFixes(Object... infos) { final PsiClass aClass = (PsiClass)infos[0]; if (aClass.isAnnotationType() || aClass.isInterface() || aClass instanceof PsiAnonymousClass) { return new InspectionGadgetsFix[]{new RemoveSerialVersionUIDFix()}; } return new InspectionGadgetsFix[]{DelegatingFixFactory.createMakeSerializableFix(aClass), new RemoveSerialVersionUIDFix()}; }
@Override protected InspectionGadgetsFix buildFix(Object... infos) { final PsiClass aClass = (PsiClass)infos[2]; if (aClass instanceof PsiAnonymousClass) { return null; } return DelegatingFixFactory.createMakeSerializableFix(aClass); }