@Override public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) { registrar.registerReferenceProvider(PlatformPatterns.psiElement(PsiLiteralExpression.class), new PsiReferenceProvider() { @NotNull @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) { if (isLiteralOfConfigurationAnnotation(element) || isCoffigMethod(element)) { PsiLiteralExpression literalExpression = (PsiLiteralExpression) element; String value = literalExpression.getValue() instanceof String ? (String) literalExpression.getValue() : null; List<PsiReference> psiReferences = resolvePsiReferences(element, value, 1); return psiReferences.toArray(new PsiReference[psiReferences.size()]); } return PsiReference.EMPTY_ARRAY; } }); }
@Override public Optional<String> extractTextFromElement(PsiElement element, boolean concatString, boolean stripWhitespace) { if (element instanceof PsiLiteralExpression) { // need the entire line so find the literal expression that would hold the entire string (java) PsiLiteralExpression literal = (PsiLiteralExpression) element; Object o = literal.getValue(); String text = o != null ? o.toString() : null; if (text == null) { return Optional.empty(); } if (concatString) { final PsiPolyadicExpression parentOfType = PsiTreeUtil.getParentOfType(element, PsiPolyadicExpression.class); if (parentOfType != null) { text = parentOfType.getText(); } } // unwrap literal string which can happen in java too if (stripWhitespace) { return Optional.ofNullable(getInnerText(text)); } return Optional.of(StringUtil.unquoteString(text.replace(QUOT, "\""))); } return Optional.empty(); }
public void testGenerateDoc() throws Exception { myFixture.configureByText(JavaFileType.INSTANCE, getJavaTestDataWithCursorAfterQuestionMark()); PsiElement element = myFixture.findElementByText("\"file:inbox?\"", PsiLiteralExpression.class); String componentName = "file"; String lookup = componentName + ":inbox?delete"; PsiManager manager = myFixture.getPsiManager(); PsiElement docInfo = new CamelDocumentationProvider().getDocumentationElementForLookupItem(manager, lookup, element); String doc = new CamelDocumentationProvider().generateDoc(docInfo, null); assertEquals(readExpectedFile(), doc); String documentation = new CamelDocumentationProvider().generateDoc(element, null); assertNotNull(documentation); assertTrue(documentation.startsWith("<b>File Component</b><br/>The file component is used for reading or writing files.<br/>")); }
@Override public void tokenize(@NotNull PsiLiteralExpression element, TokenConsumer consumer) { PsiLiteralExpressionImpl literalExpression = (PsiLiteralExpressionImpl)element; if (literalExpression.getLiteralElementType() != JavaTokenType.STRING_LITERAL) return; // not a string literal String text = literalExpression.getInnerText(); if (StringUtil.isEmpty(text) || text.length() <= 2) { // optimisation to avoid expensive injection check return; } if (InjectedLanguageUtil.hasInjections(literalExpression)) return; final PsiModifierListOwner listOwner = PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class); if (listOwner != null && AnnotationUtil.isAnnotated(listOwner, Collections.singleton(AnnotationUtil.NON_NLS), false, false)) { return; } if (!text.contains("\\")) { consumer.consumeToken(element, PlainTextSplitter.getInstance()); } else { processTextWithEscapeSequences(element, text, consumer); } }
@NotNull @Override public Tokenizer getTokenizer(PsiElement element) { if (element instanceof PsiMethod) { return myMethodNameTokenizer; } if (element instanceof PsiDocComment) { return myDocCommentTokenizer; } if (element instanceof PsiLiteralExpression) { if (SuppressManager.isSuppressedInspectionName((PsiLiteralExpression)element)) { return EMPTY_TOKENIZER; } return myLiteralExpressionTokenizer; } if (element instanceof PsiNamedElement) { return myNamedElementTokenizer; } return super.getTokenizer(element); }
public static boolean isPowerOfTwo(PsiExpression rhs) { if (!(rhs instanceof PsiLiteralExpression)) { return false; } final PsiLiteralExpression literal = (PsiLiteralExpression)rhs; final Object value = literal.getValue(); if (!(value instanceof Number)) { return false; } if (value instanceof Double || value instanceof Float) { return false; } int intValue = ((Number)value).intValue(); if (intValue <= 1) { return false; } while (intValue % 2 == 0) { intValue >>= 1; } return intValue == 1; }
@Override public void visitLiteralExpression(@NotNull PsiLiteralExpression expression) { super.visitLiteralExpression(expression); final PsiType type = expression.getType(); if (type == null || !TypeUtils.isJavaLangString(type) && !type.equals(PsiType.CHAR)) { return; } final String text = expression.getText(); final Matcher matcher = newlines.matcher(text); int end = 0; while (matcher.find(end)) { final int start = matcher.start(); end = matcher.end(); registerErrorAtOffset(expression, start, end - start); } }
@Override public void visitLiteralExpression( @NotNull PsiLiteralExpression expression) { super.visitLiteralExpression(expression); final PsiType type = expression.getType(); if (type == null) { return; } if (!type.equals(PsiType.CHAR)) { return; } final String text = expression.getText(); if (text == null) { return; } if (text.equals(" ")) { return; } if (ExpressionUtils.isDeclaredConstant(expression)) { return; } if (NonNlsUtils.isNonNlsAnnotatedUse(expression)) { return; } registerError(expression); }
@Override public void visitArrayInitializerExpression( PsiArrayInitializerExpression expression) { super.visitArrayInitializerExpression(expression); final PsiExpression[] initializers = expression.getInitializers(); boolean hasDecimalLiteral = false; boolean hasOctalLiteral = false; for (final PsiExpression initializer : initializers) { if (initializer instanceof PsiLiteralExpression) { final PsiLiteralExpression literal = (PsiLiteralExpression)initializer; if (isDecimalLiteral(literal)) { hasDecimalLiteral = true; } if (isOctalLiteral(literal)) { hasOctalLiteral = true; } } } if (hasOctalLiteral && hasDecimalLiteral) { registerError(expression); } }
@Override public void visitLiteralExpression( @NotNull PsiLiteralExpression expression) { super.visitLiteralExpression(expression); final PsiType type = expression.getType(); if (type == null) { return; } if (!type.equals(PsiType.LONG)) { return; } final String text = expression.getText(); if (text == null) { return; } final int length = text.length(); if (length == 0) { return; } if (text.charAt(length - 1) != 'l') { return; } registerError(expression); }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiLiteralExpression)) { return false; } final PsiLiteralExpression expression = (PsiLiteralExpression)element; final PsiType type = expression.getType(); if (!PsiType.DOUBLE.equals(type) && !PsiType.FLOAT.equals(type)) { return false; } String text = expression.getText(); if (text == null) { return false; } text = text.toLowerCase(); if (text.startsWith("-")) { text = text.substring(1); } if (!text.contains(".") && text.startsWith("0")) { return false; //Octal integer } return !text.contains("e"); }
@Override public boolean satisfiedBy(final PsiElement element) { if (!(element instanceof PsiLiteralExpression)) { return false; } if (!PsiUtil.isLanguageLevel7OrHigher(element)) { return false; } final PsiType type = ((PsiLiteralExpression)element).getType(); if (PsiType.INT.equals(type) || PsiType.LONG.equals(type)) { final String text = element.getText(); return !(text.startsWith("0b") || text.startsWith("0B")); } return false; }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiLiteralExpression)) { return false; } final PsiLiteralExpression expression = (PsiLiteralExpression)element; final PsiType type = expression.getType(); if (!(PsiType.INT.equals(type) || PsiType.LONG.equals(type))) { return false; } @NonNls final String text = expression.getText(); if (text.charAt(0) != '0') { return true; } if (text.length() < 2) { return true; } final char c1 = text.charAt(1); if (c1 != '_' && (c1 < '0' || c1 > '7')) { return true; } return false; }
public static boolean isPowerOfTwo(PsiExpression rhs) { if (!(rhs instanceof PsiLiteralExpression)) { return false; } final PsiLiteralExpression literal = (PsiLiteralExpression)rhs; final Object value = literal.getValue(); if (!(value instanceof Number)) { return false; } if (value instanceof Double || value instanceof Float) { return false; } int intValue = ((Number)value).intValue(); if (intValue <= 0) { return false; } while (intValue % 2 == 0) { intValue >>= 1; } return intValue == 1; }
/** * Extract full node type text (i.e. namespace:nodeTypeName) from element, based on element class/type */ private String getNodeTypeText(@NotNull PsiElement element) { if (element instanceof PsiLiteralExpression) { //Java PsiLiteralExpression literalExpression = (PsiLiteralExpression) element; return literalExpression.getValue() instanceof String ? (String) literalExpression.getValue() : null; } else if (element instanceof CndSuperType) { //Cnd super types return element.getText(); } else if (element instanceof CndExtension) { //Cnd extends types return element.getText(); } else if (element instanceof CndSubNodeType) { //Cnd subnode types return element.getText(); } else if (element instanceof CndSubNodeDefaultType) { //Cnd subnode default type return element.getText(); } else if (element instanceof XmlAttributeValue) { //XML return ((XmlAttributeValue) element).getValue(); } return null; }
private int getOffset(@NotNull PsiElement element) { if (element instanceof PsiLiteralExpression) { //Java return 1; } else if (element instanceof CndSuperType) { //Cnd super types return 0; } else if (element instanceof CndExtension) { //Cnd extends types return 0; } else if (element instanceof CndSubNodeType) { //Cnd subnode types return 0; } else if (element instanceof CndSubNodeDefaultType) { //Cnd subnode default type return 0; } else if (element instanceof XmlAttributeValue) { //XML return 1; } return 0; }
@Override public void visitLiteralExpression( @NotNull PsiLiteralExpression literal) { super.visitLiteralExpression(literal); final PsiType type = literal.getType(); if (type == null) { return; } if (!(type.equals(PsiType.FLOAT) || type.equals(PsiType.DOUBLE))) { return; } final String text = literal.getText(); if (text == null) { return; } if (!isConfusing(text)) { return; } registerError(literal); }
@Override public void tokenize(@NotNull PsiLiteralExpression element, TokenConsumer consumer) { PsiLiteralExpressionImpl literalExpression = (PsiLiteralExpressionImpl) element; if (literalExpression.getLiteralElementType() != JavaTokenType.STRING_LITERAL) { return; // not a string literal } final PsiModifierListOwner listOwner = PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class); if (listOwner != null && AnnotationUtil.isAnnotated(listOwner, Collections.singleton(AnnotationUtil.NON_NLS), false, false)) { return; } String text = literalExpression.getInnerText(); if (text == null) { return; } if (!text.contains("\\")) { consumer.consumeToken(element, PlainTextSplitter.getInstance()); } else { processTextWithEscapeSequences(element, text, consumer); } }
@NotNull @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) { final PsiLiteralExpression literalExpression = (PsiLiteralExpression) element; final String value = (String) literalExpression.getValue(); final String text; if (value == null) { text = ""; } else { text = value.replace(Util.INTELLIJ_MAGIC_STRING, ""); } return new PsiReference[] { new DataFieldReference(false, literalExpression, TextRange.create(1, text.length() + 1)) }; }
@Override public boolean accepts(@Nullable Object o, ProcessingContext context) { if (o instanceof PsiLiteralExpression) { final PsiAnnotation parentOfType = PsiTreeUtil.getParentOfType((PsiLiteralExpression) o, PsiAnnotation.class); if (parentOfType != null && type.equals(parentOfType.getQualifiedName())) { if (attributeName != null) { final PsiNameValuePair nvp = PsiTreeUtil.getParentOfType((PsiLiteralExpression) o, PsiNameValuePair.class); return nvp != null && attributeName.equals(nvp.getName()); } else { return true; } } } return false; }
@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); }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiLiteralExpression)) { return false; } final PsiLiteralExpression expression = (PsiLiteralExpression)element; final PsiType type = expression.getType(); if (!PsiType.DOUBLE.equals(type) && !PsiType.FLOAT.equals(type)) { return false; } String text = expression.getText(); if (text == null) { return false; } text = text.toLowerCase(); if (text.startsWith("-")) { text = text.substring(1); } if (!text.contains("") && text.startsWith("0")) { return false; //Octal integer } return !text.contains("e"); }