private static void addReferencesInRange(List<ASTNode> array, ASTNode parent, int startOffset, int endOffset) { if (parent.getElementType() == JavaElementType.JAVA_CODE_REFERENCE || parent.getElementType() == JavaElementType.REFERENCE_EXPRESSION) { array.add(parent); return; } if (parent.getPsi() instanceof PsiFile) { JspFile jspFile = JspPsiUtil.getJspFile(parent.getPsi()); if (jspFile != null) { JspClass jspClass = (JspClass)jspFile.getJavaClass(); if (jspClass != null) { addReferencesInRange(array, jspClass.getNode(), startOffset, endOffset); } return; } } addReferencesInRangeForComposite(array, parent, startOffset, endOffset); }
public static int getClassPosition(final PsiClass aClass) { if (aClass == null || !aClass.isValid()) { return 0; } try { int pos = aClass instanceof JspClass ? ElementPresentationUtil.CLASS_KIND_JSP : ElementPresentationUtil.getClassKind(aClass); //abstract class before concrete if (pos == ElementPresentationUtil.CLASS_KIND_CLASS || pos == ElementPresentationUtil.CLASS_KIND_EXCEPTION) { boolean isAbstract = aClass.hasModifierProperty(PsiModifier.ABSTRACT) && !aClass.isInterface(); if (isAbstract) { pos --; } } return pos; } catch (IndexNotReadyException e) { return 0; } }
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("the.caret.should.be.positioned.inside.a.class.to.push.members.from")); CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.MEMBERS_PUSH_DOWN); return; } if (element instanceof PsiClass || element instanceof PsiField || element instanceof PsiMethod) { if (element instanceof JspClass) { RefactoringMessageUtil.showNotSupportedForJspClassesError(project, editor, REFACTORING_NAME, HelpID.MEMBERS_PUSH_DOWN); return; } invoke(project, new PsiElement[]{element}, dataContext); return; } element = element.getParent(); } }
public boolean tryToMove(final PsiElement element, final Project project, final DataContext dataContext, final PsiReference reference, final Editor editor) { if (isNonStaticInnerClass(element) && !JavaMoveClassesOrPackagesHandler.isReferenceInAnonymousClass(reference)) { PsiClass aClass = (PsiClass) element; FeatureUsageTracker.getInstance().triggerFeatureUsed("refactoring.move.moveInner"); final PsiClass containingClass = aClass.getContainingClass(); if (containingClass instanceof JspClass) { CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.message("move.nonstatic.class.from.jsp.not.supported"), RefactoringBundle.message("move.title"), null); return true; } MoveInnerImpl.doMove(project, new PsiElement[]{aClass}, null, LangDataKeys.TARGET_PSI_ELEMENT.getData(dataContext)); return true; } return false; }
private static void addReferencesInRange(List<ASTNode> array, ASTNode parent, int startOffset, int endOffset) { if (parent.getElementType() == JavaElementType.JAVA_CODE_REFERENCE || parent.getElementType() == JavaElementType.REFERENCE_EXPRESSION) { array.add(parent); return; } if (parent.getPsi() instanceof PsiFile) { JspFile jspFile = JspPsiUtil.getJspFile(parent.getPsi()); if (jspFile != null) { JspClass jspClass = (JspClass)jspFile.getJavaClass(); addReferencesInRange(array, jspClass.getNode(), startOffset, endOffset); return; } } addReferencesInRangeForComposite(array, parent, startOffset, endOffset); }
private static boolean canImplementOverride(final MethodHierarchyNodeDescriptor descriptor, final MethodHierarchyBrowser methodHierarchyBrowser, final boolean toImplement) { final PsiClass psiClass = descriptor.getPsiClass(); if (psiClass == null || psiClass instanceof JspClass) return false; final PsiMethod baseMethod = methodHierarchyBrowser.getBaseMethod(); if (baseMethod == null) return false; final MethodSignature signature = baseMethod.getSignature(PsiSubstitutor.EMPTY); Collection<MethodSignature> allOriginalSignatures = toImplement ? OverrideImplementUtil.getMethodSignaturesToImplement(psiClass) : OverrideImplementUtil.getMethodSignaturesToOverride(psiClass); for (final MethodSignature originalSignature : allOriginalSignatures) { if (originalSignature.equals(signature)) { return true; } } return false; }
public static boolean invalid4Move(PsiElement element) { PsiFile parentFile; if (element instanceof PsiClassOwner) { final PsiClass[] classes = ((PsiClassOwner)element).getClasses(); if (classes.length == 0) return true; for (PsiClass aClass : classes) { if (aClass instanceof JspClass) return true; } parentFile = (PsiFile)element; } else { if (element instanceof JspClass) return true; if (!(element instanceof PsiClass)) return true; if (element instanceof PsiAnonymousClass) return true; if (((PsiClass)element).getContainingClass() != null) return true; parentFile = element.getContainingFile(); } return parentFile instanceof PsiJavaFile && ProjectRootsUtil.isOutsideSourceRoot(parentFile); }
@Nullable public static PsiClass getContextClass(Project project, Editor editor, PsiFile file, boolean allowInterface) { PsiDocumentManager.getInstance(project).commitAllDocuments(); int offset = editor.getCaretModel().getOffset(); PsiElement element = file.findElementAt(offset); do { element = PsiTreeUtil.getParentOfType(element, PsiClass.class); } while (element instanceof PsiTypeParameter); final PsiClass aClass = (PsiClass)element; if (aClass instanceof JspClass) return null; return aClass == null || !allowInterface && aClass.isInterface() ? null : aClass; }
@Override public void visitClass(PsiClass aClass) { super.visitClass(aClass); if (aClass instanceof JspClass) return; if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkInterfaceMultipleInheritance(aClass)); if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkDuplicateTopLevelClass(aClass)); if (!myHolder.hasErrorResults()) myHolder.add(GenericsHighlightUtil.checkEnumMustNotBeLocal(aClass)); if (!myHolder.hasErrorResults()) myHolder.add(HighlightUtil.checkImplicitThisReferenceBeforeSuper(aClass, myJavaSdkVersion)); if (!myHolder.hasErrorResults()) myHolder.add(HighlightClassUtil.checkClassAndPackageConflict(aClass)); }
@Nullable public static HighlightInfo checkCreateInnerClassFromStaticContext(PsiElement element, PsiElement placeToSearchEnclosingFrom, PsiClass aClass) { if (aClass == null || !PsiUtil.isInnerClass(aClass)) return null; PsiClass outerClass = aClass.getContainingClass(); if (outerClass == null) return null; if (outerClass instanceof JspClass || hasEnclosingInstanceInScope(outerClass, placeToSearchEnclosingFrom, true, false)) return null; return reportIllegalEnclosingUsage(placeToSearchEnclosingFrom, aClass, outerClass, element); }
private static boolean isAvailable(PsiField field) { PsiClass containingClass = field == null ? null : field.getContainingClass(); return field != null && field.getManager().isInProject(field) && !field.hasModifierProperty(PsiModifier.STATIC) && containingClass != null && !(containingClass instanceof JspClass) && containingClass.getName() != null; }
@Override public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { if (element instanceof PsiCompiledElement) return false; final PsiField field = PsiTreeUtil.getParentOfType(element, PsiField.class, false, PsiMember.class, PsiCodeBlock.class, PsiDocComment.class); if (field == null || hasUnsuitableModifiers(field)) return false; if (!field.hasInitializer()) return false; PsiClass psiClass = field.getContainingClass(); return psiClass != null && !psiClass.isInterface() && !(psiClass instanceof PsiAnonymousClass) && !(psiClass instanceof JspClass); }
private void buildChildren(final ArrayList<Block> result, final Alignment childAlignment, final Wrap childWrap) { ASTNode child = myNode.getFirstChildNode(); int state = BEFORE_FIRST; if (myNode.getPsi() instanceof JspClass) { state = INSIDE_BODY; } while (child != null) { if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0) { final Indent indent = calcCurrentIndent(child, state); state = calcNewState(child, state); if (child.getElementType() == JavaElementType.SWITCH_LABEL_STATEMENT) { child = processCaseAndStatementAfter(result, child, childAlignment, childWrap, indent); } else if (myNode.getElementType() == JavaElementType.CLASS && child.getElementType() == JavaTokenType.LBRACE) { child = composeCodeBlock(result, child, getCodeBlockExternalIndent(), myChildrenIndent, null); } else if (myNode.getElementType() == JavaElementType.CODE_BLOCK && child.getElementType() == JavaTokenType.LBRACE && myNode.getTreeParent().getElementType() == JavaElementType.METHOD) { child = composeCodeBlock(result, child, indent, myChildrenIndent, childWrap); } else { child = processChild(result, child, chooseAlignment(child, childAlignment), childWrap, indent); } } if (child != null) { child = child.getTreeNext(); } } }
@Nullable @RequiredReadAction @Override public String getQualifiedName() { PsiClassStub<JspClass> stub = getGreenStub(); if(stub != null) { return stub.getQualifiedName(); } VirtualFile virtualFile = getContainingFile().getVirtualFile(); if(virtualFile == null) { return getName(); } VirtualFile parent = virtualFile.getParent(); if(parent == null) { return null; } String packageName = ProjectFileIndex.getInstance(getProject()).getPackageNameByDirectory(parent); if(StringUtil.isEmpty(packageName)) { return getName(); } return packageName + "." + getName(); }
public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, final DataContext dataContext) { if (elements.length != 1 || !(elements[0] instanceof PsiMethod)) return; final PsiMethod method = (PsiMethod)elements[0]; String message = null; if (!method.getManager().isInProject(method)) { message = "Move method is not supported for non-project methods"; } else if (method.isConstructor()) { message = RefactoringBundle.message("move.method.is.not.supported.for.constructors"); } else if (method.getLanguage()!= JavaLanguage.INSTANCE) { message = RefactoringBundle.message("move.method.is.not.supported.for.0", method.getLanguage().getDisplayName()); } else { final PsiClass containingClass = method.getContainingClass(); if (containingClass != null && PsiUtil.typeParametersIterator(containingClass).hasNext() && TypeParametersSearcher.hasTypeParameters(method)) { message = RefactoringBundle.message("move.method.is.not.supported.for.generic.classes"); } else if (method.findSuperMethods().length > 0 || OverridingMethodsSearch.search(method, true).toArray(PsiMethod.EMPTY_ARRAY).length > 0) { message = RefactoringBundle.message("move.method.is.not.supported.when.method.is.part.of.inheritance.hierarchy"); } else { final Set<PsiClass> classes = MoveInstanceMembersUtil.getThisClassesToMembers(method).keySet(); for (PsiClass aClass : classes) { if (aClass instanceof JspClass) { message = RefactoringBundle.message("synthetic.jsp.class.is.referenced.in.the.method"); Editor editor = CommonDataKeys.EDITOR.getData(dataContext); CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.MOVE_INSTANCE_METHOD); break; } } } } if (message != null) { showErrorHint(project, dataContext, message); return; } final List<PsiVariable> suitableVariables = new ArrayList<PsiVariable>(); message = collectSuitableVariables(method, suitableVariables); if (message != null) { final String unableToMakeStaticMessage = MakeStaticHandler.validateTarget(method); if (unableToMakeStaticMessage != null) { showErrorHint(project, dataContext, message); } else { final String suggestToMakeStaticMessage = "Would you like to make method \'" + method.getName() + "\' static and then move?"; if (Messages .showYesNoCancelDialog(project, message + ". " + suggestToMakeStaticMessage, REFACTORING_NAME, Messages.getErrorIcon()) == Messages.YES) { MakeStaticHandler.invoke(method); } } return; } new MoveInstanceMethodDialog( method, suitableVariables.toArray(new PsiVariable[suitableVariables.size()])).show(); }
public static boolean isJavaContext(PsiElement position) { if(PsiTreeUtil.getContextOfType(position, JspClass.class, false) != null) return true; return false; }
@Nullable public static HighlightInfo checkNotAllowedModifier(@NotNull PsiKeyword keyword, @NotNull PsiModifierList modifierList) { PsiElement modifierOwner = modifierList.getParent(); if (modifierOwner == null) return null; if (PsiUtilCore.hasErrorElementChild(modifierOwner)) return null; @PsiModifier.ModifierConstant String modifier = keyword.getText(); final Map<String, Set<String>> incompatibleModifierMap = getIncompatibleModifierMap(modifierList); if (incompatibleModifierMap == null) return null; Set<String> incompatibles = incompatibleModifierMap.get(modifier); PsiElement modifierOwnerParent = modifierOwner instanceof PsiMember ? ((PsiMember)modifierOwner).getContainingClass() : modifierOwner.getParent(); if (modifierOwnerParent == null) modifierOwnerParent = modifierOwner.getParent(); boolean isAllowed = true; if (modifierOwner instanceof PsiClass) { PsiClass aClass = (PsiClass)modifierOwner; if (aClass.isInterface()) { if (PsiModifier.STATIC.equals(modifier) || PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.PACKAGE_LOCAL.equals(modifier)) { isAllowed = modifierOwnerParent instanceof PsiClass; } } else { if (PsiModifier.PUBLIC.equals(modifier)) { isAllowed = modifierOwnerParent instanceof PsiJavaFile || modifierOwnerParent instanceof PsiClass && (modifierOwnerParent instanceof JspClass || ((PsiClass)modifierOwnerParent).getQualifiedName() != null); } else if (PsiModifier.STATIC.equals(modifier) || PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.PACKAGE_LOCAL.equals(modifier)) { isAllowed = modifierOwnerParent instanceof PsiClass && ((PsiClass)modifierOwnerParent).getQualifiedName() != null; } if (aClass.isEnum()) { isAllowed &= !(PsiModifier.FINAL.equals(modifier) || PsiModifier.ABSTRACT.equals(modifier)); } if (aClass.getContainingClass() instanceof PsiAnonymousClass) { isAllowed &= !(PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier)); } } } else if (modifierOwner instanceof PsiMethod) { PsiMethod method = (PsiMethod)modifierOwner; isAllowed = !(method.isConstructor() && ourConstructorNotAllowedModifiers.contains(modifier)); PsiClass containingClass = method.getContainingClass(); if ((method.hasModifierProperty(PsiModifier.PUBLIC) || method.hasModifierProperty(PsiModifier.PROTECTED)) && method.isConstructor() && containingClass != null && containingClass.isEnum()) { isAllowed = false; } if (PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.TRANSIENT.equals(modifier) || PsiModifier.STRICTFP.equals(modifier) || PsiModifier.SYNCHRONIZED.equals(modifier)) { isAllowed &= modifierOwnerParent instanceof PsiClass && !((PsiClass)modifierOwnerParent).isInterface(); } } else if (modifierOwner instanceof PsiField) { if (PsiModifier.PRIVATE.equals(modifier) || PsiModifier.PROTECTED.equals(modifier) || PsiModifier.TRANSIENT.equals(modifier) || PsiModifier.STRICTFP.equals(modifier) || PsiModifier.SYNCHRONIZED.equals(modifier)) { isAllowed = modifierOwnerParent instanceof PsiClass && !((PsiClass)modifierOwnerParent).isInterface(); } } else if (modifierOwner instanceof PsiClassInitializer) { isAllowed = PsiModifier.STATIC.equals(modifier); } else if (modifierOwner instanceof PsiLocalVariable || modifierOwner instanceof PsiParameter) { isAllowed = PsiModifier.FINAL.equals(modifier); } isAllowed &= incompatibles != null; if (!isAllowed) { String message = JavaErrorMessages.message("modifier.not.allowed", modifier); HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(keyword).descriptionAndTooltip(message).create(); QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createModifierListFix(modifierList, modifier, false, false)); return highlightInfo; } return null; }
public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, final DataContext dataContext) { if (elements.length != 1 || !(elements[0] instanceof PsiMethod)) return; final PsiMethod method = (PsiMethod)elements[0]; String message = null; if (!method.getManager().isInProject(method)) { message = "Move method is not supported for non-project methods"; } else if (method.isConstructor()) { message = RefactoringBundle.message("move.method.is.not.supported.for.constructors"); } else if (method.getLanguage()!= JavaLanguage.INSTANCE) { message = RefactoringBundle.message("move.method.is.not.supported.for.0", method.getLanguage().getDisplayName()); } else { final PsiClass containingClass = method.getContainingClass(); if (containingClass != null && PsiUtil.typeParametersIterator(containingClass).hasNext() && TypeParametersSearcher.hasTypeParameters(method)) { message = RefactoringBundle.message("move.method.is.not.supported.for.generic.classes"); } else if (method.findSuperMethods().length > 0 || OverridingMethodsSearch.search(method, true).toArray(PsiMethod.EMPTY_ARRAY).length > 0) { message = RefactoringBundle.message("move.method.is.not.supported.when.method.is.part.of.inheritance.hierarchy"); } else { final Set<PsiClass> classes = MoveInstanceMembersUtil.getThisClassesToMembers(method).keySet(); for (PsiClass aClass : classes) { if (aClass instanceof JspClass) { message = RefactoringBundle.message("synthetic.jsp.class.is.referenced.in.the.method"); Editor editor = PlatformDataKeys.EDITOR.getData(dataContext); CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.MOVE_INSTANCE_METHOD); break; } } } } if (message != null) { showErrorHint(project, dataContext, message); return; } final List<PsiVariable> suitableVariables = new ArrayList<PsiVariable>(); message = collectSuitableVariables(method, suitableVariables); if (message != null) { final String unableToMakeStaticMessage = MakeStaticHandler.validateTarget(method); if (unableToMakeStaticMessage != null) { showErrorHint(project, dataContext, message); } else { final String suggestToMakeStaticMessage = "Would you like to make method \'" + method.getName() + "\' static and then move?"; if (Messages .showYesNoCancelDialog(project, message + ". " + suggestToMakeStaticMessage, REFACTORING_NAME, Messages.getErrorIcon()) == DialogWrapper.OK_EXIT_CODE) { MakeStaticHandler.invoke(method); } } return; } new MoveInstanceMethodDialog( method, suitableVariables.toArray(new PsiVariable[suitableVariables.size()])).show(); }
/** * Ignore these, they cause an NPE inside of AnnotationUtil */ private static boolean isBrokenPsiClass(PsiClass psiClass) { return (psiClass == null || psiClass instanceof PsiAnonymousClass || psiClass instanceof JspClass); }
@Override public JspClass getJspClass() { return (JspClass) getParent(); }
public JspClassImpl(@NotNull PsiClassStub<JspClass> stub, @NotNull IStubElementType nodeType) { super(stub, nodeType); }
@Override @RequiredReadAction public boolean processDeclarations(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state, PsiElement lastParent, @NotNull PsiElement place) { if(!processImpl(processor, state, lastParent, place)) { return false; } LanguageLevel level = PsiUtil.getLanguageLevel(place); JspClass jspClass = PsiTreeUtil.getParentOfType(this, JspClass.class); if(jspClass == null) { return true; } XmlTag[] directiveTags = jspClass.getJspxFile().getDirectiveTags(JspDirectiveKind.INCLUDE, false); for(XmlTag directiveTag : directiveTags) { if(directiveTag.getStartOffsetInParent() > getStartOffsetInParent()) { return true; } XmlAttribute attribute = directiveTag.getAttribute("file"); if(attribute == null) { continue; } XmlAttributeValue valueElement = attribute.getValueElement(); if(valueElement == null) { continue; } PsiReference lastRef = null; PsiReference[] references = valueElement.getReferences(); for(PsiReference reference : references) { if(reference instanceof FileReference) { lastRef = ((FileReference) reference).getLastFileReference(); break; } } if(lastRef == null) { continue; } PsiElement element = lastRef.resolve(); if(!(element instanceof JspFile)) { continue; } PsiClass javaClass = ((JspFile) element).getJavaClass(); if(javaClass == null) { continue; } if(!PsiClassImplUtil.processDeclarationsInClass(javaClass, processor, state, null, lastParent, place, level, false)) { return false; } } return true; }