Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
102 lines (87 loc) · 3.67 KB
/
Copy pathMain.java
File metadata and controls
102 lines (87 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.javacs;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JSR310Module;
import com.sun.tools.javac.api.JavacTool;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.Socket;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.eclipse.lsp4j.launch.LSPLauncher;
import org.eclipse.lsp4j.services.LanguageClient;
public class Main {
public static final ObjectMapper JSON =
new ObjectMapper()
.registerModule(new Jdk8Module())
.registerModule(new JSR310Module())
.registerModule(pathAsJson())
.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
private static final Logger LOG = Logger.getLogger("main");
public static void setRootFormat() {
Logger root = Logger.getLogger("");
for (Handler h : root.getHandlers()) h.setFormatter(new LogFormat());
}
private static SimpleModule pathAsJson() {
SimpleModule m = new SimpleModule();
m.addSerializer(
Path.class,
new JsonSerializer<Path>() {
@Override
public void serialize(
Path path, JsonGenerator gen, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
gen.writeString(path.toString());
}
});
m.addDeserializer(
Path.class,
new JsonDeserializer<Path>() {
@Override
public Path deserialize(
JsonParser parse, DeserializationContext deserializationContext)
throws IOException, JsonProcessingException {
return Paths.get(parse.getText());
}
});
return m;
}
public static void main(String[] args) {
try {
ClassLoader langTools = LangTools.createLangToolsClassLoader();
Class<?> main = Class.forName("org.javacs.Main", true, langTools);
Method run = main.getMethod("run");
run.invoke(null);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Failed", e);
}
}
public static ClassLoader checkJavacClassLoader() {
return JavacTool.create().getClass().getClassLoader();
}
public static void run() {
assert checkJavacClassLoader() instanceof ChildFirstClassLoader;
setRootFormat();
try {
JavaLanguageServer server = new JavaLanguageServer();
Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(server, System.in, System.out);
server.installClient(launcher.getRemoteProxy());
launcher.startListening();
LOG.info(String.format("java.version is %s", System.getProperty("java.version")));
} catch (Throwable t) {
LOG.log(Level.SEVERE, t.getMessage(), t);
System.exit(1);
}
}
}