|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * Core API for SciJava code intelligence features. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2026 SciJava developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package org.scijava.code.api; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Objects; |
| 36 | + |
| 37 | +/** |
| 38 | + * Where a script runs, as far as analyzing it needs to know: the interpreter |
| 39 | + * (or other executable) that runs it, extra paths to search for modules or |
| 40 | + * packages, and classpath entries. Every part is optional. |
| 41 | + * <p> |
| 42 | + * For example, a CPython script run via Appose has its environment's Python |
| 43 | + * as interpreter, and a directory of packages installed for completion (see |
| 44 | + * dependency overlays) as an extra search path. |
| 45 | + * </p> |
| 46 | + * |
| 47 | + * @author Gabriel Selzer |
| 48 | + * @see ScriptDocument |
| 49 | + */ |
| 50 | +public final class EnvironmentContext { |
| 51 | + |
| 52 | + /** No particular environment. */ |
| 53 | + public static final EnvironmentContext EMPTY = new EnvironmentContext(null, |
| 54 | + Collections.emptyList(), Collections.emptyList()); |
| 55 | + |
| 56 | + private final String interpreter; |
| 57 | + private final List<String> searchPaths; |
| 58 | + private final List<String> classpath; |
| 59 | + |
| 60 | + /** |
| 61 | + * @param interpreter Path of the interpreter or executable, or null. |
| 62 | + * @param searchPaths Extra paths to search for modules, e.g. for Python. |
| 63 | + * @param classpath Classpath entries, e.g. for JVM languages. |
| 64 | + */ |
| 65 | + public EnvironmentContext(final String interpreter, |
| 66 | + final List<String> searchPaths, final List<String> classpath) |
| 67 | + { |
| 68 | + this.interpreter = interpreter; |
| 69 | + this.searchPaths = searchPaths == null ? Collections.emptyList() |
| 70 | + : Collections.unmodifiableList(new ArrayList<>(searchPaths)); |
| 71 | + this.classpath = classpath == null ? Collections.emptyList() : Collections |
| 72 | + .unmodifiableList(new ArrayList<>(classpath)); |
| 73 | + } |
| 74 | + |
| 75 | + /** Path of the interpreter or executable running the script, or null. */ |
| 76 | + public String interpreter() { |
| 77 | + return interpreter; |
| 78 | + } |
| 79 | + |
| 80 | + /** Extra paths to search for modules or packages; never null. */ |
| 81 | + public List<String> searchPaths() { |
| 82 | + return searchPaths; |
| 83 | + } |
| 84 | + |
| 85 | + /** Classpath entries; never null. */ |
| 86 | + public List<String> classpath() { |
| 87 | + return classpath; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean equals(final Object o) { |
| 92 | + if (!(o instanceof EnvironmentContext)) return false; |
| 93 | + final EnvironmentContext that = (EnvironmentContext) o; |
| 94 | + return Objects.equals(interpreter, that.interpreter) && searchPaths.equals( |
| 95 | + that.searchPaths) && classpath.equals(that.classpath); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public int hashCode() { |
| 100 | + return Objects.hash(interpreter, searchPaths, classpath); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public String toString() { |
| 105 | + return "EnvironmentContext[interpreter=" + interpreter + ", searchPaths=" + |
| 106 | + searchPaths + ", classpath=" + classpath + "]"; |
| 107 | + } |
| 108 | +} |
0 commit comments