use of meghanada.analyze.Source in project meghanada-server by mopemope.
the class Session method execMain.
public InputStream execMain(String path, boolean debug) throws Exception {
boolean b = this.changeProject(path);
Optional<Source> source = this.parseJavaSource(new File(path));
return source.map(src -> {
try {
String clazz = src.getFQCN();
return currentProject.execMainClass(clazz, debug);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}).orElse(null);
}
use of meghanada.analyze.Source in project meghanada-server by mopemope.
the class Project method mergeFromProjectConfig.
public Project mergeFromProjectConfig() throws ProjectParseException {
final File configFile = new File(this.projectRoot, Config.MEGHANADA_CONF_FILE);
if (configFile.exists()) {
final com.typesafe.config.Config config = ConfigFactory.parseFile(configFile);
// java.home
if (config.hasPath(JAVA_HOME)) {
String o = config.getString(JAVA_HOME);
System.setProperty("java.home", o);
}
// java.home
if (config.hasPath(JAVA_VERSION)) {
String o = config.getString(JAVA_VERSION);
System.setProperty("java.specification.version", o);
}
// compile-source
if (config.hasPath(COMPILE_SOURCE)) {
this.compileSource = config.getString(COMPILE_SOURCE);
}
// compile-source
if (config.hasPath(COMPILE_TARGET)) {
this.compileTarget = config.getString(COMPILE_TARGET);
}
// dependencies
if (config.hasPath(DEPENDENCIES)) {
config.getStringList(DEPENDENCIES).stream().filter(path -> new File(path).exists()).map(path -> {
final File file = new File(path);
final ProjectDependency.Type type = ProjectDependency.getFileType(file);
return new ProjectDependency(file.getName(), "COMPILE", "1.0.0", file, type);
}).forEach(this.dependencies::add);
}
// test-dependencies
if (config.hasPath(TEST_DEPENDENCIES)) {
config.getStringList(TEST_DEPENDENCIES).stream().filter(path -> new File(path).exists()).map(path -> {
final File file = new File(path);
final ProjectDependency.Type type = ProjectDependency.getFileType(file);
return new ProjectDependency(file.getName(), "TEST", "1.0.0", file, type);
}).forEach(this.dependencies::add);
}
// sources
if (config.hasPath(SOURCES)) {
config.getStringList(SOURCES).stream().filter(path -> new File(path).exists()).map(File::new).forEach(file -> this.sources.add(file));
}
// sources
if (config.hasPath(RESOURCES)) {
config.getStringList(RESOURCES).stream().filter(path -> new File(path).exists()).map(File::new).forEach(file -> this.resources.add(file));
}
// test-sources
if (config.hasPath(TEST_SOURCES)) {
config.getStringList(TEST_SOURCES).stream().filter(path -> new File(path).exists()).map(File::new).forEach(file -> this.testSources.add(file));
}
// test-resources
if (config.hasPath(TEST_RESOURCES)) {
config.getStringList(TEST_RESOURCES).stream().filter(path -> new File(path).exists()).map(File::new).forEach(file -> this.testResources.add(file));
}
// output
if (config.hasPath(OUTPUT)) {
String o = config.getString(OUTPUT);
this.output = new File(o);
}
// test-output
if (config.hasPath(TEST_OUTPUT)) {
String o = config.getString(TEST_OUTPUT);
this.testOutput = new File(o);
}
final Config mainConfig = Config.load();
if (config.hasPath(INCLUDE_FILE)) {
final List<String> list = config.getStringList(INCLUDE_FILE);
mainConfig.setIncludeList(list);
}
if (config.hasPath(EXCLUDE_FILE)) {
final List<String> list = config.getStringList(INCLUDE_FILE);
mainConfig.setExcludeList(list);
}
if (config.hasPath(JAVA8_JAVAC_ARGS) && mainConfig.isJava8()) {
final List<String> list = config.getStringList(JAVA8_JAVAC_ARGS);
mainConfig.setJava8JavacArgs(list);
}
if (config.hasPath(JAVA9_JAVAC_ARGS) && mainConfig.isJava9()) {
final List<String> list = config.getStringList(JAVA9_JAVAC_ARGS);
mainConfig.setJava9JavacArgs(list);
}
}
// guard
if (this.output == null) {
throw new ProjectParseException("require output path");
}
if (this.testOutput == null) {
throw new ProjectParseException("require test output path");
}
// freeze
this.sources = new ImmutableSet.Builder<File>().addAll(this.sources).build();
log.debug("sources {}", this.sources);
this.resources = new ImmutableSet.Builder<File>().addAll(this.resources).build();
log.debug("resources {}", this.resources);
log.debug("output {}", this.output);
this.testSources = new ImmutableSet.Builder<File>().addAll(this.testSources).build();
log.debug("test sources {}", this.testSources);
this.testResources = new ImmutableSet.Builder<File>().addAll(this.testResources).build();
log.debug("test resources {}", this.testResources);
log.debug("test output {}", this.testOutput);
for (final ProjectDependency dependency : this.dependencies) {
log.debug("dependency {}:{}", dependency.getId(), dependency.getVersion());
}
return this;
}
use of meghanada.analyze.Source in project meghanada-server by mopemope.
the class LocationSearcher method searchFromDependency.
private Location searchFromDependency(final SearchContext context) throws IOException {
final String searchFQCN = context.searchFQCN;
final CachedASMReflector reflector = CachedASMReflector.getInstance();
final File classFile = reflector.getClassFile(searchFQCN);
final String tempDir = System.getProperty("java.io.tmpdir");
if (classFile != null && classFile.exists() && classFile.getName().endsWith(FileUtils.JAR_EXT)) {
final String androidHome = System.getenv("ANDROID_HOME");
if (androidHome != null) {
final Optional<ProjectDependency> dependencyOptional = this.project.getDependencies().stream().filter(dependency -> dependency.getFile().equals(classFile)).findFirst();
if (dependencyOptional.isPresent()) {
final ProjectDependency dependency = dependencyOptional.get();
final String sourceJar = ClassNameUtils.getSimpleName(dependency.getId()) + '-' + dependency.getVersion() + "-sources.jar";
final File root = new File(androidHome, "extras");
if (root.exists()) {
return getLocationFromSrcOrDecompile(context, classFile, root, sourceJar);
}
}
}
final File depParent = classFile.getParentFile();
final File dependencyDir = depParent.getParentFile();
final String srcJarName = ClassNameUtils.replace(classFile.getName(), FileUtils.JAR_EXT, "-sources.jar");
final String disable = System.getProperty("disable-source-jar");
if (disable != null && disable.equals("true")) {
return searchLocationFromDecompileFile(context, searchFQCN, classFile, tempDir);
}
return getLocationFromSrcOrDecompile(context, classFile, dependencyDir, srcJarName);
}
return null;
}
use of meghanada.analyze.Source in project meghanada-server by mopemope.
the class LocationSearcher method getMethodLocationFromProject.
private Optional<Location> getMethodLocationFromProject(final String methodName, final List<String> arguments, final File file) {
try {
final Source declaringClassSrc = getSource(project, file);
final String path = declaringClassSrc.getFile().getPath();
return declaringClassSrc.getClassScopes().stream().flatMap(ts -> ts.getScopes().stream()).filter(bs -> {
if (!methodName.equals(bs.getName())) {
return false;
}
if (!(bs instanceof MethodScope)) {
return false;
}
final MethodScope methodScope = (MethodScope) bs;
final List<String> parameters = methodScope.getParameters();
return ClassNameUtils.compareArgumentType(arguments, parameters);
}).map(MethodScope.class::cast).map(ms -> new Location(path, ms.getBeginLine(), ms.getNameRange().begin.column)).findFirst();
} catch (ExecutionException e) {
throw new UncheckedExecutionException(e);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of meghanada.analyze.Source in project meghanada-server by mopemope.
the class LocationSearcher method searchLocalVariable.
private Optional<Location> searchLocalVariable(final Source source, final int line, final int col, final String symbol) {
final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
final Map<String, Variable> variableMap = source.getVariableMap(line);
log.trace("variables={}", variableMap);
final Optional<Variable> variable = Optional.ofNullable(variableMap.get(symbol));
final Optional<Location> location = variable.map(var -> {
if (var.isDecl()) {
final Location loc = new Location(source.getFile().getPath(), var.range.begin.line, var.range.begin.column);
return Optional.of(loc);
} else {
final String fqcn = var.fqcn;
final Location loc = getFQCNLocation(fqcn);
return Optional.ofNullable(loc);
}
}).orElseGet(() -> {
// isField
final Optional<TypeScope> ts = source.getTypeScope(line);
if (!ts.isPresent()) {
return Optional.empty();
}
return ts.get().getField(symbol).map(fieldSymbol -> new Location(source.getFile().getPath(), fieldSymbol.range.begin.line, fieldSymbol.range.begin.column));
});
log.traceExit(entryMessage);
return location;
}
Aggregations