Skip to content

Commit

Permalink
Refactor ImportItem class
Browse files Browse the repository at this point in the history
  • Loading branch information
Kota Mizushima committed Aug 24, 2023
1 parent 1af688a commit 8c2115b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
7 changes: 4 additions & 3 deletions src/main/scala/onion/compiler/ImportItem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package onion.compiler
* @author Kota Mizushima
*
*/
case class ImportItem(simpleName : String, fqcn: String) {
case class ImportItem(simpleName : String, fqcn: Seq[String]) {
val isOnDemand: Boolean = simpleName == "*"

/**
Expand All @@ -21,9 +21,10 @@ case class ImportItem(simpleName : String, fqcn: String) {
*/
def matches(simpleName: String): Option[String] = {
if (isOnDemand) {
Some(fqcn.replaceAll("\\*", simpleName))
if(fqcn.length == 0) None
else Some(fqcn.take(fqcn.length - 1).appended(simpleName).mkString("."))
} else if (this.simpleName == simpleName) {
Some(fqcn)
Some(fqcn.mkString((".")))
} else {
None
}
Expand Down
32 changes: 16 additions & 16 deletions src/main/scala/onion/compiler/Typing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,25 @@ class Typing(config: CompilerConfig) extends AnyRef with ProcessingUnit[Seq[AST.
val module = unit.module
val moduleName = if (module != null) module.name else null
val imports = Buffer[ImportItem](
ImportItem("*", "java.lang.*"),
ImportItem("*", "java.io.*"),
ImportItem("*", "java.util.*"),
ImportItem("*", "javax.swing.*"),
ImportItem("*", "java.awt.event.*"),
ImportItem("JByte", "java.lang.Byte"),
ImportItem("JShort", "java.lang.Short"),
ImportItem("JCharacter", "java.lang.Character"),
ImportItem("JInteger", "java.lang.Integer"),
ImportItem("JLong", "java.lang.Long"),
ImportItem("JFloat", "java.lang.Float"),
ImportItem("JDouble", "java.lang.Double"),
ImportItem("JBoolean", "java.lang.Boolean"),
ImportItem("*", "onion.*"),
ImportItem("*", if (moduleName != null) moduleName + ".*" else "*")
ImportItem("*", Seq("java", "lang", "*")),
ImportItem("*", Seq("java", "io", "*")),
ImportItem("*", Seq("java", "util", "*")),
ImportItem("*", Seq("javax", "swing", "*")),
ImportItem("*", Seq("java", "awt", "event", "*")),
ImportItem("JByte", Seq("java", "lang", "Byte")),
ImportItem("JShort", Seq("java", "lang", "Short")),
ImportItem("JCharacter", Seq("java", "lang", "Character")),
ImportItem("JInteger", Seq("java", "lang", "Integer")),
ImportItem("JLong", Seq("java", "lang", "Long")),
ImportItem("JFloat", Seq("java", "lang", "Float")),
ImportItem("JDouble", Seq("java", "lang", "Double")),
ImportItem("JBoolean", Seq("java", "lang", "Boolean")),
ImportItem("*", Seq("onion", "*")),
ImportItem("*", if (moduleName != null) moduleName.split("\\.").appended("*")else Seq("*"))
)
if(unit.imports != null) {
for((key, value) <- unit.imports.mapping) {
imports.append(ImportItem(key, value))
imports.append(ImportItem(key, value.split("\\.")))
}
}
val staticList = new StaticImportList
Expand Down
28 changes: 28 additions & 0 deletions src/test/scala/onion/compiler/tools/ImportSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package onion.compiler.tools

import onion.tools.Shell

class ImportSpec extends AbstractShellSpec {
describe("Import a class") {
it("import java.util.*") {
val result = shell.run(
"""
| import {
| java.util.*;
| }
| class Increment {
| public:
| static def main(args: String[]): Int {
| xs = new ArrayList();
| xs.add(new Integer(2));
| return xs.get(0)$Integer.intValue();
| }
| }
""".stripMargin,
"None",
Array()
)
assert(Shell.Success(2) == result)
}
}
}

0 comments on commit 8c2115b

Please sign in to comment.