Search in sources :

Example 16 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project generator by mybatis.

the class ExistingJavaFileVisitor method isGeneratedAnnotation.

@SuppressWarnings("unchecked")
private boolean isGeneratedAnnotation(Annotation annotation) {
    String typeName = annotation.getTypeName().getFullyQualifiedName();
    if (isGeneratedType(typeName)) {
        if (annotation.isNormalAnnotation()) {
            NormalAnnotation normalAnnotation = (NormalAnnotation) annotation;
            List<MemberValuePair> values = normalAnnotation.values();
            for (MemberValuePair pair : values) {
                String name = pair.getName().getFullyQualifiedName();
                String value = null;
                Expression exp = pair.getValue();
                if (exp instanceof StringLiteral) {
                    value = ((StringLiteral) exp).getLiteralValue();
                }
                if (MyBatisGenerator.class.getName().equals(value) && "value".equals(name)) {
                    return true;
                }
            }
        } else if (annotation.isSingleMemberAnnotation()) {
            SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation) annotation;
            Expression exp = singleMemberAnnotation.getValue();
            String value = null;
            if (exp instanceof StringLiteral) {
                value = ((StringLiteral) exp).getLiteralValue();
            }
            if (MyBatisGenerator.class.getName().equals(value)) {
                return true;
            }
        }
    }
    return false;
}
Also used : MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) SingleMemberAnnotation(org.eclipse.jdt.core.dom.SingleMemberAnnotation) NormalAnnotation(org.eclipse.jdt.core.dom.NormalAnnotation)

Example 17 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project sts4 by spring-projects.

the class WebfluxPathFinder method visit.

