sandbox/attributed-index-web/StandaloneFinder.java
branchdonation_review
changeset 1043 57843026e60b
parent 1027 205b7632914c
parent 1040 f7b6892fd754
child 1044 7feb751ba76b
     1.1 --- a/sandbox/attributed-index-web/StandaloneFinder.java	Mon Dec 19 11:37:36 2016 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,213 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2009-2010 Sun Microsystems, Inc. All rights reserved.
     1.8 - *
     1.9 - * The contents of this file are subject to the terms of either the GNU
    1.10 - * General Public License Version 2 only ("GPL") or the Common
    1.11 - * Development and Distribution License("CDDL") (collectively, the
    1.12 - * "License"). You may not use this file except in compliance with the
    1.13 - * License. You can obtain a copy of the License at
    1.14 - * http://www.netbeans.org/cddl-gplv2.html
    1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.16 - * specific language governing permissions and limitations under the
    1.17 - * License.  When distributing the software, include this License Header
    1.18 - * Notice in each file and include the License file at
    1.19 - * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.20 - * particular file as subject to the "Classpath" exception as provided
    1.21 - * by Sun in the GPL Version 2 section of the License file that
    1.22 - * accompanied this code. If applicable, add the following below the
    1.23 - * License Header, with the fields enclosed by brackets [] replaced by
    1.24 - * your own identifying information:
    1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
    1.26 - *
    1.27 - * If you wish your version of this file to be governed by only the CDDL
    1.28 - * or only the GPL Version 2, indicate your decision by adding
    1.29 - * "[Contributor] elects to include this software in this distribution
    1.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.31 - * single choice of license, a recipient has the option to distribute
    1.32 - * your version of this file under either the CDDL, the GPL Version 2 or
    1.33 - * to extend the choice of license to its licensees as provided above.
    1.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.35 - * Version 2 license, then the option applies only if the new code is
    1.36 - * made subject to such option by the copyright holder.
    1.37 - *
    1.38 - * Contributor(s):
    1.39 - *
    1.40 - * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
    1.41 - */
    1.42 -
    1.43 -package org.netbeans.modules.jackpot30.backend.impl.api;
    1.44 -
    1.45 -import com.sun.source.tree.CompilationUnitTree;
    1.46 -import com.sun.source.tree.Tree;
    1.47 -import com.sun.source.util.TreePath;
    1.48 -import com.sun.source.util.Trees;
    1.49 -import com.sun.tools.javac.api.JavacTaskImpl;
    1.50 -import java.io.File;
    1.51 -import java.io.IOException;
    1.52 -import java.net.URI;
    1.53 -import java.net.URL;
    1.54 -import java.util.Arrays;
    1.55 -import java.util.BitSet;
    1.56 -import java.util.Collection;
    1.57 -import java.util.Collections;
    1.58 -import java.util.HashMap;
    1.59 -import java.util.LinkedList;
    1.60 -import java.util.Map;
    1.61 -import javax.tools.Diagnostic;
    1.62 -import javax.tools.JavaCompiler;
    1.63 -import javax.tools.JavaFileObject;
    1.64 -import javax.tools.SimpleJavaFileObject;
    1.65 -import javax.tools.ToolProvider;
    1.66 -import org.apache.lucene.document.Document;
    1.67 -import org.apache.lucene.index.IndexReader;
    1.68 -import org.apache.lucene.index.Term;
    1.69 -import org.apache.lucene.search.Collector;
    1.70 -import org.apache.lucene.search.IndexSearcher;
    1.71 -import org.apache.lucene.search.Query;
    1.72 -import org.apache.lucene.search.Searcher;
    1.73 -import org.apache.lucene.search.TermQuery;
    1.74 -import org.apache.lucene.store.FSDirectory;
    1.75 -import org.netbeans.modules.jackpot30.impl.duplicates.indexing.DuplicatesIndex;
    1.76 -import org.netbeans.modules.jackpot30.impl.indexing.AbstractLuceneIndex.BitSetCollector;
    1.77 -import org.netbeans.modules.jackpot30.impl.indexing.FileBasedIndex;
    1.78 -import org.netbeans.modules.jackpot30.spi.PatternConvertor;
    1.79 -import org.netbeans.modules.java.hints.jackpot.impl.Utilities;
    1.80 -import org.netbeans.modules.java.hints.jackpot.impl.pm.BulkSearch;
    1.81 -import org.netbeans.modules.java.hints.jackpot.impl.pm.BulkSearch.BulkPattern;
    1.82 -import org.netbeans.modules.java.hints.jackpot.spi.HintDescription;
    1.83 -import org.netbeans.modules.java.hints.jackpot.spi.HintDescription.AdditionalQueryConstraints;
    1.84 -import org.netbeans.modules.java.hints.jackpot.spi.Trigger.PatternDescription;
    1.85 -import org.netbeans.modules.parsing.impl.indexing.CacheFolder;
    1.86 -import org.netbeans.modules.parsing.impl.indexing.SPIAccessor;
    1.87 -import org.openide.filesystems.FileObject;
    1.88 -import org.openide.filesystems.FileUtil;
    1.89 -
    1.90 -/**
    1.91 - *
    1.92 - * @author lahvac
    1.93 - */
    1.94 -public class StandaloneFinder {
    1.95 -
    1.96 -    public static Collection<? extends String> findCandidates(File sourceRoot, String pattern) throws IOException {
    1.97 -        BulkPattern bulkPattern = preparePattern(pattern, null);
    1.98 -        
    1.99 -        return FileBasedIndex.get(sourceRoot.toURI().toURL()).findCandidates(bulkPattern);
   1.100 -    }
   1.101 -
   1.102 -    public static int[] findCandidateOccurrenceSpans(File sourceRoot, String relativePath, String pattern) throws IOException {
   1.103 -        BulkPattern bulkPattern = preparePattern(pattern, null);
   1.104 -        CharSequence source = FileBasedIndex.get(sourceRoot.toURI().toURL()).getSourceCode(relativePath).toString().replaceAll("\r\n", "\n");
   1.105 -        JavacTaskImpl jti = prepareJavacTaskImpl();
   1.106 -        CompilationUnitTree cut = jti.parse(new JFOImpl(source)).iterator().next();
   1.107 -        Collection<TreePath> paths = new LinkedList<TreePath>();
   1.108 -        
   1.109 -        for (Collection<TreePath> c : BulkSearch.getDefault().match(null, new TreePath(cut), bulkPattern).values()) {
   1.110 -            paths.addAll(c);
   1.111 -        }
   1.112 -
   1.113 -        Trees t = Trees.instance(jti);
   1.114 -        int[] result = new int[2 * paths.size()];
   1.115 -        int i = 0;
   1.116 -
   1.117 -        for (TreePath tp : paths) {
   1.118 -            result[i++] = (int) t.getSourcePositions().getStartPosition(cut, tp.getLeaf());
   1.119 -            result[i++] = (int) t.getSourcePositions().getEndPosition(cut, tp.getLeaf());
   1.120 -        }
   1.121 -
   1.122 -        return result;
   1.123 -    }
   1.124 -
   1.125 -    public static Collection<Diagnostic<? extends JavaFileObject>> parseAndReportErrors(String pattern) {
   1.126 -        Collection<Diagnostic<? extends JavaFileObject>> errors = new LinkedList<Diagnostic<? extends JavaFileObject>>();
   1.127 -
   1.128 -        preparePattern(pattern, errors);
   1.129 -
   1.130 -        return errors;
   1.131 -    }
   1.132 -
   1.133 -    private static File getIndex(URL url) throws IOException {
   1.134 -        FileObject indexBaseFolder = CacheFolder.getDataFolder(url);
   1.135 -        String path = SPIAccessor.getInstance().getIndexerPath(DuplicatesIndex.NAME, DuplicatesIndex.VERSION);
   1.136 -        FileObject indexFolder = FileUtil.createFolder(indexBaseFolder, path);
   1.137 -        return FileUtil.toFile(indexFolder);
   1.138 -    }
   1.139 -
   1.140 -    public static Map<String, Collection<? extends String>> containsHash(File sourceRoot, Iterable<? extends String> hashes) throws IOException {
   1.141 -        File cacheRoot = getIndex(sourceRoot.toURI().toURL());
   1.142 -        File dir = new File(cacheRoot, "fulltext");
   1.143 -
   1.144 -        if (dir.listFiles() != null && dir.listFiles().length > 0) {
   1.145 -            IndexReader reader = IndexReader.open(FSDirectory.open(dir), true);
   1.146 -            Map<String, Collection<? extends String>> result = new HashMap<String, Collection<? extends String>>();
   1.147 -
   1.148 -            for (String hash : hashes) {
   1.149 -                Collection<String> found = new LinkedList<String>();
   1.150 -                Query query = new TermQuery(new Term("generalized", hash));
   1.151 -                Searcher s = new IndexSearcher(reader);
   1.152 -                BitSet matchingDocuments = new BitSet(reader.maxDoc());
   1.153 -                Collector c = new BitSetCollector(matchingDocuments);
   1.154 -
   1.155 -                s.search(query, c);
   1.156 -
   1.157 -                for (int docNum = matchingDocuments.nextSetBit(0); docNum >= 0; docNum = matchingDocuments.nextSetBit(docNum + 1)) {
   1.158 -                    final Document doc = reader.document(docNum);
   1.159 -
   1.160 -                    found.add(doc.getField("path").stringValue());
   1.161 -                }
   1.162 -
   1.163 -                result.put(hash, found);
   1.164 -            }
   1.165 -            
   1.166 -            return result;
   1.167 -        }
   1.168 -
   1.169 -        return Collections.emptyMap();
   1.170 -    }
   1.171 -    
   1.172 -    private static BulkPattern preparePattern(String pattern, Collection<Diagnostic<? extends JavaFileObject>> errors) {
   1.173 -        return preparePattern(PatternConvertor.create(pattern), errors);
   1.174 -    }
   1.175 -
   1.176 -    //XXX: copied from BatchSearch, may be possible to merge once CompilationInfo is accessible in server mode
   1.177 -    private static BulkPattern preparePattern(final Iterable<? extends HintDescription> patterns, Collection<Diagnostic<? extends JavaFileObject>> errors) {
   1.178 -        JavacTaskImpl javac = prepareJavacTaskImpl();
   1.179 -        Collection<String> code = new LinkedList<String>();
   1.180 -        Collection<Tree> trees = new LinkedList<Tree>();
   1.181 -        Collection<AdditionalQueryConstraints> additionalConstraints = new LinkedList<AdditionalQueryConstraints>();
   1.182 -
   1.183 -        for (HintDescription pattern : patterns) {
   1.184 -            String textPattern = ((PatternDescription) pattern.getTrigger()).getPattern();
   1.185 -
   1.186 -            code.add(textPattern);
   1.187 -            trees.add(Utilities.parseAndAttribute(javac, textPattern, errors));
   1.188 -            additionalConstraints.add(pattern.getAdditionalConstraints());
   1.189 -        }
   1.190 -
   1.191 -        return BulkSearch.getDefault().create(code, trees, additionalConstraints);
   1.192 -    }
   1.193 -
   1.194 -    private static JavacTaskImpl prepareJavacTaskImpl() {
   1.195 -        final String bootPath = System.getProperty("sun.boot.class.path"); //NOI18N
   1.196 -        final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
   1.197 -
   1.198 -        assert tool != null;
   1.199 -
   1.200 -        JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, Arrays.asList("-bootclasspath",  bootPath, "-Xjcov"), null, Collections.<JavaFileObject>emptyList());
   1.201 -        
   1.202 -        return ct;
   1.203 -    }
   1.204 -
   1.205 -    private static final class JFOImpl extends SimpleJavaFileObject {
   1.206 -        private final CharSequence code;
   1.207 -        public JFOImpl(CharSequence code) {
   1.208 -            super(URI.create(""), Kind.SOURCE);
   1.209 -            this.code = code;
   1.210 -        }
   1.211 -        @Override
   1.212 -        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.213 -            return code;
   1.214 -        }
   1.215 -    }
   1.216 -}