Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ImportItem class #94

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}