private static void seekChildren(PsiElement element, PsiIdentifier target, Find find) { for (PsiElement child : element.getChildren()) { if (child instanceof PsiLocalVariable) { PsiLocalVariable localVariable = (PsiLocalVariable) child; if (isSameName(target, localVariable.getNameIdentifier())) { find.findLocalVariable = localVariable; return; } } if (child instanceof PsiParameter) { PsiParameter parameter = (PsiParameter) child; if (isSameName(target, parameter.getNameIdentifier())) { find.findParameter = parameter; return; } } seekChildren(child, target, find); if (!find.isEmpty()) return; } }
public void testCamelGutterForVariableAndConstant() { myFixture.configureByFiles("JavaCamelRouteLineMarkerProviderFromVariableTestData.java"); List<GutterMark> gutters = myFixture.findAllGutters(); assertNotNull(gutters); //remove first element since it is navigate to super implementation gutter icon gutters.remove(0); assertEquals("Should contain 2 Camel gutters", 2, gutters.size()); assertGuttersHasCamelIcon(gutters); LineMarkerInfo.LineMarkerGutterIconRenderer firstGutter = (LineMarkerInfo.LineMarkerGutterIconRenderer) gutters.get(0); assertTrue(firstGutter.getLineMarkerInfo().getElement() instanceof PsiIdentifier); assertEquals("The navigation start element doesn't match", "uriVar", firstGutter.getLineMarkerInfo().getElement().getText()); List<GotoRelatedItem> firstGutterTargets = getGutterNavigationDestinationElements(firstGutter); assertEquals("Navigation should have two targets", 2, firstGutterTargets.size()); }
public void testCamelGutterForMethodCallFrom() { myFixture.configureByFiles("JavaCamelRouteLineMarkerProviderFromMethodCallTestData.java"); List<GutterMark> gutters = myFixture.findAllGutters(); assertNotNull(gutters); //remove first element since it is navigate to super implementation gutter icon gutters.remove(0); // remove last element since it is from method returning route uri gutters.remove(gutters.size() - 1); assertEquals("Should contain 1 Camel gutters", 1, gutters.size()); assertGuttersHasCamelIcon(gutters); LineMarkerInfo.LineMarkerGutterIconRenderer firstGutter = (LineMarkerInfo.LineMarkerGutterIconRenderer) gutters.get(0); assertTrue(firstGutter.getLineMarkerInfo().getElement() instanceof PsiIdentifier); assertEquals("The navigation start element doesn't match", "calcEndpoint", firstGutter.getLineMarkerInfo().getElement().getText()); List<GotoRelatedItem> firstGutterTargets = getGutterNavigationDestinationElements(firstGutter); assertEquals("Navigation should have two targets", 2, firstGutterTargets.size()); assertEquals("The navigation variable target element doesn't match", "calcEndpoint", getGuttersWithMethodTarget(firstGutterTargets).get(0).getName()); }
protected ClsMemberImpl(T stub) { super(stub); myDocComment = !isDeprecated() ? null : new AtomicNotNullLazyValue<PsiDocComment>() { @NotNull @Override protected PsiDocComment compute() { return new ClsDocCommentImpl(ClsMemberImpl.this); } }; myNameIdentifier = new AtomicNotNullLazyValue<PsiIdentifier>() { @NotNull @Override protected PsiIdentifier compute() { return new ClsIdentifierImpl(ClsMemberImpl.this, getName()); } }; }
@Override public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { List<TextRange> result = super.select(e, editorText, cursorOffset, editor); final PsiField field = (PsiField)e; final TextRange range = field.getTextRange(); final PsiIdentifier first = field.getNameIdentifier(); final TextRange firstRange = first.getTextRange(); final PsiElement last = field.getInitializer(); final int end = last == null ? firstRange.getEndOffset() : last.getTextRange().getEndOffset(); PsiDocComment comment = field.getDocComment(); if (comment != null) { TextRange commentTextRange = comment.getTextRange(); addRangeElem(result, editorText, comment, commentTextRange.getEndOffset()); } addRangeElem(result, editorText, first, end); //addRangeElem (result, editorText, field, textLength, field.getTypeElement(), end); addRangeElem(result, editorText, field.getModifierList(), range.getEndOffset()); //addRangeElem (result, editorText, field, textLength, field.getDocComment(), end); result.addAll(expandToWholeLine(editorText, range)); return result; }
@Override public void tokenize(@NotNull T element, TokenConsumer consumer) { final PsiIdentifier psiIdentifier = PsiTreeUtil.getChildOfType(element, PsiIdentifier.class); final PsiTypeElement psiType = PsiTreeUtil.getChildOfType(element, PsiTypeElement.class); if (psiIdentifier == null) { return; } final String identifier = psiIdentifier.getText(); final String type = psiType==null?null:psiType.getText(); if (identifier == null) { return; } if (type == null || !type.equalsIgnoreCase(identifier)) { myIdentifierTokenizer.tokenize(psiIdentifier, consumer); } if (psiType != null) { myTypeTokenizer.tokenize(psiType, consumer); } }
@Override public void visitMethod(PsiMethod method) { super.visitMethod(method); if (!TestNGUtil.hasTest(method)) { return; } final PsiIdentifier nameIdentifier = method.getNameIdentifier(); if (nameIdentifier == null) { return; } final String name = method.getName(); if (isValid(name)) { return; } if (!isOnTheFly() && MethodUtils.hasSuper(method)) { return; } if (LibraryUtil.isOverrideOfLibraryMethod(method)) { return; } registerMethodError(method, name); }
@Override public void visitMethod(PsiMethod method) { super.visitMethod(method); if (!TestUtils.isJUnit4TestMethod(method) || !TestUtils.isRunnable(method)) { return; } final PsiIdentifier nameIdentifier = method.getNameIdentifier(); if (nameIdentifier == null) { return; } final String name = method.getName(); if (isValid(name)) { return; } if (!isOnTheFly() && MethodUtils.hasSuper(method)) { return; } if (LibraryUtil.isOverrideOfLibraryMethod(method)) { return; } registerMethodError(method, name); }
@Override public void visitMethod(PsiMethod method) { super.visitMethod(method); if (!TestUtils.isJUnit3TestMethod(method) || !TestUtils.isRunnable(method)) { return; } final PsiIdentifier nameIdentifier = method.getNameIdentifier(); if (nameIdentifier == null) { return; } final String name = method.getName(); if (isValid(name)) { return; } if (!isOnTheFly() && MethodUtils.hasSuper(method)) { return; } if (LibraryUtil.isOverrideOfLibraryMethod(method)) { return; } registerMethodError(method, name); }
@Override public void visitTypeParameter(PsiTypeParameter parameter) { super.visitTypeParameter(parameter); final String name = parameter.getName(); if (name == null) { return; } if (isValid(name)) { return; } final PsiIdentifier nameIdentifier = parameter.getNameIdentifier(); if (nameIdentifier == null) { return; } registerError(nameIdentifier, name); }
@Override public void visitMethod(@NotNull PsiMethod method) { super.visitMethod(method); if (method.isConstructor()) { return; } if (!method.hasModifierProperty(PsiModifier.NATIVE)) { return; } final PsiIdentifier nameIdentifier = method.getNameIdentifier(); if (nameIdentifier == null) { return; } final String name = method.getName(); if (isValid(name)) { return; } if (!isOnTheFly() && MethodUtils.hasSuper(method)) { return; } if (LibraryUtil.isOverrideOfLibraryMethod(method)) { return; } registerMethodError(method, name); }
@Override public void visitContinueStatement( @NotNull PsiContinueStatement statement) { super.visitContinueStatement(statement); final PsiIdentifier label = statement.getLabelIdentifier(); if (label == null) { return; } final String labelText = label.getText(); if (labelText == null) { return; } if (labelText.length() == 0) { return; } registerStatementError(statement); }
@Override public void visitBreakStatement(@NotNull PsiBreakStatement statement) { super.visitBreakStatement(statement); final PsiIdentifier labelIdentifier = statement.getLabelIdentifier(); if (labelIdentifier == null) { return; } final String labelText = labelIdentifier.getText(); if (labelText == null) { return; } if (labelText.length() == 0) { return; } registerStatementError(statement); }
@NotNull @Override public IntentionAction[] getQuickFixes(@NotNull PsiElement element) { if (!(element instanceof PsiIdentifier)) return IntentionAction.EMPTY_ARRAY; PsiElement parent = element.getParent(); if (!(parent instanceof PsiClass)) return IntentionAction.EMPTY_ARRAY; PsiClass psiClass = (PsiClass)parent; if (InheritanceUtil.isInheritor(psiClass, LocalInspectionTool.class.getName())) { return new IntentionAction[]{new RegisterInspectionFix(psiClass, LocalInspectionEP.LOCAL_INSPECTION)}; } if (InheritanceUtil.isInheritor(psiClass, GlobalInspectionTool.class.getName())) { return new IntentionAction[]{new RegisterInspectionFix(psiClass, InspectionEP.GLOBAL_INSPECTION)}; } ExtensionPointLocator extensionPointLocator = new ExtensionPointLocator(psiClass); List<ExtensionPointCandidate> candidateList = extensionPointLocator.findSuperCandidates(); if (!candidateList.isEmpty()) { return new IntentionAction[]{new RegisterExtensionFix(psiClass, candidateList)}; } return IntentionAction.EMPTY_ARRAY; }
/** * Try to find layout XML file in selected element * * @param element * @return */ public static PsiFile findLayoutResource(PsiElement element) { if (element == null) { return null; // nothing to be used } if (!(element instanceof PsiIdentifier)) { return null; // nothing to be used } PsiElement layout = element.getParent().getFirstChild(); if (layout == null) { return null; // no file to process } if (!"R.layout".equals(layout.getText())) { return null; // not layout file } Project project = element.getProject(); String name = String.format("%s.xml", element.getText()); PsiFile[] files = FilenameIndex.getFilesByName(project, name, new EverythingGlobalScope(project)); if (files.length <= 0) { return null; //no matching files } return files[0]; }
@Override public void visitMethod(@NotNull PsiMethod method) { super.visitMethod(method); if (method.isConstructor() || method.hasModifierProperty(PsiModifier.STATIC)) { return; } final PsiIdentifier nameIdentifier = method.getNameIdentifier(); if (nameIdentifier == null) { return; } final String name = method.getName(); if (isValid(name)) { return; } if (!isOnTheFly()) { if (MethodUtils.hasSuper(method)) { return; } } if (LibraryUtil.isOverrideOfLibraryMethod(method)) { return; } registerMethodError(method, name); }
@Override public void visitMethod(@NotNull PsiMethod method) { if (method.isConstructor()) { return; } final PsiIdentifier nameIdentifier = method.getNameIdentifier(); if (nameIdentifier == null) { return; } final String methodName = method.getName(); if (ignoreIfMethodIsOverride && MethodUtils.hasSuper(method)) { return; } final PsiClass aClass = method.getContainingClass(); if (aClass == null) { return; } final PsiMethod[] methods = aClass.getAllMethods(); for (PsiMethod testMethod : methods) { final String testMethodName = testMethod.getName(); if (!methodName.equals(testMethodName) && methodName.equalsIgnoreCase(testMethodName)) { registerError(nameIdentifier, testMethodName); } } }
public void testIntParameter() throws Exception { myFixture.configureByText("a.java", "import lombok.val;\n" + "abstract class Test {\n" + " private void test() {\n" + " int[] myArray = new int[] {1, 2, 3, 4, 5};\n" + " for(val my<caret>Var: myArray) {" + " }\n" + " } \n" + "}\n"); final PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); assertTrue(elementAtCaret instanceof PsiIdentifier); final PsiElement localParameter = elementAtCaret.getParent(); assertTrue(localParameter.toString(), localParameter instanceof PsiParameter); final PsiType type = ((PsiParameter) localParameter).getType(); assertNotNull(localParameter.toString(), type); assertEquals(type.getCanonicalText(), true, type.equalsToText("int")); }
protected ClsMemberImpl(T stub) { super(stub); myDocComment = !stub.isDeprecated() ? null : new AtomicNotNullLazyValue<PsiDocComment>() { @NotNull @Override protected PsiDocComment compute() { return new ClsDocCommentImpl(ClsMemberImpl.this); } }; myNameIdentifier = new AtomicNotNullLazyValue<PsiIdentifier>() { @NotNull @Override protected PsiIdentifier compute() { return new ClsIdentifierImpl(ClsMemberImpl.this, getName()); } }; }
@Nullable private MyLineMarkerInfo collectMarkerFor(@NotNull PsiElement element) { Optional<PsiField> field = PsiElementUtil.findParentOfTypeOn(element, PsiField.class); if (!(element instanceof PsiIdentifier) || !(element.getParent() instanceof PsiField)) { return null; } List<PsiField> references = allReferencingFields(field.orElseThrow(IllegalArgumentException::new)); if (references.isEmpty()) { return null; } return new MyLineMarkerInfo(element, Icons.JGIVEN, new MarkerType("jgiven", (e) -> "JGiven States", navigatorToElements()), "JGiven States"); }
@Override protected void collectNavigationMarkers(@NotNull PsiElement element, Collection<? super RelatedItemLineMarkerInfo> result) { if (!isConfigFile(element)) { return; } if (lastResult != result) { synchronized (this) { lastResult = result; configClasses = new HashMap<>(); CoffigResolver .from(element.getProject()) .onlyAtTopLevel() .classes() .forEach(match -> configClasses.put(match.getFullPath(), match.getConfigClass())); } } if (element instanceof YAMLKeyValue) { String path = resolvePath(element); if (configClasses != null) { PsiClass configClass = configClasses.get(path); if (configClass != null) { PsiIdentifier nameIdentifier = configClass.getNameIdentifier(); if (nameIdentifier != null) { NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder .create(SeedStackIcons.CLASS) .setTargets(nameIdentifier.getNavigationElement()) .setTooltipText("Navigate to configuration class"); result.add(builder.createLineMarkerInfo(element)); } } } } }
/** * メソッドが変更するフィールド変数(状態)を解析 * @param allFields クラスが所持するすべてのフィールド * @return メソッドが変更する全てのフィールド */ private Map<String, PsiField> parse(@NonNull Map<String, PsiField> allFields) { Map<String, PsiField> writeFields = new HashMap<>(); // メソッド内の全代入式を抽出 List<PsiAssignmentExpression> assignments = ElementUtil.extractAssignments(mMethod); for (PsiAssignmentExpression assignment : assignments) { // 左辺から変数名を取得 List<PsiIdentifier> ids = ElementUtil.extractIdentifiers(assignment.getLExpression()); PsiIdentifier id = ids.get(ids.size()-1); if (allFields.containsKey(id.getText())) { // フィールドと同名のクラス変数やインスタンス変数か否かをチェック boolean isInstance = ElementUtil.isThisExpression(id) || ElementUtil.isRefExpression(id); // フィールドと同名のローカル変数やパラメータか否かをチェック PsiElement element = ElementUtil.ScopeBacktrack .seek(assignment, allFields.get(id.getText()).getNameIdentifier()); if (element != null && !isInstance) continue; writeFields.put(id.getText(), allFields.get(id.getText())); } } return writeFields; }
private static void extractIdentifiers(@NonNull PsiElement element, List<PsiIdentifier> identifiers) { for (PsiElement child : element.getChildren()) { if (child instanceof PsiIdentifier) { identifiers.add((PsiIdentifier) child); continue; } extractIdentifiers(child, identifiers); } }
/** * 識別子の this 式判定 * @param id 識別子ノード * @return 識別子ノードが、this式の名称か否か (true:this式の識別子、false:this式の識別子でない) */ public static boolean isThisExpression(@NonNull PsiIdentifier id) { // 名称がthis式の識別名か否かをチェック List<PsiElement> elderBrothers = ElementUtil.getElderBrother(id); boolean isThisExpr = false; for (PsiElement brother : elderBrothers) { isThisExpr = isThisExpr || (brother instanceof PsiThisExpression) || (brother instanceof PsiParenthesizedExpression); } return isThisExpr; }
/** * 識別子の 参照式 判定 * @param id 識別子ノード * @return 識別子ノードが、参照式(インスタンス要素)の名称か否か * (true:参照式の識別子、false:参照式の識別子でない) */ public static boolean isRefExpression(@NonNull PsiIdentifier id) { // 名称が参照式の識別子か否かをチェック List<PsiElement> elderBrothers = ElementUtil.getElderBrother(id); boolean isInstance = false; for (PsiElement brother : elderBrothers) { isInstance = isInstance || (brother instanceof PsiReferenceExpression); } return isInstance; }
private static void seek(PsiElement element, PsiIdentifier target, Find find) { while(!(element instanceof PsiMethod)) { seekElderBrother(element, target, find); if (!find.isEmpty()) return; element = LintUtils.skipParentheses(element.getParent()); } }