Skip to content

Commit 67efa47

Browse files
Merge pull request #1 from CodinGame/init
Java compiler implementation
2 parents f7dc825 + 22e3f9f commit 67efa47

File tree

10 files changed

+401
-1
lines changed

10 files changed

+401
-1
lines changed

.gitignore

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### macOS template
3+
*.DS_Store
4+
.AppleDouble
5+
.LSOverride
6+
7+
# Icon must end with two \r
8+
Icon
9+
10+
11+
# Thumbnails
12+
._*
13+
14+
# Files that might appear in the root of a volume
15+
.DocumentRevisions-V100
16+
.fseventsd
17+
.Spotlight-V100
18+
.TemporaryItems
19+
.Trashes
20+
.VolumeIcon.icns
21+
.com.apple.timemachine.donotpresent
22+
23+
# Directories potentially created on remote AFP share
24+
.AppleDB
25+
.AppleDesktop
26+
Network Trash Folder
27+
Temporary Items
28+
.apdisk
29+
### JetBrains template
30+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
31+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
32+
33+
# User-specific stuff:
34+
.idea/
35+
36+
# Gradle:
37+
.idea/gradle.xml
38+
.idea/libraries
39+
40+
# Mongo Explorer plugin:
41+
.idea/mongoSettings.xml
42+
43+
## File-based project format:
44+
*.iws
45+
*.iml
46+
47+
## Plugin-specific files:
48+
49+
# IntelliJ
50+
/out/
51+
52+
# mpeltonen/sbt-idea plugin
53+
.idea_modules/
54+
55+
# JIRA plugin
56+
atlassian-ide-plugin.xml
57+
58+
# Crashlytics plugin (for Android Studio and IntelliJ)
59+
com_crashlytics_export_strings.xml
60+
crashlytics.properties
61+
crashlytics-build.properties
62+
fabric.properties
63+
### Maven template
64+
target/
65+
pom.xml.tag
66+
pom.xml.releaseBackup
67+
pom.xml.versionsBackup
68+
pom.xml.next
69+
release.properties
70+
dependency-reduced-pom.xml
71+
buildNumber.properties
72+
.mvn/timing.properties
73+
74+
# Exclude maven wrapper
75+
!/.mvn/wrapper/maven-wrapper.jar
76+
### Java template
77+
78+
# BlueJ files
79+
*.ctxt
80+
81+
# Mobile Tools for Java (J2ME)
82+
.mtj.tmp/
83+
84+
# Package Files #
85+
*.war
86+
*.ear
87+
88+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
89+
hs_err_pid*
90+
### Eclipse template
91+
92+
.metadata
93+
bin/
94+
tmp/
95+
*.tmp
96+
*.bak
97+
*.swp
98+
*~.nib
99+
local.properties
100+
.settings/
101+
.loadpath
102+
.recommenders
103+
104+
# Eclipse Core
105+
.project
106+
107+
# External tool builders
108+
.externalToolBuilders/
109+
110+
# Locally stored "Eclipse launch configurations"
111+
*.launch
112+
113+
# PyDev specific (Python IDE for Eclipse)
114+
*.pydevproject
115+
116+
# CDT-specific (C/C++ Development Tooling)
117+
.cproject
118+
119+
# JDT-specific (Eclipse Java Development Tools)
120+
.classpath
121+
122+
# Java annotation processor (APT)
123+
.factorypath
124+
125+
# PDT-specific (PHP Development Tools)
126+
.buildpath
127+
128+
# sbteclipse plugin
129+
.target
130+
131+
# Tern plugin
132+
.tern-project
133+
134+
# TeXlipse plugin
135+
.texlipse
136+
137+
# STS (Spring Tool Suite)
138+
.springBeans
139+
140+
# Code Recommenders
141+
.recommenders/
142+

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM openjdk:8-jdk
2+
3+
# Add files.
4+
ADD target/java-compiler-0.0.1-SNAPSHOT-jar-with-dependencies.jar /usr/src/codingame/java-compiler/java-compiler.jar
5+
ADD src/main/resources/cgjavac /usr/src/codingame/java-compiler/cgjavac
6+
7+
ENTRYPOINT ["/usr/src/codingame/java-compiler/cgjavac"]

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# java-compiler
2-
A java compiler which generates the expect json as result
2+
3+
A java compiler which generates the expected json as result.

hooks/pre_build

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
echo "=> Building the java compiler jar"
3+
docker run \
4+
-v $(pwd):/usr/src/java-compiler \
5+
-w /usr/src/java-compiler \
6+
maven:3 \
7+
mvn clean package

