@Nullable public static CreateClassOrPackageFix createFix(@NotNull final String qualifiedName, @NotNull final GlobalSearchScope scope, @NotNull final PsiElement context, @Nullable final PsiPackage basePackage, @Nullable ClassKind kind, @Nullable String superClass, @Nullable String templateName) { final List<PsiDirectory> directories = getWritableDirectoryListDefault(basePackage, scope, context.getManager()); if (directories.isEmpty()) { return null; } final String redPart = basePackage == null ? qualifiedName : qualifiedName.substring(basePackage.getQualifiedName().length() + 1); final int dot = redPart.indexOf('.'); final boolean fixPath = dot >= 0; final String firstRedName = fixPath ? redPart.substring(0, dot) : redPart; for (Iterator<PsiDirectory> i = directories.iterator(); i.hasNext(); ) { if (!checkCreateClassOrPackage(kind != null && !fixPath, i.next(), firstRedName)) { i.remove(); } } return new CreateClassOrPackageFix(directories, context, fixPath ? qualifiedName : redPart, redPart, kind, superClass, templateName); }
@Nullable public static CreateClassOrPackageFix createFix(@NotNull final String qualifiedName, @NotNull final PsiElement context, @Nullable ClassKind kind, String superClass) { return createFix(qualifiedName, context.getResolveScope(), context, null, kind, superClass, null); }
private CreateClassOrPackageFix(@NotNull List<PsiDirectory> writableDirectoryList, @NotNull PsiElement context, @NotNull String presentation, @NotNull String redPart, @Nullable ClassKind kind, @Nullable String superClass, @Nullable final String templateName) { super(context); myRedPart = redPart; myTemplateName = templateName; myWritableDirectoryList = writableDirectoryList; myClassKind = kind; mySuperClass = superClass; myPresentation = presentation; }
@Override @NotNull public String getText() { return QuickFixBundle.message( myClassKind == ClassKind.INTERFACE ? "create.interface.text" : myClassKind != null ? "create.class.text" : "create.package.text", myPresentation); }
private static boolean isClassAccepted(final PsiClass clazz, @Nullable final ClassKind classKind, final boolean instantiatable, final boolean concrete, final boolean notInterface, final boolean notEnum) { if (classKind == ClassKind.ANNOTATION) return clazz.isAnnotationType(); if (classKind == ClassKind.ENUM) return clazz.isEnum(); if (instantiatable) { if (PsiUtil.isInstantiatable(clazz)) { return true; } } else if (concrete) { if (!clazz.hasModifierProperty(PsiModifier.ABSTRACT) && !clazz.isInterface()) { return true; } } else if (notInterface) { if (!clazz.isInterface()) { return true; } } else if (notEnum) { if (!clazz.isEnum()) { return true; } } else { return true; } return false; }
public void processSubclassVariants(@NotNull PsiPackage context, @NotNull String[] extendClasses, Consumer<LookupElement> result) { GlobalSearchScope packageScope = PackageScope.packageScope(context, true); GlobalSearchScope scope = myJavaClassReferenceSet.getProvider().getScope(getElement().getProject()); if (scope != null) { packageScope = packageScope.intersectWith(scope); } final GlobalSearchScope allScope = ProjectScope.getAllScope(context.getProject()); final boolean instantiatable = JavaClassReferenceProvider.INSTANTIATABLE.getBooleanValue(getOptions()); final boolean notInterface = JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(getOptions()); final boolean notEnum = JavaClassReferenceProvider.NOT_ENUM.getBooleanValue(getOptions()); final boolean concrete = JavaClassReferenceProvider.CONCRETE.getBooleanValue(getOptions()); final ClassKind classKind = getClassKind(); for (String extendClassName : extendClasses) { final PsiClass extendClass = JavaPsiFacade.getInstance(context.getProject()).findClass(extendClassName, allScope); if (extendClass != null) { // add itself if (packageScope.contains(extendClass.getContainingFile().getVirtualFile())) { if (isClassAccepted(extendClass, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(extendClass, extendClassName)); } } for (final PsiClass clazz : ClassInheritorsSearch.search(extendClass, packageScope, true)) { String qname = clazz.getQualifiedName(); if (qname != null && isClassAccepted(clazz, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(clazz, qname)); } } } } }
@Override protected JavaClassReferenceProvider createClassReferenceProvider(GenericDomValue<PsiClass> genericDomValue, ConvertContext context, ExtendClass extendClass) { final JavaClassReferenceProvider provider = super.createClassReferenceProvider(genericDomValue, context, extendClass); provider.setOption(JavaClassReferenceProvider.CLASS_KIND, ClassKind.ANNOTATION); return provider; }
@Override protected JavaClassReferenceProvider createClassReferenceProvider(GenericDomValue<PsiClass> genericDomValue, ConvertContext context, ExtendClass extendClass) { final JavaClassReferenceProvider provider = super.createClassReferenceProvider(genericDomValue, context, extendClass); provider.setOption(JavaClassReferenceProvider.CLASS_KIND, ClassKind.ENUM); return provider; }
@Override public void registerReferenceProviders(PsiReferenceRegistrar registrar) { JavaClassReferenceProvider provider = new JavaClassReferenceProvider(); provider.setOption(JavaClassReferenceProvider.CLASS_KIND, ClassKind.CLASS); registrar.registerReferenceProvider( JavaClassQualifiedNameReference.PATTERN, new JavaClassReferenceProvider()); }
@Nullable public static CreateClassOrPackageFix createCreateClassOrPackageFix( @NotNull final String qualifiedName, @NotNull final GlobalSearchScope scope, @NotNull final PsiElement context, @NotNull final ClassKind classKind, @Nullable final String superClass, @Nullable final String template) { final PsiPackage package$; final int lastIndexOfDot = qualifiedName.lastIndexOf('.'); if(lastIndexOfDot == -1) { package$ = null; } else if(lastIndexOfDot == (qualifiedName.length() - 1)) { return null; } else { package$ = JavaPsiFacade. getInstance(context.getProject()). findPackage(qualifiedName.substring(0, lastIndexOfDot)); } return CreateClassOrPackageFix. createFix( qualifiedName, scope, context, package$, classKind, superClass, template); }
@Nullable public static CreateClassOrPackageFix createFix(@NotNull final String qualifiedName, @NotNull final GlobalSearchScope scope, @NotNull final PsiElement context, @Nullable final PsiPackage basePackage, @Nullable ClassKind kind, @Nullable String superClass, @Nullable String templateName) { final List<PsiDirectory> directories = getWritableDirectoryListDefault(basePackage, scope, context.getManager()); if (directories.isEmpty()) { return null; } final String redPart = basePackage == null ? qualifiedName : qualifiedName.substring(basePackage.getQualifiedName().length() + 1); final int dot = redPart.indexOf('.'); final boolean fixPath = dot >= 0; final String firstRedName = fixPath ? redPart.substring(0, dot) : redPart; for (Iterator<PsiDirectory> i = directories.iterator(); i.hasNext(); ) { if (!checkCreateClassOrPackage(kind != null && !fixPath, i.next(), firstRedName)) { i.remove(); } } return directories.isEmpty() ? null : new CreateClassOrPackageFix(directories, context, fixPath ? qualifiedName : redPart, redPart, kind, superClass, templateName); }
private Object[] processPackage(final PsiPackage aPackage) { final ArrayList<Object> list = new ArrayList<Object>(); final int startOffset = StringUtil.isEmpty(aPackage.getName()) ? 0 : aPackage.getQualifiedName().length() + 1; final GlobalSearchScope scope = getScope(); for (final PsiPackage subPackage : aPackage.getSubPackages(scope)) { final String shortName = subPackage.getQualifiedName().substring(startOffset); if (JavaPsiFacade.getInstance(subPackage.getProject()).getNameHelper().isIdentifier(shortName)) { list.add(subPackage); } } final List<PsiClass> classes = ContainerUtil.filter(aPackage.getClasses(scope), new Condition<PsiClass>() { @Override public boolean value(PsiClass psiClass) { return StringUtil.isNotEmpty(psiClass.getName()); } }); final Map<CustomizableReferenceProvider.CustomizationKey, Object> options = getOptions(); if (options != null) { final boolean instantiatable = JavaClassReferenceProvider.INSTANTIATABLE.getBooleanValue(options); final boolean concrete = JavaClassReferenceProvider.CONCRETE.getBooleanValue(options); final boolean notInterface = JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(options); final boolean notEnum = JavaClassReferenceProvider.NOT_ENUM.getBooleanValue(options); final ClassKind classKind = getClassKind(); for (PsiClass clazz : classes) { if (isClassAccepted(clazz, classKind, instantiatable, concrete, notInterface, notEnum)) { list.add(clazz); } } } else { list.addAll(classes); } return list.toArray(); }
@NotNull public void processSubclassVariants(@NotNull PsiPackage context, @NotNull String[] extendClasses, Consumer<LookupElement> result) { GlobalSearchScope packageScope = PackageScope.packageScope(context, true); GlobalSearchScope scope = myJavaClassReferenceSet.getProvider().getScope(getElement().getProject()); if (scope != null) { packageScope = packageScope.intersectWith(scope); } final GlobalSearchScope allScope = ProjectScope.getAllScope(context.getProject()); final boolean instantiatable = JavaClassReferenceProvider.INSTANTIATABLE.getBooleanValue(getOptions()); final boolean notInterface = JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(getOptions()); final boolean notEnum = JavaClassReferenceProvider.NOT_ENUM.getBooleanValue(getOptions()); final boolean concrete = JavaClassReferenceProvider.CONCRETE.getBooleanValue(getOptions()); final ClassKind classKind = getClassKind(); for (String extendClassName : extendClasses) { final PsiClass extendClass = JavaPsiFacade.getInstance(context.getProject()).findClass(extendClassName, allScope); if (extendClass != null) { // add itself if (packageScope.contains(extendClass.getContainingFile().getVirtualFile())) { if (isClassAccepted(extendClass, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(extendClass, extendClassName)); } } for (final PsiClass clazz : ClassInheritorsSearch.search(extendClass, packageScope, true)) { String qname = clazz.getQualifiedName(); if (qname != null && isClassAccepted(clazz, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(clazz, qname)); } } } } }
@Override protected JavaClassReferenceProvider createClassReferenceProvider(GenericDomValue<PsiClass> genericDomValue, ConvertContext context, ExtendClass extendClass) { final JavaClassReferenceProvider provider = super.createClassReferenceProvider(genericDomValue, context, extendClass); provider.setOption(JavaClassReferenceProvider.CLASS_KIND, ClassKind.ANNOTATION); //provider.setOption(JavaClassReferenceProvider.EXTEND_CLASS_NAMES, new String[] {"org.springframework.samples.petclinic.jsr330.Foo"}); // return provider; }
@Nullable public static CreateClassOrPackageFix createFix(@NotNull final String qualifiedName, @NotNull final GlobalSearchScope scope, @NotNull final PsiElement context, @Nullable final PsiJavaPackage basePackage, @Nullable ClassKind kind, @Nullable String superClass, @Nullable String templateName) { final List<PsiDirectory> directories = getWritableDirectoryListDefault(basePackage, scope, context.getManager()); if (directories.isEmpty()) { return null; } final String redPart = basePackage == null ? qualifiedName : qualifiedName.substring(basePackage.getQualifiedName().length() + 1); final int dot = redPart.indexOf('.'); final boolean fixPath = dot >= 0; final String firstRedName = fixPath ? redPart.substring(0, dot) : redPart; for (Iterator<PsiDirectory> i = directories.iterator(); i.hasNext(); ) { if (!checkCreateClassOrPackage(kind != null && !fixPath, i.next(), firstRedName)) { i.remove(); } } return directories.isEmpty() ? null : new CreateClassOrPackageFix(directories, context, fixPath ? qualifiedName : redPart, redPart, kind, superClass, templateName); }
@Override @NotNull public String getText() { return JavaQuickFixBundle.message( myClassKind == ClassKind.INTERFACE ? "create.interface.text" : myClassKind != null ? "create.class.text" : "create.package.text", myPresentation); }
public void processSubclassVariants(@NotNull PsiJavaPackage context, @NotNull String[] extendClasses, Consumer<LookupElement> result) { GlobalSearchScope packageScope = PackageScope.packageScope(context, true); GlobalSearchScope scope = myJavaClassReferenceSet.getProvider().getScope(getElement().getProject()); if(scope != null) { packageScope = packageScope.intersectWith(scope); } final GlobalSearchScope allScope = ProjectScope.getAllScope(context.getProject()); final boolean instantiatable = JavaClassReferenceProvider.INSTANTIATABLE.getBooleanValue(getOptions()); final boolean notInterface = JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(getOptions()); final boolean notEnum = JavaClassReferenceProvider.NOT_ENUM.getBooleanValue(getOptions()); final boolean concrete = JavaClassReferenceProvider.CONCRETE.getBooleanValue(getOptions()); final ClassKind classKind = getClassKind(); for(String extendClassName : extendClasses) { final PsiClass extendClass = JavaPsiFacade.getInstance(context.getProject()).findClass(extendClassName, allScope); if(extendClass != null) { // add itself if(packageScope.contains(extendClass.getContainingFile().getVirtualFile())) { if(isClassAccepted(extendClass, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(extendClass, extendClassName)); } } for(final PsiClass clazz : ClassInheritorsSearch.search(extendClass, packageScope, true)) { String qname = clazz.getQualifiedName(); if(qname != null && isClassAccepted(clazz, classKind, instantiatable, concrete, notInterface, notEnum)) { result.consume(createSubclassLookupValue(clazz, qname)); } } } } }
@Override @Nullable public IntentionAction createCreateClassOrPackageFix(@NotNull final PsiElement context, @NotNull final String qualifiedName, final boolean createClass, final String superClass) { return CreateClassOrPackageFix.createFix(qualifiedName, context, createClass ? ClassKind.CLASS : null, superClass); }
@Override @Nullable public IntentionAction createCreateClassOrInterfaceFix(@NotNull final PsiElement context, @NotNull final String qualifiedName, final boolean createClass, final String superClass) { return CreateClassOrPackageFix.createFix(qualifiedName, context, createClass ? ClassKind.CLASS : ClassKind.INTERFACE, superClass); }
@Nullable public ClassKind getClassKind() { return JavaClassReferenceProvider.CLASS_KIND.getValue(getOptions()); }
@Nullable private List<? extends LocalQuickFix> registerFixes() { final List<LocalQuickFix> list = QuickFixFactory.getInstance().registerOrderEntryFixes(new QuickFixActionRegistrarImpl(null), this); final String[] extendClasses = getExtendClassNames(); final String extendClass = extendClasses != null && extendClasses.length > 0 ? extendClasses[0] : null; final JavaClassReference[] references = getJavaClassReferenceSet().getAllReferences(); PsiPackage contextPackage = null; for (int i = myIndex; i >= 0; i--) { final PsiElement context = references[i].getContext(); if (context != null) { if (context instanceof PsiPackage) { contextPackage = (PsiPackage)context; } break; } } boolean createJavaClass = !canReferencePackage(); ClassKind kind = createJavaClass ? getClassKind() : null; if (createJavaClass && kind == null) kind = ClassKind.CLASS; final String templateName = JavaClassReferenceProvider.CLASS_TEMPLATE.getValue(getOptions()); final TextRange range = new TextRange(references[0].getRangeInElement().getStartOffset(), getRangeInElement().getEndOffset()); final String qualifiedName = range.substring(getElement().getText()); final CreateClassOrPackageFix action = CreateClassOrPackageFix.createFix(qualifiedName, getScope(getJavaContextFile()), getElement(), contextPackage, kind, extendClass, templateName); if (action != null) { if (list == null) { return Collections.singletonList(action); } else { final ArrayList<LocalQuickFix> fixes = new ArrayList<LocalQuickFix>(list.size() + 1); fixes.addAll(list); fixes.add(action); return fixes; } } return list; }
@Nullable public static CreateClassOrPackageFix createFix(@NotNull final String qualifiedName, @NotNull final PsiElement context, @Nullable ClassKind kind, final String superClass) { return createFix(qualifiedName, context.getResolveScope(), context, null, kind, superClass, null); }
@Nullable private List<? extends LocalQuickFix> registerFixes(final HighlightInfo info) { final List<LocalQuickFix> list = OrderEntryFix.registerFixes(new QuickFixActionRegistrarImpl(info), this); final String[] extendClasses = getExtendClassNames(); final String extendClass = extendClasses != null && extendClasses.length > 0 ? extendClasses[0] : null; final JavaClassReference[] references = getJavaClassReferenceSet().getAllReferences(); PsiPackage contextPackage = null; for (int i = myIndex; i >= 0; i--) { final PsiElement context = references[i].getContext(); if (context != null) { if (context instanceof PsiPackage) { contextPackage = (PsiPackage)context; } break; } } boolean createJavaClass = !canReferencePackage(); ClassKind kind = createJavaClass ? getClassKind() : null; if (createJavaClass && kind == null) kind = ClassKind.CLASS; final String templateName = JavaClassReferenceProvider.CLASS_TEMPLATE.getValue(getOptions()); final TextRange range = new TextRange(references[0].getRangeInElement().getStartOffset(), getRangeInElement().getEndOffset()); final String qualifiedName = range.substring(getElement().getText()); final CreateClassOrPackageFix action = CreateClassOrPackageFix.createFix(qualifiedName, getScope(), getElement(), contextPackage, kind, extendClass, templateName); if (action != null) { QuickFixAction.registerQuickFixAction(info, action); if (list == null) { return Arrays.asList(action); } else { final ArrayList<LocalQuickFix> fixes = new ArrayList<LocalQuickFix>(list.size() + 1); fixes.addAll(list); fixes.add(action); return fixes; } } return list; }
private static boolean isClassAccepted(final PsiClass clazz, @Nullable final ClassKind classKind, final boolean instantiatable, final boolean concrete, final boolean notInterface, final boolean notEnum) { if(classKind == ClassKind.ANNOTATION) { return clazz.isAnnotationType(); } if(classKind == ClassKind.ENUM) { return clazz.isEnum(); } if(instantiatable) { if(PsiUtil.isInstantiatable(clazz)) { return true; } } else if(concrete) { if(!clazz.hasModifierProperty(PsiModifier.ABSTRACT) && !clazz.isInterface()) { return true; } } else if(notInterface) { if(!clazz.isInterface()) { return true; } } else if(notEnum) { if(!clazz.isEnum()) { return true; } } else { return true; } return false; }
@Nullable private List<? extends LocalQuickFix> registerFixes() { final List<LocalQuickFix> list = QuickFixFactory.getInstance().registerOrderEntryFixes(new QuickFixActionRegistrarImpl(null), this); final String[] extendClasses = getExtendClassNames(); final String extendClass = extendClasses != null && extendClasses.length > 0 ? extendClasses[0] : null; final JavaClassReference[] references = getJavaClassReferenceSet().getAllReferences(); PsiJavaPackage contextPackage = null; for(int i = myIndex; i >= 0; i--) { final PsiElement context = references[i].getContext(); if(context != null) { if(context instanceof PsiJavaPackage) { contextPackage = (PsiJavaPackage) context; } break; } } boolean createJavaClass = !canReferencePackage(); ClassKind kind = createJavaClass ? getClassKind() : null; if(createJavaClass && kind == null) { kind = ClassKind.CLASS; } final String templateName = JavaClassReferenceProvider.CLASS_TEMPLATE.getValue(getOptions()); final TextRange range = new TextRange(references[0].getRangeInElement().getStartOffset(), getRangeInElement().getEndOffset()); final String qualifiedName = range.substring(getElement().getText()); final CreateClassOrPackageFix action = CreateClassOrPackageFix.createFix(qualifiedName, getScope(getJavaContextFile()), getElement(), contextPackage, kind, extendClass, templateName); if(action != null) { if(list == null) { return Collections.singletonList(action); } else { final ArrayList<LocalQuickFix> fixes = new ArrayList<LocalQuickFix>(list.size() + 1); fixes.addAll(list); fixes.add(action); return fixes; } } return list; }