Search in sources :

Example 16 with SearchResult

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

the class JavaFileJDTTest method checkStaticMethodInvocation.

@Test
public void checkStaticMethodInvocation() throws Exception {
    File file = new File("tests/files/JavaFileChecker.java");
    final BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
    final Collection<ServiceReference<JavaFile>> sr = context.getServiceReferences(JavaFile.class, "(file.extension=java)");
    JavaFile javaFileChecker = context.getService(sr.iterator().next());
    javaFileChecker.setFile(file);
    List<SearchResult> searchResults = javaFileChecker.findMethodInvocations(null, "String", "valueOf", null);
    assertNotNull(searchResults);
    SearchResult searchResult = searchResults.get(0);
    assertNotNull(searchResult);
    if (Util.isWindows()) {
        assertEquals(14, searchResult.startLine);
        assertEquals(15, searchResult.endLine);
        assertEquals(242, searchResult.startOffset);
        assertEquals(265, searchResult.endOffset);
    } else {
        assertEquals(14, searchResult.startLine);
        assertEquals(15, searchResult.endLine);
        assertEquals(229, searchResult.startOffset);
        assertEquals(251, searchResult.endOffset);
    }
}
Also used : JavaFile(com.liferay.blade.api.JavaFile) SearchResult(com.liferay.blade.api.SearchResult) JavaFile(com.liferay.blade.api.JavaFile) File(java.io.File) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 17 with SearchResult

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

the class DeprecatedMethodsMigrator method searchFile.

@Override
protected List<SearchResult> searchFile(File file, JavaFile fileChecker) {
    List<SearchResult> searchResults = new ArrayList<>();
    String[] parameters = null;
    JSONArray parameterJSONArray = _tempMethod.getJSONArray("parameters");
    if (parameterJSONArray != null) {
        parameters = new String[parameterJSONArray.length()];
        for (int i = 0; i < parameterJSONArray.length(); i++) {
            parameters[i] = parameterJSONArray.getString(i);
        }
    }
    searchResults.addAll(fileChecker.findMethodInvocations(_tempMethod.getString("className"), null, _tempMethod.getString("methodName"), parameters));
    searchResults.addAll(fileChecker.findMethodInvocations(null, _tempMethod.getString("className"), _tempMethod.getString("methodName"), parameters));
    return searchResults;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) SearchResult(com.liferay.blade.api.SearchResult)

Example 18 with SearchResult

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

the class DeprecatedMethodsMigrator method analyze.

@Override
public List<Problem> analyze(File file) {
    List<Problem> problems = new ArrayList<>();
    String fileExtension = new Path(file.getAbsolutePath()).getFileExtension();
    for (JSONArray deprecatedMethodsArray : _deprecatedMethods) {
        for (int j = 0; j < deprecatedMethodsArray.length(); j++) {
            _tempMethod = deprecatedMethodsArray.getJSONObject(j);
            List<SearchResult> searchResults = searchFile(file, createFileChecker(type, file, fileExtension));
            if (searchResults != null) {
                for (SearchResult searchResult : searchResults) {
                    int makerType = Problem.MARKER_ERROR;
                    if (_tempMethod.getString("deprecatedVersion").equals("7.0")) {
                        makerType = Problem.MARKER_WARNING;
                    }
                    problems.add(new Problem(_tempMethod.getString("javadoc"), _tempMethod.getString("javadoc"), fileExtension, "", file, searchResult.startLine, searchResult.startOffset, searchResult.endOffset, _tempMethod.getString("javadoc"), searchResult.autoCorrectContext, Problem.STATUS_NOT_RESOLVED, Problem.DEFAULT_MARKER_ID, makerType));
                }
            }
        }
    }
    return problems;
}
Also used : Path(org.eclipse.core.runtime.Path) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Problem(com.liferay.blade.api.Problem) SearchResult(com.liferay.blade.api.SearchResult)

Example 19 with SearchResult

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

the class JavaFileJDTTest method checkMethodInvocation.

@Test
public void checkMethodInvocation() throws Exception {
    File file = new File("tests/files/JavaFileChecker.java");
    final BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
    final Collection<ServiceReference<JavaFile>> sr = context.getServiceReferences(JavaFile.class, "(file.extension=java)");
    JavaFile javaFileChecker = context.getService(sr.iterator().next());
    javaFileChecker.setFile(file);
    List<SearchResult> searchResults = javaFileChecker.findMethodInvocations("Foo", null, "bar", null);
    assertNotNull(searchResults);
    assertEquals(4, searchResults.size());
    SearchResult searchResult = searchResults.get(0);
    assertNotNull(searchResult);
    if (Util.isWindows()) {
        assertEquals(10, searchResult.startLine);
        assertEquals(11, searchResult.endLine);
        assertEquals(190, searchResult.startOffset);
        assertEquals(210, searchResult.endOffset);
    } else {
        assertEquals(10, searchResult.startLine);
        assertEquals(11, searchResult.endLine);
        assertEquals(181, searchResult.startOffset);
        assertEquals(200, searchResult.endOffset);
    }
}
Also used : JavaFile(com.liferay.blade.api.JavaFile) SearchResult(com.liferay.blade.api.SearchResult) JavaFile(com.liferay.blade.api.JavaFile) File(java.io.File) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 20 with SearchResult

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

the class JavaFileJDT method findImport.

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

        @Override
        public boolean visit(ImportDeclaration node) {
            String searchContext = node.getName().toString();
            if (importName.equals(searchContext)) {
                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 false;
        }
    });
    if (ListUtil.isNotEmpty(searchResults)) {
        return searchResults.get(0);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) SearchResult(com.liferay.blade.api.SearchResult) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

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