pom.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.codingame.codemachine</groupId>
5+
<artifactId>java-compiler</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
</properties>
11+
12+
<dependencies>
13+
<dependency>
14+
<groupId>com.google.code.gson</groupId>
15+
<artifactId>gson</artifactId>
16+
<version>2.8.0</version>
17+
</dependency>
18+
</dependencies>
19+
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
<version>3.5.1</version>
26+
<configuration>
27+
<source>1.8</source>
28+
<target>1.8</target>
29+
</configuration>
30+
</plugin>
31+
<plugin>
32+
<artifactId>maven-assembly-plugin</artifactId>
33+
<configuration>
34+
<archive>
35+
<manifest>
36+
<mainClass>com.codingame.codemachine.compiler.java.CodinGameJavaCompiler</mainClass>
37+
</manifest>
38+
</archive>
39+
<descriptorRefs>
40+
<descriptorRef>jar-with-dependencies</descriptorRef>
41+
</descriptorRefs>
42+
</configuration>
43+
<executions>
44+
<execution>
45+
<id>make-assembly</id>
46+
<phase>package</phase>
47+
<goals>
48+
<goal>single</goal>
49+
</goals>
50+
</execution>
51+
</executions>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
</project>
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.codingame.codemachine.compiler.java;
2+
3+
import com.codingame.codemachine.compiler.java.core.CompilationLogDto;
4+
import com.codingame.codemachine.compiler.java.core.CompilationLogKind;
5+
import com.codingame.codemachine.compiler.java.core.CompilationResult;
6+
import com.google.gson.Gson;
7+
8+
import javax.tools.Diagnostic;
9+
import javax.tools.Diagnostic.Kind;
10+
import javax.tools.DiagnosticCollector;
11+
import javax.tools.JavaCompiler;
12+
import javax.tools.JavaFileObject;
13+
import javax.tools.StandardJavaFileManager;
14+
import javax.tools.ToolProvider;
15+
import java.io.IOException;
16+
import java.io.OutputStream;
17+
import java.io.PrintStream;
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import static java.util.Collections.singletonList;
22+
23+
public class CodinGameJavaCompiler {
24+
25+
private static class NullOutputStream extends OutputStream {
26+
@Override
27+
public void write(int b) throws IOException {
28+
}
29+
}
30+
31+
public static void main(String... args) throws IOException {
32+
PrintStream realOut = System.out;
33+
System.setOut(new PrintStream(new NullOutputStream(), true));
34+
System.setErr(new PrintStream(new NullOutputStream(), true));
35+
36+
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
37+
DiagnosticCollector<JavaFileObject> diagnosticsCollector = new DiagnosticCollector<>();
38+
StandardJavaFileManager fileManager =
39+
compiler.getStandardFileManager(diagnosticsCollector, null, null);
40+
41+
List<String> files = new ArrayList<>();
42+
List<String> options = new ArrayList<>();
43+
44+
45+
for (int i = 0; i < args.length; ++i) {
46+
String arg = args[i];
47+
48+
if (arg.startsWith("-")) {
49+
int paramCount = compiler.isSupportedOption(arg);
50+
if (paramCount < 0) {
51+
paramCount = fileManager.isSupportedOption(arg);
52+
}
53+
if (paramCount < 0) {
54+
// unsupported, let javacTask show the error
55+
paramCount = 0;
56+
}
57+
options.add(arg);
58+
for (int j = 0; j < paramCount; ++j, i++) {
59+
options.add(args[i + 1]);
60+
}
61+
}
62+
else {
63+
files.add(arg);
64+
}
65+
}
66+
67+
if (!files.isEmpty()) {
68+
List<CompilationLogDto> logs = new ArrayList<>();
69+
70+
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(files);
71+
JavaCompiler.CompilationTask task =
72+
compiler.getTask(null, fileManager, diagnosticsCollector, options, null, compilationUnits);
73+
boolean success = task.call();
74+
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnosticsCollector.getDiagnostics()) {
75+
switch (diagnostic.getKind()) {
76+
case ERROR:
77+
case WARNING:
78+
CompilationLogDto log = new CompilationLogDto();
79+
log.setFilename(diagnostic.getSource().getName());
80+
log.setLine(diagnostic.getLineNumber());
81+
log.setColumn(diagnostic.getColumnNumber());
82+
log.setMessage(diagnostic.getMessage(null));
83+
log.setKind(diagnostic.getKind() == Kind.ERROR ? CompilationLogKind.ERROR : CompilationLogKind.WARNING);
84+
logs.add(log);
85+
break;
86+
default:
87+
// nothing
88+
}
89+
}
90+
fileManager.close();
91+
92+
CompilationResult result = new CompilationResult();
93+
result.setSuccess(success);
94+
result.setLogs(logs);
95+
realOut.println(new Gson().toJson(result));
96+
System.exit(success ? 0 : 1);
97+
}
98+
else {
99+
CompilationResult result = new CompilationResult();
100+
result.setSuccess(false);
101+
CompilationLogDto log = new CompilationLogDto();
102+
log.setKind(CompilationLogKind.ERROR);
103+
log.setMessage("no source file");
104+
result.setLogs(singletonList(log));
105+
realOut.println(new Gson().toJson(result));
106+
System.exit(2);
107+
}
108+
}
109+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.codingame.codemachine.compiler.java.core;
2+
3+
public class CompilationLogDto {
4+
private CompilationLogKind kind;
5+
private Long line, column;
6+
private String message;
7+
private String filename;
8+
9+
public CompilationLogKind getKind() {
10+
return kind;
11+
}
12+
13+
public void setKind(CompilationLogKind kind) {
14+
this.kind = kind;
15+
}
16+
17+
public Long getLine() {
18+
return line;
19+
}
20+
21+
public void setLine(Long line) {
22+
this.line = line;
23+
}
24+
25+
public Long getColumn() {
26+
return column;
27+
}
28+
29+
public void setColumn(Long column) {
30+
this.column = column;
31+
}
32+
33+
public String getMessage() {
34+
return message;
35+
}
36+
37+
public void setMessage(String message) {
38+
this.message = message;
39+
}
40+
41+
public String getFilename() {
42+
return filename;
43+
}
44+
45+
public void setFilename(String filename) {
46+
this.filename = filename;
47+
}
48+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.codingame.codemachine.compiler.java.core;
2+
3+
public enum CompilationLogKind {
4+
ERROR, WARNING
5+
}

0 commit comments

Comments
 (0)