Skip to content
This repository was archived by the owner on Aug 2, 2019. It is now read-only.

Commit 03b289b

Browse files
committed
Initial commit.
0 parents  commit 03b289b

File tree

5 files changed

+299
-0
lines changed

5 files changed

+299
-0
lines changed

Diff for: pom.xml

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.github.scalatojava</groupId>
6+
<artifactId>scala-to-java</artifactId>
7+
<version>1.0</version>
8+
9+
<packaging>jar</packaging>
10+
<properties>
11+
<scala.version>2.11.4</scala.version>
12+
</properties>
13+
14+
<repositories>
15+
<repository>
16+
<id>scala-tools.org</id>
17+
<name>Scala-Tools Maven2 Repository</name>
18+
<url>http://scala-tools.org/repo-releases</url>
19+
</repository>
20+
</repositories>
21+
22+
<pluginRepositories>
23+
<pluginRepository>
24+
<id>scala-tools.org</id>
25+
<name>Scala-Tools Maven2 Repository</name>
26+
<url>http://scala-tools.org/repo-releases</url>
27+
</pluginRepository>
28+
</pluginRepositories>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>org.scala-lang</groupId>
33+
<artifactId>scala-library</artifactId>
34+
<version>${scala.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.scala-lang</groupId>
38+
<artifactId>scala-compiler</artifactId>
39+
<version>${scala.version}</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.scala-lang</groupId>
43+
<artifactId>scala-reflect</artifactId>
44+
<version>${scala.version}</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.bitbucket.mstrobel</groupId>
48+
<artifactId>procyon-compilertools</artifactId>
49+
<version>0.5.29</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.scalatest</groupId>
53+
<artifactId>scalatest_2.11</artifactId>
54+
<version>2.2.5</version>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-compiler-plugin</artifactId>
64+
<version>3.3</version>
65+
<configuration>
66+
<source>1.8</source>
67+
<target>1.8</target>
68+
</configuration>
69+
</plugin>
70+
<plugin>
71+
<groupId>org.scala-tools</groupId>
72+
<artifactId>maven-scala-plugin</artifactId>
73+
<version>2.15.2</version>
74+
<executions>
75+
<execution>
76+
<goals>
77+
<goal>compile</goal>
78+
<goal>testCompile</goal>
79+
</goals>
80+
</execution>
81+
</executions>
82+
<configuration>
83+
<scalaVersion>${scala.version}</scalaVersion>
84+
<args>
85+
<arg>-target:jvm-1.8</arg>
86+
</args>
87+
</configuration>
88+
</plugin>
89+
<!--disable surefire -->
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-surefire-plugin</artifactId>
93+
<version>2.7</version>
94+
<configuration>
95+
<skipTests>true</skipTests>
96+
</configuration>
97+
</plugin>
98+
<!--enable scalatest -->
99+
<plugin>
100+
<groupId>org.scalatest</groupId>
101+
<artifactId>scalatest-maven-plugin</artifactId>
102+
<version>1.0</version>
103+
<configuration>
104+
<reportsDirectory>
105+
${project.build.directory}/surefire-reports
106+
</reportsDirectory>
107+
<junitxml>.</junitxml>
108+
<filereports>WDF TestSuite.txt</filereports>
109+
</configuration>
110+
<executions>
111+
<execution>
112+
<id>test</id>
113+
<goals>
114+
<goal>test</goal>
115+
</goals>
116+
</execution>
117+
</executions>
118+
</plugin>
119+
<plugin>
120+
<executions>
121+
<execution>
122+
<id>make-assembly</id>
123+
<phase>package</phase>
124+
<goals>
125+
<goal>single</goal>
126+
</goals>
127+
</execution>
128+
</executions>
129+
<artifactId>maven-assembly-plugin</artifactId>
130+
<version>2.5.5</version>
131+
<configuration>
132+
<archive>
133+
<manifest>
134+
<mainClass>com.github.scalatojava.Main</mainClass>
135+
</manifest>
136+
</archive>
137+
<descriptorRefs>
138+
<descriptorRef>jar-with-dependencies</descriptorRef>
139+
</descriptorRefs>
140+
<finalName>scala-to-java</finalName>
141+
<appendAssemblyId>false</appendAssemblyId>
142+
</configuration>
143+
</plugin>
144+
</plugins>
145+
</build>
146+
<reporting>
147+
<plugins>
148+
<plugin>
149+
<groupId>org.scala-tools</groupId>
150+
<artifactId>maven-scala-plugin</artifactId>
151+
<configuration>
152+
<scalaVersion>${scala.version}</scalaVersion>
153+
</configuration>
154+
</plugin>
155+
</plugins>
156+
</reporting>
157+
</project>

Diff for: readme.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Scala To Java
2+
---------
3+
4+
Simple tool written in Scala that reveals the mystery of scala compiler.
5+
Reads scala code from the StdIn and writes it's decompiled Java version
6+
to StdOut.
7+
8+
9+
Usage
10+
---
11+
12+
* Make sure you have Java 1.8 and Maven installed
13+
* Checkout the project
14+
* In project directory invoke `mvn clean package`.
15+
In target directory `scala-to-java.jar` will be created
16+
* Run application with `java -jar target/scala-to-java.jar`
17+
* Type any scala code, for example `println("hello, world")`
18+
and finish with `END` character (`Ctrl-D`)
19+
20+
21+
Source highlighting and more
22+
---
23+
24+
To improve usage experience, you may want to use external source code
25+
highlighter (like `pygmentize`) and `pv` as the indicator of transpiling
26+
process.
27+
28+
My setup:
29+
```sh
30+
alias scala-to-java='java -jar ~/.scala-to-java.jar | pv -W | pygmentize -f
31+
256 -l java -O style=monokai'
32+
```
33+
34+
Credits
35+
---
36+
37+
Thanks to [Stanley Shyiko](https://github.com/shyiko), who
38+
actually implemented all the magic.

Diff for: src/main/scala/com/github/scalatojava/Main.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.scalatojava
2+
3+
/**
4+
* @author <a href="mailto:[email protected]">Ivan Zaytsev</a>
5+
* 2015-07-12
6+
*/
7+
object Main extends App {
8+
val source = Iterator.continually(scala.io.StdIn.readLine()).takeWhile(
9+
_ != null).mkString("\n")
10+
11+
println()
12+
private val java = ScalaToJava(source)
13+
14+
println(java.replaceFirst("(?s)public final class _\\$\\s+.*", ""))
15+
}
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.github.scalatojava
2+
3+
import java.io.{File, StringWriter}
4+
import java.nio.file.{Paths, Files}
5+
6+
import com.strobel.assembler.InputTypeLoader
7+
import com.strobel.assembler.metadata._
8+
import com.strobel.decompiler.languages.Languages
9+
import com.strobel.decompiler.languages.java.JavaFormattingOptions
10+
import com.strobel.decompiler.{PlainTextOutput, DecompilerSettings, DecompilationOptions}
11+
import scala.collection.mutable
12+
import scala.tools.nsc._
13+
import java.io._
14+
import java.util.Arrays
15+
16+
/**
17+
* Scala-to-Java translator
18+
* @author <a href="mailto:[email protected]">Stanley Shyiko</a>
19+
*/
20+
object ScalaToJava {
21+
22+
def decompile(file: File): String = {
23+
val settings = new DecompilerSettings
24+
settings.setLanguage(Languages.java)
25+
settings.setTypeLoader(new InputTypeLoader)
26+
settings.setFormattingOptions(JavaFormattingOptions.createDefault)
27+
28+
val options = new DecompilationOptions
29+
options.setSettings(settings)
30+
options.setFullDecompilation(true)
31+
32+
settings.getTypeLoader.tryLoadType(file.getPath,
33+
new Buffer(Files.readAllBytes(Paths.get(file.getPath))))
34+
35+
val metadataSystem = new NoRetryMetadataSystem(settings.getTypeLoader)
36+
val resolvedType = metadataSystem.resolveType(file.getName.substring(0,
37+
file.getName.length - ".class".length), mightBePrimitive = false)
38+
val writer = new StringWriter
39+
val output = new PlainTextOutput(writer)
40+
settings.getLanguage.decompileType(resolvedType, output, options)
41+
writer.flush()
42+
writer.toString
43+
}
44+
45+
def compile(input: File, output: File): Array[File] = {
46+
val settings = new Settings
47+
settings processArgumentString "-Xscript _ -usejavacp -d " + output.getPath
48+
val compiler = new Global(settings)
49+
val runner = new compiler.Run
50+
runner.compile(List(input.getPath))
51+
output.listFiles(new FilenameFilter {
52+
override def accept(dir: File, name: String): Boolean = name.endsWith(".class")
53+
})
54+
}
55+
56+
def apply(value: String): String = {
57+
val output = Files.createTempDirectory("s2j").toFile
58+
val input = new File(output, "_.scala")
59+
Files.write(input.toPath, Arrays.asList(value))
60+
compile(input, output).map(decompile).mkString("\n\n")
61+
}
62+
63+
class NoRetryMetadataSystem(typeLoader: ITypeLoader) extends MetadataSystem(typeLoader) {
64+
65+
val failedTypes = mutable.Set[String]()
66+
67+
override def resolveType(descriptor: String, mightBePrimitive: Boolean): TypeDefinition = {
68+
if (failedTypes.contains(descriptor)) {
69+
return null
70+
}
71+
val r = super.resolveType(descriptor, mightBePrimitive)
72+
if (r == null) {
73+
failedTypes.add(descriptor)
74+
}
75+
r
76+
}
77+
}
78+
}
79+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.scalatojava
2+
3+
import org.scalatest._
4+
5+
class ScalaToJavaSpec extends FlatSpec with Matchers {
6+
7+
it should "be able to convert scala to java" in {
8+
ScalaToJava("""println("hello world")""") should include("class _")
9+
}
10+
}

0 commit comments

Comments
 (0)