Learn how to work with Java
The general syntax for Java Foreign Import Declarations is:
1 2 | foreign import java [safety] "[import-string]" [eta-identifier]
:: [arg-type-1] -> [arg-type-2] -> .. -> [return-type]
|
[eta-identifier] should be a valid Eta identifier that will be used for calling the corresponding Java method inside of Eta code.
[argTypeN] should be a marshallable Eta type. See Marshalling Between Java and Eta Types.
Let’s import the boolean canExecute() instance method from the java.io.File class.
The following are all equivalent ways of performing the import:
1 2 3 4 5 6 7 8 9 10 11 12 13 | data File = File @java.io.File
deriving Class
foreign import java unsafe canExecute :: Java File Bool
foreign import java unsafe "canExecute" canExecute1 :: Java File Bool
foreign import java unsafe "canExecute" canExecute2 :: File -> IO Bool
-- Note: The example below is shown for illustration purposes and should never
-- be done in practice because "canExecute" is not a pure function.
foreign import java unsafe "canExecute" canExecute3 :: File -> Bool
|
Let’s import the File createTempFile(String, String) static method from the java.io.File class.
The following are all equivalent ways of performing the import:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | data File = File @java.io.File
deriving Class
foreign import java unsafe "@static java.io.File.createTempFile"
createTempFile :: String -> String -> Java a File
foreign import java unsafe "@static java.io.File.createTempFile"
createTempFile1 :: String -> String -> IO File
-- Note: The example below is shown for illustration purposes and should never
-- be done in practice because "createTempFile" is not a pure function.
foreign import java unsafe "@static java.io.File.createTempFile"
createTempFile2 :: String -> String -> File
|
The example below demonstrates importing a static method with no arguments.
Note that it is similar to the type signature for instance methods with no parameters.
1 2 3 4 5 6 7 8 9 | import Java
foreign import java unsafe "@static java.lang.System.currentTimeMillis"
currentTimeMillis :: IO Int64
main :: IO ()
main = do
now <- currentTimeMillis
print now
|
Let’s import the File(String) constructor from the java.io.File class.
The following are all equivalent ways of performing the import:
1 2 3 4 5 6 7 8 | data File = File @java.io.File
deriving Class
foreign import java unsafe "@new" newFile :: String -> Java a File
foreign import java unsafe "@new" newFile1 :: String -> IO File
foreign import java unsafe "@new" newFile2 :: String -> File
|
Let’s import the private String path instance field from the java.io.File class. Note that the imports shown below are purely for illustration purposes and will throw an exception if called because path is a private field.
The following are all equivalent ways of performing the get/set imports:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | data File = File @java.io.File
deriving Class
-- Imports for getting the field
foreign import java unsafe "@field path" getFilePath :: Java File String
foreign import java unsafe "@field path" getFilePath1 :: File -> IO String
foreign import java unsafe "@field path" getFilePath2 :: File -> String
-- Imports for setting the field.
foreign import java unsafe "@field path" setFilePath :: String -> Java File ()
foreign import java unsafe "@field path" setFilePath1 :: File -> String -> IO ()
-- Note that setting the value of a field is always an impure operation so a pure
-- import is not supported by the compiler
|
Let’s import the String pathSeparator static field from the java.io.File class.
The following are all equivalent ways of performing the get/set imports:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | -- Imports for getting the field
foreign import java unsafe "@static @field java.io.File.pathSeparator"
getPathSeparator :: Java a String
foreign import java unsafe "@static @field java.io.File.pathSeparator"
getPathSeparator1 :: IO String
foreign import java unsafe "@static @field java.io.File.pathSeparator"
getPathSeparator2 :: String
-- Imports for setting the field.
-- NOTE: These imports are only shown for illustration purposes, but they will
-- crash if used since `pathSeparator` is a final field.
foreign import java unsafe "@static @field java.io.File.pathSeparator"
setPathSeparator :: String -> Java a ()
foreign import java unsafe "@static @field java.io.File.pathSeparator"
setPathSeparator1 :: String -> IO ()
|
It is also possible to import Java generics, enums, variable-argument functions, and interfaces with one method (e.g. Runnable, ActionListener). For details on these see Advanced Foreign Imports.