Search in sources :

Example 21 with SearchResult

use of com.liferay.blade.api.SearchResult in project liferay-ide by liferay.

the class JavaFileJDT method findServiceAPIs.

@Override
public List<SearchResult> findServiceAPIs(String[] serviceFQNPrefixes) {
    List<SearchResult> searchResults = new ArrayList<>();
    for (String prefix : serviceFQNPrefixes) {
        for (String suffix : _SERVICE_API_SUFFIXES) {
            String serviceFQN = prefix + suffix;
            SearchResult importResult = findImport(serviceFQN);
            if (importResult != null) {
                searchResults.add(importResult);
            }
            String service = serviceFQN.substring(serviceFQN.lastIndexOf('.') + 1, serviceFQN.length());
            searchResults.addAll(findMethodInvocations(null, service, "*", null));
            searchResults.addAll(findMethodInvocations(service, null, "*", null));
        }
    }
    return searchResults;
}
Also used : ArrayList(java.util.ArrayList) SearchResult(com.liferay.blade.api.SearchResult)

Example 22 with SearchResult

use of com.liferay.blade.api.SearchResult in project liferay-ide by liferay.

the class JavaFileJDT method findMethodDeclaration.

@Override
public List<SearchResult> findMethodDeclaration(String name, String[] params, String returnType) {
    List<SearchResult> searchResults = new ArrayList<>();
    _ast.accept(new ASTVisitor() {

        @Override
        public boolean visit(MethodDeclaration node) {
            boolean sameParmSize = true;
            boolean sameReturnType = true;
            if (returnType != null) {
                Type type = node.getReturnType2();
                if (type != null) {
                    String returnTypeName = type.resolveBinding().getName();
                    if (!returnTypeName.equals(returnType)) {
                        sameReturnType = false;
                    }
                } else {
                    sameReturnType = false;
                }
            }
            String methodName = node.getName().toString();
            List<?> parmsList = node.parameters();
            if (name.equals(methodName) && (params.length == parmsList.size())) {
                for (int i = 0; i < params.length; i++) {
                    Object x = parmsList.get(i);
                    if (!(params[i].trim().equals(x.toString().substring(0, params[i].trim().length())))) {
                        sameParmSize = false;
                        break;
                    }
                }
            } else {
                sameParmSize = false;
            }
            if (sameParmSize && sameReturnType) {
                int startLine = _ast.getLineNumber(node.getName().getStartPosition());
                int startOffset = node.getName().getStartPosition();
                node.accept(new ASTVisitor() {

                    @Override
                    public boolean visit(Block node) {
                        // SimpleName parent can not be MarkerAnnotation and
                        // SimpleType
                        // SingleVariableDeclaration node contains the
                        // parms's type
                        int endLine = _ast.getLineNumber(node.getStartPosition());
                        int endOffset = node.getStartPosition();
                        searchResults.add(createSearchResult(null, startOffset, endOffset, startLine, endLine, true));
                        return false;
                    }
                });
            }
            return false;
        }
    });
    return searchResults;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ArrayList(java.util.ArrayList) Block(org.eclipse.jdt.core.dom.Block) SearchResult(com.liferay.blade.api.SearchResult) ArrayList(java.util.ArrayList) List(java.util.List) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 23 with SearchResult

use of com.liferay.blade.api.SearchResult in project liferay-ide by liferay.

the class JavaFileJDT method findMethodInvocations.

/**
 * find the method invocations for a particular method on a given type or
 * expression
 *
 * @param typeHint
 *            the type hint to use when matching expressions
 * @param expressionValue
 *            the expression only value (no type hint)
 * @param methodName
 *            the method name
 * @return search results
 */
@Override
@SuppressWarnings("unchecked")
public List<SearchResult> findMethodInvocations(String typeHint, String expressionValue, String methodName, String[] methodParamTypes) {
    List<SearchResult> searchResults = new ArrayList<>();
    _ast.accept(new ASTVisitor() {

        @Override
        public boolean visit(MethodInvocation node) {
            String methodNameValue = node.getName().toString();
            Expression expression = node.getExpression();
            ITypeBinding type = null;
            if (expression != null) {
                try {
                    type = _typeCache.get(expression).orElse(null);
                } catch (ExecutionException ee) {
                }
            }
            if (((methodName.equals(methodNameValue)) || ("*".equals(methodName))) && ((typeHint != null && type != null && type.getName().endsWith(typeHint)) || (typeHint == null && expression != null && expression.toString().equals(expressionValue)))) {
                boolean argumentsMatch = false;
                if (methodParamTypes != null) {
                    Expression[] argExpressions = ((List<Expression>) node.arguments()).toArray(new Expression[0]);
                    if (argExpressions.length == methodParamTypes.length) {
                        // args number matched
                        boolean possibleMatch = true;
                        // assume all types will match until we find
                        // otherwise
                        boolean typeMatched = true;
                        boolean typeUnresolved = false;
                        for (int i = 0; i < argExpressions.length; i++) {
                            Expression arg = argExpressions[i];
                            ITypeBinding argType = arg.resolveTypeBinding();
                            if (argType != null) {
                                if (_typeMatch(methodParamTypes[i], argType.getQualifiedName())) {
                                    continue;
                                } else {
                                    // type unmatched
                                    possibleMatch = false;
                                    typeMatched = false;
                                    break;
                                }
                            } else {
                                possibleMatch = false;
                                // there are two cases :
                                // typeUnresolved : means that all resolved
                                // type is matched and there is unsolved
                                // type , need to set fullMatch false
                                // typeUnmatched : means that some resolved
                                // type is unmatched , no need to add
                                // SearchResult
                                // do not add searchResults now, just record
                                // the state and continue
                                // because there maybe unmatched type later
                                // which will break this case
                                typeUnresolved = true;
                            }
                        }
                        if (typeMatched && typeUnresolved) {
                            int startOffset = expression.getStartPosition();
                            int startLine = _ast.getLineNumber(startOffset);
                            int endOffset = node.getStartPosition() + node.getLength();
                            int endLine = _ast.getLineNumber(endOffset);
                            // can't resolve the type but args number
                            // matched , note that the last param is false
                            searchResults.add(createSearchResult(null, startOffset, endOffset, startLine, endLine, false));
                        }
                        if (possibleMatch) {
                            argumentsMatch = true;
                        }
                    }
                } else // any method args types is OK without setting
                // methodParamTypes
                {
                    argumentsMatch = true;
                }
                if (argumentsMatch) {
                    int startOffset = expression.getStartPosition();
                    int startLine = _ast.getLineNumber(startOffset);
                    int endOffset = node.getStartPosition() + node.getLength();
                    int endLine = _ast.getLineNumber(endOffset);
                    boolean fullMatch = true;
                    if ((typeHint != null) && (type != null) && type.getName().endsWith(typeHint) && !type.getName().equals(typeHint)) {
                        fullMatch = false;
                    }
                    searchResults.add(createSearchResult(null, startOffset, endOffset, startLine, endLine, fullMatch));
                }
            }
            return true;
        }
    });
    return searchResults;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) SearchResult(com.liferay.blade.api.SearchResult) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ExecutionException(java.util.concurrent.ExecutionException) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 24 with SearchResult

use of com.liferay.blade.api.SearchResult in project liferay-ide by liferay.

the class JavaFileJDT method findSuperClass.

@Override
public List<SearchResult> findSuperClass(String superClassName) {
    List<SearchResult> searchResults = new ArrayList<>();
    _ast.accept(new ASTVisitor() {

        @Override
        public boolean visit(TypeDeclaration node) {
            ITypeBinding superClass = null;
            if (node.resolveBinding() != null) {
                superClass = node.resolveBinding().getSuperclass();
                if (superClass != null) {
                    String searchContext = superClass.getName();
                    if (searchContext.equals(superClassName)) {
                        int startLine = _ast.getLineNumber(node.getName().getStartPosition());
                        int startOffset = node.getName().getStartPosition();
                        int endLine = _ast.getLineNumber(node.getName().getStartPosition() + node.getName().getLength());
                        int endOffset = node.getName().getStartPosition() + node.getName().getLength();
                        searchResults.add(createSearchResult(searchContext, startOffset, endOffset, startLine, endLine, true));
                    }
                }
            }
            return true;
        }
    });
    return searchResults;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayList(java.util.ArrayList) SearchResult(com.liferay.blade.api.SearchResult) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 25 with SearchResult

use of com.liferay.blade.api.SearchResult in project liferay-ide by liferay.

the class AbstractFileMigrator method analyze.

@Override
public List<Problem> analyze(File file) {
    List<Problem> problems = new ArrayList<>();
    String fileExtension = new Path(file.getAbsolutePath()).getFileExtension();
    List<SearchResult> searchResults = searchFile(file, createFileChecker(type, file, fileExtension));
    if (ListUtil.isNotEmpty(searchResults)) {
        String sectionHtml = MarkdownParser.getSection("BREAKING_CHANGES.markdown", sectionKey);
        if ((sectionHtml != null) && sectionHtml.equals("#legacy")) {
            sectionHtml = problemSummary;
        }
        for (SearchResult searchResult : searchResults) {
            if (searchResult != null) {
                problems.add(new Problem(problemTitle, problemSummary, fileExtension, problemTickets, file, searchResult.startLine, searchResult.startOffset, searchResult.endOffset, sectionHtml, searchResult.autoCorrectContext, Problem.STATUS_NOT_RESOLVED, Problem.DEFAULT_MARKER_ID, Problem.MARKER_ERROR));
            }
        }
    }
    return problems;
}
Also used : Path(org.eclipse.core.runtime.Path) ArrayList(java.util.ArrayList) Problem(com.liferay.blade.api.Problem) SearchResult(com.liferay.blade.api.SearchResult)

Aggregations

SearchResult (com.liferay.blade.api.SearchResult)29 ArrayList (java.util.ArrayList)25 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)8 JavaFile (com.liferay.blade.api.JavaFile)4 Problem (com.liferay.blade.api.Problem)4 File (java.io.File)4 Test (org.junit.Test)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 NodeList (org.w3c.dom.NodeList)3 PropertiesFileChecker (com.liferay.blade.upgrade.liferay70.PropertiesFileChecker)2 List (java.util.List)2 Path (org.eclipse.core.runtime.Path)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 ImportDeclaration (org.eclipse.jdt.core.dom.ImportDeclaration)2 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)2 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)2 JSONArray (org.json.JSONArray)2 KeyInfo (com.liferay.blade.upgrade.liferay70.PropertiesFileChecker.KeyInfo)1 ExecutionException (java.util.concurrent.ExecutionException)1