@Override
public boolean visit(MethodInvocation node) {
    boolean visitChildren = true;
    if (node != this.root) {
        IMethodBinding methodBinding = node.resolveMethodBinding();
        try {
            if (WebfluxUtils.REQUEST_PREDICATES_TYPE.equals(methodBinding.getDeclaringClass().getBinaryName())) {
                String name = methodBinding.getName();
                if (name != null && WebfluxUtils.REQUEST_PREDICATE_ALL_PATH_METHODS.contains(name)) {
                    StringLiteral stringLiteral = WebfluxUtils.extractStringLiteralArgument(node);
                    if (stringLiteral != null) {
                        Range range = doc.toRange(stringLiteral.getStartPosition(), stringLiteral.getLength());
                        path.add(new WebfluxRouteElement(stringLiteral.getLiteralValue(), range));
                    }
                }
            }
        } catch (BadLocationException e) {
        // ignore
        }
        if (WebfluxUtils.isRouteMethodInvocation(methodBinding)) {
            visitChildren = false;
        }
    }
    return visitChildren;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Range(org.eclipse.lsp4j.Range) BadLocationException(org.springframework.ide.vscode.commons.util.BadLocationException)

Example 18 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project sts4 by spring-projects.

the class ScopeCompletionProcessor method provideCompletions.

@Override
public Collection<ICompletionProposal> provideCompletions(ASTNode node, Annotation annotation, ITypeBinding type, int offset, IDocument doc) {
    List<ICompletionProposal> result = new ArrayList<>();
    try {
        if (node instanceof SimpleName && node.getParent() instanceof MemberValuePair) {
            MemberValuePair memberPair = (MemberValuePair) node.getParent();
            // case: @Scope(value=<*>)
            if ("value".equals(memberPair.getName().toString()) && memberPair.getValue().toString().equals("$missing$")) {
                for (ScopeNameCompletion completion : ScopeNameCompletionProposal.COMPLETIONS) {
                    ICompletionProposal proposal = new ScopeNameCompletionProposal(completion, doc, offset, offset, "");
                    result.add(proposal);
                }
            }
        } else // case: @Scope(<*>)
        if (node == annotation && doc.get(offset - 1, 2).endsWith("()")) {
            for (ScopeNameCompletion completion : ScopeNameCompletionProposal.COMPLETIONS) {
                ICompletionProposal proposal = new ScopeNameCompletionProposal(completion, doc, offset, offset, "");
                result.add(proposal);
            }
        } else if (node instanceof StringLiteral && node.getParent() instanceof Annotation) {
            // case: @Scope("...")
            if (node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
                String prefix = doc.get(node.getStartPosition(), offset - node.getStartPosition());
                for (ScopeNameCompletion completion : ScopeNameCompletionProposal.COMPLETIONS) {
                    if (completion.getValue().startsWith(prefix)) {
                        ICompletionProposal proposal = new ScopeNameCompletionProposal(completion, doc, node.getStartPosition(), node.getStartPosition() + node.getLength(), prefix);
                        result.add(proposal);
                    }
                }
            }
        } else if (node instanceof StringLiteral && node.getParent() instanceof MemberValuePair) {
            MemberValuePair memberPair = (MemberValuePair) node.getParent();
            // case: @Scope(value=<*>)
            if ("value".equals(memberPair.getName().toString()) && node.toString().startsWith("\"") && node.toString().endsWith("\"")) {
                String prefix = doc.get(node.getStartPosition(), offset - node.getStartPosition());
                for (ScopeNameCompletion completion : ScopeNameCompletionProposal.COMPLETIONS) {
                    if (completion.getValue().startsWith(prefix)) {
                        ICompletionProposal proposal = new ScopeNameCompletionProposal(completion, doc, node.getStartPosition(), node.getStartPosition() + node.getLength(), prefix);
                        result.add(proposal);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
Also used : MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) Annotation(org.eclipse.jdt.core.dom.Annotation)

Example 19 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project sts4 by spring-projects.

the class ActiveProfilesProvider method getLiveHoverHints.

@Override
public Collection<Range> getLiveHoverHints(Annotation annotation, TextDocument doc, SpringBootApp[] runningApps) {
    if (runningApps.length > 0) {
        Builder<Range> ranges = ImmutableList.builder();
        nameRange(doc, annotation).ifPresent(ranges::add);
        Set<String> allActiveProfiles = getAllActiveProfiles(runningApps);
        annotation.accept(new ASTVisitor() {

            @Override
            public boolean visit(StringLiteral node) {
                String value = ASTUtils.getLiteralValue(node);
                if (value != null && allActiveProfiles.contains(value)) {
                    rangeOf(doc, node).ifPresent(ranges::add);
                }
                return true;
            }
        });
        return ranges.build();
    }
    return ImmutableList.of();
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ASTUtils.nameRange(org.springframework.ide.vscode.boot.java.utils.ASTUtils.nameRange) Range(org.eclipse.lsp4j.Range) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 20 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project eclipse-cs by checkstyle.

the class StringLiteralEqualityQuickfix method handleGetCorrectingASTVisitor.

/**
 * {@inheritDoc}
 */
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartPosition) {
    return new ASTVisitor() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean visit(InfixExpression node) {
            if (containsPosition(lineInfo, node.getStartPosition())) {
                StringLiteral literal = null;
                Expression otherOperand = null;
                if (node.getLeftOperand() instanceof StringLiteral) {
                    literal = (StringLiteral) node.getLeftOperand();
                    otherOperand = node.getRightOperand();
                } else if (node.getRightOperand() instanceof StringLiteral) {
                    literal = (StringLiteral) node.getRightOperand();
                    otherOperand = node.getLeftOperand();
                } else {
                    return true;
                }
                Expression replacementNode = null;
                MethodInvocation equalsInvocation = node.getAST().newMethodInvocation();
                // $NON-NLS-1$
                equalsInvocation.setName(node.getAST().newSimpleName("equals"));
                equalsInvocation.setExpression((Expression) ASTNode.copySubtree(node.getAST(), literal));
                equalsInvocation.arguments().add(ASTNode.copySubtree(node.getAST(), otherOperand));
                // expression
                if (node.getOperator().equals(InfixExpression.Operator.NOT_EQUALS)) {
                    PrefixExpression prefixExpression = node.getAST().newPrefixExpression();
                    prefixExpression.setOperator(PrefixExpression.Operator.NOT);
                    prefixExpression.setOperand(equalsInvocation);
                    replacementNode = prefixExpression;
                } else {
                    replacementNode = equalsInvocation;
                }
                replaceNode(node, replacementNode);
            }
            return true;
        }

        /**
         * Replaces the given node with the replacement node (using reflection
         * since I am not aware of a proper API to do this).
         *
         * @param node
         *          the node to replace
         * @param replacementNode
         *          the replacement
         */
        private void replaceNode(ASTNode node, ASTNode replacementNode) {
            try {
                if (node.getLocationInParent().isChildProperty()) {
                    String property = node.getLocationInParent().getId();
                    String capitalizedProperty = property.substring(0, 1).toUpperCase() + property.substring(1);
                    String setterMethodName = "set" + capitalizedProperty;
                    Class<?> testClass = node.getClass();
                    while (testClass != null) {
                        try {
                            Method setterMethod = node.getParent().getClass().getMethod(setterMethodName, testClass);
                            setterMethod.invoke(node.getParent(), replacementNode);
                            break;
                        } catch (NoSuchMethodException e) {
                            testClass = testClass.getSuperclass();
                        }
                    }
                } else if (node.getLocationInParent().isChildListProperty()) {
                    Method listMethod = node.getParent().getClass().getMethod(node.getLocationInParent().getId(), (Class<?>[]) null);
                    @SuppressWarnings("unchecked") List<ASTNode> list = (List<ASTNode>) listMethod.invoke(node.getParent(), (Object[]) null);
                    list.set(list.indexOf(node), replacementNode);
                }
            } catch (InvocationTargetException e) {
                CheckstyleLog.log(e);
            } catch (IllegalAccessException e) {
                CheckstyleLog.log(e);
            } catch (NoSuchMethodException e) {
                CheckstyleLog.log(e);
            }
        }
    };
}
Also used : MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) List(java.util.List)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)54 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)20 Expression (org.eclipse.jdt.core.dom.Expression)16 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 CastExpression (org.eclipse.jdt.core.dom.CastExpression)11 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)10 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 AST (org.eclipse.jdt.core.dom.AST)7 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)7 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)6 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 ArrayList (java.util.ArrayList)5 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 Name (org.eclipse.jdt.core.dom.Name)5