public class Hello {
public static void main(String[] args) {
- hello(5);
+ hello();
}
- static void hello(int n) {
- System.out.println("Здравствуй, мир!");
- System.out.printf("Argument: %d", n);
+ static void hello() {
+ System.out.println("Hello Java world!");
}
}
i0, i1, i8,
newMethod,
setStackSize, setMaxLocals,
+ withClassPath,
+ getClassField, getClassMethod,
generate
) where
endMethod
return (NameType name sig)
+getClass :: String -> Generate (Class Direct)
+getClass name = do
+ cp <- St.gets classPath
+ res <- liftIO $ getEntry cp name
+ case res of
+ Just (NotLoaded p) -> fail $ "Class file was not loaded: " ++ p
+ Just (Loaded _ c) -> return c
+ Just (NotLoadedJAR p c) -> fail $ "Class was not loaded from JAR " ++ p ++ ": " ++ c
+ Just (LoadedJAR _ c) -> return c
+ Nothing -> fail $ "No such class in ClassPath: " ++ name
+
+getClassField :: String -> B.ByteString -> Generate (NameType Field)
+getClassField clsName fldName = do
+ cls <- getClass clsName
+ case lookupField fldName cls of
+ Just fld -> return (fieldNameType fld)
+ Nothing -> fail $ "No such field in class " ++ clsName ++ ": " ++ toString fldName
+
+getClassMethod :: String -> B.ByteString -> Generate (NameType Method)
+getClassMethod clsName mName = do
+ cls <- getClass clsName
+ case lookupMethod mName cls of
+ Just m -> return (methodNameType m)
+ Nothing -> fail $ "No such method in class " ++ clsName ++ ": " ++ toString mName
+
-- | Convert Generator state to method Code.
genCode :: GState -> Code
genCode st = Code {
HasSignature (..), HasAttributes (..),
NameType (..),
fieldNameType, methodNameType,
+ lookupField, lookupMethod,
toString,
className,
apsize, arsize, arlist
deriving instance Show (Field File)
deriving instance Show (Field Direct)
+lookupField :: B.ByteString -> Class Direct -> Maybe (Field Direct)
+lookupField name cls = look (classFields cls)
+ where
+ look [] = Nothing
+ look (f:fs)
+ | fieldName f == name = Just f
+ | otherwise = look fs
+
fieldNameType :: Field Direct -> NameType Field
fieldNameType f = NameType (fieldName f) (fieldSignature f)
methodNameType :: Method Direct -> NameType Method
methodNameType m = NameType (methodName m) (methodSignature m)
+lookupMethod :: B.ByteString -> Class Direct -> Maybe (Method Direct)
+lookupMethod name cls = look (classMethods cls)
+ where
+ look [] = Nothing
+ look (f:fs)
+ | methodName f == name = Just f
+ | otherwise = look fs
+
instance Binary (Method File) where
put (Method {..}) = do
put methodAccessFlags
import JVM.Converter
import JVM.Assembler
import JVM.Builder
+import Java.ClassPath
import qualified Java.Lang
import qualified Java.IO
test :: Generate ()
test = do
+ withClassPath $ do
+ addDirectory "."
+
+ helloJava <- getClassMethod "./Hello" "hello"
+
newMethod [ACC_PUBLIC] "<init>" [] ReturnsVoid $ do
setStackSize 1
invokeStatic Java.Lang.integer Java.Lang.valueOfInteger
aastore
invokeVirtual Java.IO.printStream Java.IO.printf
+ invokeStatic "Hello" helloJava
pop
i0 RETURN