import Data.Binary.Put
import Data.Word
import Data.Char
+import Data.List
import qualified Data.ByteString.Lazy as B
import Debug.Trace
| BoolType -- Z
| ObjectType String -- L <class name>
| Array (Maybe Int) FieldType
- deriving (Eq, Show)
+ deriving (Eq)
+
+instance Show FieldType where
+ show SignedByte = "byte"
+ show CharByte = "char"
+ show DoubleType = "double"
+ show FloatType = "float"
+ show IntType = "int"
+ show LongInt = "long"
+ show ShortInt = "short"
+ show BoolType = "bool"
+ show (ObjectType s) = "Object " ++ s
+ show (Array Nothing t) = show t ++ "[]"
+ show (Array (Just n) t) = show t ++ "[" ++ show n ++ "]"
type FieldSignature = FieldType
data ReturnSignature =
Returns FieldType
| ReturnsVoid
- deriving (Eq, Show)
+ deriving (Eq)
+
+instance Show ReturnSignature where
+ show (Returns t) = show t
+ show ReturnsVoid = "Void"
instance Binary ReturnSignature where
put (Returns sig) = put sig
data MethodSignature =
MethodSignature [ArgumentSignature] ReturnSignature
- deriving (Eq, Show)
+ deriving (Eq)
+
+instance Show MethodSignature where
+ show (MethodSignature args ret) = "(" ++ intercalate ", " (map show args) ++ ") returns " ++ show ret
instance Binary MethodSignature where
put (MethodSignature args ret) = do
instance IsString B.ByteString where
fromString s = B.pack $ map (fromIntegral . ord) $ encodeString s
+toString :: B.ByteString -> String
+toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
+
type Pool = Array Word16 Constant
class HasAttributes a where
ntName :: B.ByteString,
ntSignature :: Signature a }
-deriving instance Show (Signature a) => Show (NameType a)
+instance Show (Signature a) => Show (NameType a) where
+ show (NameType n t) = toString n ++ ": " ++ show t
+
deriving instance Eq (Signature a) => Eq (NameType a)
data Constant =
| CNameType B.ByteString B.ByteString
| CUTF8 {getString :: B.ByteString}
| CUnicode {getString :: B.ByteString}
- deriving (Eq, Show)
+ deriving (Eq)
+
+instance Show Constant where
+ show (CClass name) = "class " ++ toString name
+ show (CField cls nt) = "field " ++ toString cls ++ "." ++ show nt
+ show (CMethod cls nt) = "method " ++ toString cls ++ "." ++ show nt
+ show (CIfaceMethod cls nt) = "interface method " ++ toString cls ++ "." ++ show nt
+ show (CString s) = "String \"" ++ toString s ++ "\""
+ show (CInteger x) = show x
+ show (CFloat x) = show x
+ show (CLong x) = show x
+ show (CDouble x) = show x
+ show (CNameType name tp) = toString name ++ ": " ++ toString tp
+ show (CUTF8 s) = "UTF8 \"" ++ toString s ++ "\""
+ show (CUnicode s) = "Unicode \"" ++ toString s ++ "\""
data Field = Field {
fieldAccess :: Access,
{-# LANGUAGE OverloadedStrings #-}
import Control.Monad
+import Data.Array
import System.Environment
import qualified Data.ByteString.Lazy as B
+import Text.Printf
import Data.BinaryState
import JVM.Types
cls <- decompileFile clspath
putStr "Class: "
B.putStrLn (this cls)
+ putStrLn "Constants pool:"
+ forM_ (assocs $ constantPool cls) $ \(i, c) ->
+ putStrLn $ printf " #%d:\t%s" i (show c)
putStrLn "Methods:"
forM_ (methods cls) $ \m -> do
putStr ">> Method "