private void verifyExtensionInterfaces( PsiElement element, AnnotationHolder holder ) { if( element instanceof PsiJavaCodeReferenceElementImpl && ((PsiJavaCodeReferenceElementImpl)element).getTreeParent() instanceof ReferenceListElement && ((PsiJavaCodeReferenceElementImpl)element).getTreeParent().getText().startsWith( PsiKeyword.IMPLEMENTS ) ) { final PsiElement resolve = element.getReference().resolve(); if( resolve instanceof PsiExtensibleClass ) { PsiExtensibleClass iface = (PsiExtensibleClass)resolve; if( !isStructuralInterface( iface ) ) { TextRange range = new TextRange( element.getTextRange().getStartOffset(), element.getTextRange().getEndOffset() ); holder.createErrorAnnotation( range, ExtIssueMsg.MSG_ONLY_STRUCTURAL_INTERFACE_ALLOWED_HERE.get( iface.getName() ) ); } } } }
void setScope(String scope) { if (PsiKeyword.PUBLIC.equals(scope)) { myScopeSlider.setValue(1); } else if (PsiKeyword.PROTECTED.equals(scope)) { myScopeSlider.setValue(2); } else if (PsiKeyword.PRIVATE.equals(scope)) { myScopeSlider.setValue(4); } else { myScopeSlider.setValue(3); } handleSlider(); }
@Override @Nullable public UsageTarget[] getTargets(Editor editor, final PsiFile file) { if (editor == null || file == null) return null; PsiElement element = file.findElementAt(TargetElementUtil.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset())); if (element == null) return null; if (element instanceof PsiKeyword && PsiKeyword.THROWS.equals(element.getText())) { return new UsageTarget[]{new PsiElement2UsageTargetAdapter(element)}; } final PsiElement parent = element.getParent(); if (parent instanceof PsiThrowStatement) { return new UsageTarget[] {new PsiElement2UsageTargetAdapter(parent)}; } return null; }
@Override public void visitTryStatement(@NotNull PsiTryStatement statement) { super.visitTryStatement(statement); final PsiCodeBlock finallyBlock = statement.getFinallyBlock(); if (finallyBlock == null) { return; } if (ControlFlowUtils.codeBlockMayCompleteNormally(finallyBlock)) { return; } final PsiElement[] children = statement.getChildren(); for (final PsiElement child : children) { final String childText = child.getText(); if (PsiKeyword.FINALLY.equals(childText)) { registerError(child); return; } } }
@NotNull public DfaTypeValue create(@NotNull PsiType type, boolean nullable) { type = TypeConversionUtil.erasure(type); mySharedInstance.myType = type; mySharedInstance.myCanonicalText = StringUtil.notNullize(type.getCanonicalText(), PsiKeyword.NULL); mySharedInstance.myIsNullable = nullable; String id = mySharedInstance.toString(); ArrayList<DfaTypeValue> conditions = myStringToObject.get(id); if (conditions == null) { conditions = new ArrayList<DfaTypeValue>(); myStringToObject.put(id, conditions); } else { for (DfaTypeValue aType : conditions) { if (aType.hardEquals(mySharedInstance)) return aType; } } DfaTypeValue result = new DfaTypeValue(type, nullable, myFactory, mySharedInstance.myCanonicalText); conditions.add(result); return result; }
@Override @Nullable public UsageTarget[] getTargets(Editor editor, final PsiFile file) { if (editor == null || file == null) return null; PsiElement element = file.findElementAt(TargetElementUtilBase.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset())); if (element == null) return null; if (element instanceof PsiKeyword && PsiKeyword.THROWS.equals(element.getText())) { return new UsageTarget[]{new PsiElement2UsageTargetAdapter(element)}; } final PsiElement parent = element.getParent(); if (parent instanceof PsiThrowStatement) { return new UsageTarget[] {new PsiElement2UsageTargetAdapter(parent)}; } return null; }
@Override public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) { super.visitBinaryExpression(expression); if (!ComparisonUtils.isEqualityComparison(expression)) { return; } final PsiExpression lhs = expression.getLOperand(); if (!ExpressionUtils.hasStringType(lhs)) { return; } final PsiExpression rhs = expression.getROperand(); if (rhs == null || !ExpressionUtils.hasStringType(rhs)) { return; } final String lhsText = lhs.getText(); if (PsiKeyword.NULL.equals(lhsText)) { return; } final String rhsText = rhs.getText(); if (PsiKeyword.NULL.equals(rhsText)) { return; } final PsiJavaToken sign = expression.getOperationSign(); registerError(sign); }
/** * Check MethodCallExpressions for calls for default (argument less) constructor * Produce an error if resolved constructor method is build by lombok and contains some arguments */ @Override public void visitMethodCallExpression(PsiMethodCallExpression methodCall) { super.visitMethodCallExpression(methodCall); PsiExpressionList list = methodCall.getArgumentList(); PsiReferenceExpression referenceToMethod = methodCall.getMethodExpression(); boolean isThisOrSuper = referenceToMethod.getReferenceNameElement() instanceof PsiKeyword; final int parameterCount = list.getExpressions().length; if (isThisOrSuper && parameterCount == 0) { JavaResolveResult[] results = referenceToMethod.multiResolve(true); JavaResolveResult resolveResult = results.length == 1 ? results[0] : JavaResolveResult.EMPTY; PsiElement resolved = resolveResult.getElement(); if (resolved instanceof LombokLightMethodBuilder && ((LombokLightMethodBuilder) resolved).getParameterList().getParameters().length != 0) { holder.registerProblem(methodCall, "Default constructor doesn't exist", ProblemHighlightType.ERROR); } } }
private static boolean stopImportListParsing(PsiBuilder b) { IElementType type = b.getTokenType(); if(IMPORT_LIST_STOPPER_SET.contains(type)) { return true; } if(type == JavaTokenType.IDENTIFIER) { String text = b.getTokenText(); if(PsiKeyword.OPEN.equals(text) || PsiKeyword.MODULE.equals(text)) { return true; } } return false; }
private PsiBuilder.Marker parseStatement(PsiBuilder builder) { String kw = builder.getTokenText(); if(PsiKeyword.REQUIRES.equals(kw)) { return parseRequiresStatement(builder); } if(PsiKeyword.EXPORTS.equals(kw)) { return parseExportsStatement(builder); } if(PsiKeyword.OPENS.equals(kw)) { return parseOpensStatement(builder); } if(PsiKeyword.USES.equals(kw)) { return parseUsesStatement(builder); } if(PsiKeyword.PROVIDES.equals(kw)) { return parseProvidesStatement(builder); } return null; }
@Nullable public DfaValue create(PsiVariable variable) { Object value = variable.computeConstantValue(); PsiType type = variable.getType(); if(value == null) { Boolean boo = computeJavaLangBooleanFieldReference(variable); if(boo != null) { DfaConstValue unboxed = createFromValue(boo, PsiType.BOOLEAN, variable); return myFactory.getBoxedFactory().createBoxed(unboxed); } PsiExpression initializer = variable.getInitializer(); if(initializer instanceof PsiLiteralExpression && initializer.textMatches(PsiKeyword.NULL)) { return dfaNull; } return null; } return createFromValue(value, type, variable); }
@RequiredReadAction @Override public void doCollectInformation(@NotNull ProgressIndicator progressIndicator) { LanguageLevel languageLevel = myFile.getLanguageLevel(); myFile.accept(new JavaRecursiveElementVisitor() { @Override public void visitKeyword(PsiKeyword keyword) { if(JavaLexer.isSoftKeyword(keyword.getNode().getChars(), languageLevel)) { ContainerUtil.addIfNotNull(myResults, HighlightInfo.newHighlightInfo(JavaHighlightInfoTypes.JAVA_KEYWORD).range(keyword).create()); } } }); }
public static boolean isSimplifiableImplicitReturn( PsiIfStatement ifStatement) { if (ifStatement.getElseBranch() != null) { return false; } PsiStatement thenBranch = ifStatement.getThenBranch(); thenBranch = ControlFlowUtils.stripBraces(thenBranch); final PsiElement nextStatement = PsiTreeUtil.skipSiblingsForward(ifStatement, PsiWhiteSpace.class); if (!(nextStatement instanceof PsiStatement)) { return false; } final PsiStatement elseBranch = (PsiStatement)nextStatement; return ConditionalUtils.isReturn(thenBranch, PsiKeyword.TRUE) && ConditionalUtils.isReturn(elseBranch, PsiKeyword.FALSE); }
public static boolean isSimplifiableImplicitReturnNegated( PsiIfStatement ifStatement) { if (ifStatement.getElseBranch() != null) { return false; } PsiStatement thenBranch = ifStatement.getThenBranch(); thenBranch = ControlFlowUtils.stripBraces(thenBranch); final PsiElement nextStatement = PsiTreeUtil.skipSiblingsForward(ifStatement, PsiWhiteSpace.class); if (!(nextStatement instanceof PsiStatement)) { return false; } final PsiStatement elseBranch = (PsiStatement)nextStatement; return ConditionalUtils.isReturn(thenBranch, PsiKeyword.FALSE) && ConditionalUtils.isReturn(elseBranch, PsiKeyword.TRUE); }
@Override public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) { if (before == null) { if (first == last && ElementType.KEYWORD_BIT_SET.contains(first.getElementType())) { anchor = getDefaultAnchor((PsiModifierList)SourceTreeToPsiMap.treeElementToPsi(this), (PsiKeyword)SourceTreeToPsiMap.treeElementToPsi(first)); before = Boolean.TRUE; } } return super.addInternal(first, last, anchor, before); }
@Nullable private static ASTNode getDefaultAnchor(PsiModifierList modifierList, PsiKeyword modifier) { Integer order = ourModifierToOrderMap.get(modifier.getText()); if (order == null) return null; for (ASTNode child = SourceTreeToPsiMap.psiToTreeNotNull(modifierList).getFirstChildNode(); child != null; child = child.getTreeNext()) { if (ElementType.KEYWORD_BIT_SET.contains(child.getElementType())) { Integer order1 = ourModifierToOrderMap.get(child.getText()); if (order1 == null) continue; if (order1.intValue() > order.intValue()) { return child; } } } return null; }
public NullSmartCompletionContributor() { extend(CompletionType.SMART, and(JavaSmartCompletionContributor.INSIDE_EXPRESSION, not(psiElement().afterLeaf("."))), new ExpectedTypeBasedCompletionProvider() { @Override protected void addCompletions(final CompletionParameters parameters, final CompletionResultSet result, final Collection<ExpectedTypeInfo> infos) { if (!StringUtil.startsWithChar(result.getPrefixMatcher().getPrefix(), 'n')) { return; } LinkedHashSet<CompletionResult> results = result.runRemainingContributors(parameters, true); for (CompletionResult completionResult : results) { if (completionResult.isStartMatch()) { return; } } for (final ExpectedTypeInfo info : infos) { if (!(info.getType() instanceof PsiPrimitiveType)) { final LookupElement item = BasicExpressionCompletionContributor.createKeywordLookupItem(parameters.getPosition(), PsiKeyword.NULL); result.addElement(JavaSmartCompletionContributor.decorate(item, infos)); return; } } } }); }
@Override public HighlightUsagesHandlerBase createHighlightUsagesHandler(@NotNull Editor editor, @NotNull PsiFile file, @NotNull PsiElement target) { if (target instanceof PsiKeyword) { if (PsiKeyword.RETURN.equals(target.getText()) || PsiKeyword.THROW.equals(target.getText())) { return new HighlightExitPointsHandler(editor, file, target); } } return null; }
public ChangeExtendsToImplementsFix(@NotNull PsiClass aClass, @NotNull PsiClassType classToExtendFrom) { super(aClass, classToExtendFrom, true); myName = myClassToExtendFrom == null ? getFamilyName() : QuickFixBundle.message("exchange.extends.implements.keyword", aClass.isInterface() == myClassToExtendFrom.isInterface() ? PsiKeyword.IMPLEMENTS : PsiKeyword.EXTENDS, aClass.isInterface() == myClassToExtendFrom.isInterface() ? PsiKeyword.EXTENDS : PsiKeyword.IMPLEMENTS, myClassToExtendFrom.getName()); }
@Override public boolean canSelect(PsiElement e) { if (e instanceof PsiKeyword) { return true; } if (e instanceof PsiJavaToken) { IElementType tokenType = ((PsiJavaToken)e).getTokenType(); return tokenType == JavaTokenType.IDENTIFIER || tokenType == JavaTokenType.STRING_LITERAL; } return false; }
@Override public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { List<TextRange> result = new ArrayList<TextRange>(); result.addAll(expandToWholeLine(editorText, e.getTextRange(), false)); PsiIfStatement statement = (PsiIfStatement)e; final PsiKeyword elseKeyword = statement.getElseElement(); if (elseKeyword != null) { final PsiStatement then = statement.getThenBranch(); if (then != null) { final TextRange thenRange = new TextRange(statement.getTextRange().getStartOffset(), then.getTextRange().getEndOffset()); if (thenRange.contains(cursorOffset)) { result.addAll(expandToWholeLine(editorText, thenRange, false)); } } result.addAll(expandToWholeLine(editorText, new TextRange(elseKeyword.getTextRange().getStartOffset(), statement.getTextRange().getEndOffset()), false)); final PsiStatement branch = statement.getElseBranch(); if (branch instanceof PsiIfStatement) { PsiIfStatement elseIf = (PsiIfStatement)branch; final PsiKeyword element = elseIf.getElseElement(); if (element != null) { final PsiStatement elseThen = elseIf.getThenBranch(); if (elseThen != null) { result.addAll(expandToWholeLine(editorText, new TextRange(elseKeyword.getTextRange().getStartOffset(), elseThen.getTextRange().getEndOffset()), false)); } } } } return result; }
String getScope() { switch (myScopeSlider.getValue()) { case 1: return PsiKeyword.PUBLIC; case 2: return PsiKeyword.PROTECTED; case 3: return PsiKeyword.PACKAGE; case 4: return PsiKeyword.PRIVATE; default: return null; } }
public static LookupElement createLookupElement(final PsiMethod method, final @Nullable TIntObjectHashMap<SubLookupElement> replaceElements) { if (method.isConstructor()) { //noinspection ConstantConditions return LookupElementBuilder.create(String.format("%s %s", PsiKeyword.NEW, method.getContainingClass().getName())); } else if (method.hasModifierProperty(PsiModifier.STATIC)) { return new ChainCompletionMethodCallLookupElement(method, replaceElements, false, true); } else { return new ChainCompletionMethodCallLookupElement(method, replaceElements); } }
public void generateComponentLayout(final LwComponent component, final FormSourceCodeGenerator generator, final String variable, final String parentVariable) { final LwTabbedPane.Constraints tabConstraints = (LwTabbedPane.Constraints)component.getCustomLayoutConstraints(); if (tabConstraints == null){ throw new IllegalArgumentException("tab constraints cannot be null: " + component.getId()); } generator.startMethodCall(parentVariable, "addTab"); generator.push(tabConstraints.myTitle); if (tabConstraints.myIcon != null || tabConstraints.myToolTip != null) { if (tabConstraints.myIcon == null) { generator.pushVar(PsiKeyword.NULL); } else { generator.pushIcon(tabConstraints.myIcon); } } generator.pushVar(variable); if (tabConstraints.myToolTip != null) { generator.push(tabConstraints.myToolTip); } generator.endMethod(); int index = component.getParent().indexOfComponent(component); if (tabConstraints.myDisabledIcon != null) { generator.startMethodCall(parentVariable, "setDisabledIconAt"); generator.push(index); generator.pushIcon(tabConstraints.myDisabledIcon); generator.endMethod(); } if (!tabConstraints.myEnabled) { generator.startMethodCall(parentVariable, "setEnabledAt"); generator.push(index); generator.push(tabConstraints.myEnabled); generator.endMethod(); } }
public NullSmartCompletionContributor() { extend(CompletionType.SMART, and(JavaSmartCompletionContributor.INSIDE_EXPRESSION, not(psiElement().afterLeaf("."))), new ExpectedTypeBasedCompletionProvider() { @Override protected void addCompletions(final CompletionParameters parameters, final CompletionResultSet result, final Collection<ExpectedTypeInfo> infos) { if (!StringUtil.startsWithChar(result.getPrefixMatcher().getPrefix(), 'n')) { return; } LinkedHashSet<CompletionResult> results = result.runRemainingContributors(parameters, true); for (CompletionResult completionResult : results) { if (completionResult.isStartMatch()) { return; } } for (final ExpectedTypeInfo info : infos) { if (!(info.getType() instanceof PsiPrimitiveType)) { final LookupItem item = (LookupItem)BasicExpressionCompletionContributor.createKeywordLookupItem(parameters.getPosition(), PsiKeyword.NULL); item.setAttribute(LookupItem.TYPE, PsiType.NULL); result.addElement(JavaSmartCompletionContributor.decorate(item, infos)); return; } } } }); }
@Override public HighlightUsagesHandlerBase createHighlightUsagesHandler(final Editor editor, final PsiFile file) { int offset = TargetElementUtilBase.adjustOffset(file, editor.getDocument(), editor.getCaretModel().getOffset()); PsiElement target = file.findElementAt(offset); if (target instanceof PsiKeyword) { if (PsiKeyword.RETURN.equals(target.getText()) || PsiKeyword.THROW.equals(target.getText())) { return new HighlightExitPointsHandler(editor, file, target); } } return null; }
public ChangeExtendsToImplementsFix(PsiClass aClass, PsiClassType classToExtendFrom) { super(aClass, classToExtendFrom, true); myName = QuickFixBundle.message("exchange.extends.implements.keyword", aClass.isInterface() == myClassToExtendFrom.isInterface() ? PsiKeyword.IMPLEMENTS : PsiKeyword.EXTENDS, aClass.isInterface() == myClassToExtendFrom.isInterface() ? PsiKeyword.EXTENDS : PsiKeyword.IMPLEMENTS, myClassToExtendFrom.getName()); }
@Override public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) { List<TextRange> result = new ArrayList<TextRange>(); result.addAll(expandToWholeLine(editorText, e.getTextRange(), false)); PsiIfStatement statement = (PsiIfStatement)e; final PsiKeyword elseKeyword = statement.getElseElement(); if (elseKeyword != null) { result.addAll(expandToWholeLine(editorText, new TextRange(elseKeyword.getTextRange().getStartOffset(), statement.getTextRange().getEndOffset()), false)); final PsiStatement branch = statement.getElseBranch(); if (branch instanceof PsiIfStatement) { PsiIfStatement elseIf = (PsiIfStatement)branch; final PsiKeyword element = elseIf.getElseElement(); if (element != null) { result.addAll(expandToWholeLine(editorText, new TextRange(elseKeyword.getTextRange().getStartOffset(), elseIf.getThenBranch().getTextRange().getEndOffset()), false)); } } } return result; }
@NotNull public static String getReturnValueOfType(@Nullable PsiType type) { if (type instanceof PsiPrimitiveType) { if (PsiType.BOOLEAN.equals(type)) { return PsiKeyword.FALSE; } else { return "0"; } } else if (PsiType.VOID.equals(type)) { return ""; } else { return PsiKeyword.NULL; } }