1 {-# LANGUAGE TypeFamilies, StandaloneDeriving, FlexibleInstances, FlexibleContexts, UndecidableInstances, RecordWildCards, OverloadedStrings #-}
2 -- | Functions to convert from low-level .class format representation and
3 -- high-level Java classes, methods etc representation
5 (decompile, decompileFile,
17 import qualified Data.ByteString.Lazy as B
19 import qualified Data.Set as S
20 import qualified Data.Map as M
25 -- | Parse .class file data
26 decompile :: B.ByteString -> Class
27 decompile bstr = convertClass $ decode bstr
29 -- | Parse class data from file
30 decompileFile :: FilePath -> IO Class
31 decompileFile path = convertClass `fmap` decodeFile path
33 convertClass :: ClassFile -> Class
34 convertClass (ClassFile {..}) =
35 let pool = constantPoolArray constsPool
36 superName = className $ pool ! superClass
39 classAccess = convertAccess accessFlags,
40 this = className $ pool ! thisClass,
41 super = if superClass == 0 then Nothing else Just superName,
42 implements = map (\i -> className $ pool ! i) interfaces,
43 fields = map (convertField pool) classFields,
44 methods = map (convertMethod pool) classMethods,
45 classAttrs = convertAttrs pool classAttributes }
47 constantPoolArray :: [CpInfo] -> Pool
48 constantPoolArray list = pool
51 pool = listArray (1,n) $ map convert list
52 n = fromIntegral $ length list
54 convertNameType :: (HasSignature a, Binary (Signature a)) => Word16 -> NameType a
56 let (CNameType n s) = pool ! i
57 in NameType n (decode s)
59 convert (CONSTANT_Class i) = CClass $ getString $ pool ! i
60 convert (CONSTANT_Fieldref i j) = CField (className $ pool ! i) (convertNameType j)
61 convert (CONSTANT_Methodref i j) = CMethod (className $ pool ! i) (convertNameType j)
62 convert (CONSTANT_InterfaceMethodref i j) = CIfaceMethod (className $ pool ! i) (convertNameType j)
63 convert (CONSTANT_String i) = CString $ getString $ pool ! i
64 convert (CONSTANT_Integer x) = CInteger x
65 convert (CONSTANT_Float x) = CFloat x
66 convert (CONSTANT_Long x) = CLong (fromIntegral x)
67 convert (CONSTANT_Double x) = CDouble x
68 convert (CONSTANT_NameAndType i j) = CNameType (getString $ pool ! i) (getString $ pool ! j)
69 convert (CONSTANT_Utf8 _ bs) = CUTF8 bs
70 convert (CONSTANT_Unicode _ bs) = CUnicode bs
72 convertAccess :: Word16 -> Access
73 convertAccess w = S.fromList $ concat $ zipWith (\i f -> if testBit w i then [f] else []) [0..] $ [
86 convertField :: Pool -> FieldInfo -> Field
87 convertField pool (FieldInfo {..}) = Field {
88 fieldAccess = convertAccess fieldAccessFlags,
89 fieldName = getString $ pool ! fieldNameIndex,
90 fieldSignature = decode $ getString $ pool ! fieldSignatureIndex,
91 fieldAttrs = convertAttrs pool fieldAttributes }
93 convertMethod :: Pool -> MethodInfo -> Method
94 convertMethod pool (MethodInfo {..}) = Method {
95 methodAccess = convertAccess methodAccessFlags,
96 methodName = getString $ pool ! methodNameIndex,
97 methodSignature = decode $ getString $ pool ! methodSignatureIndex,
98 methodAttrs = convertAttrs pool methodAttributes }
100 convertAttrs :: Pool -> [AttributeInfo] -> Attributes
101 convertAttrs pool attrs = M.fromList $ map go attrs
103 go (AttributeInfo {..}) = (getString $ pool ! attributeName,
106 -- | Try to get class method by name
107 methodByName :: Class -> B.ByteString -> Maybe Method
108 methodByName cls name =
109 find (\m -> methodName m == name) (methods cls)
111 -- | Try to get object attribute by name
112 attrByName :: (HasAttributes a) => a -> B.ByteString -> Maybe B.ByteString
113 attrByName x name = M.lookup name (attributes x)
115 -- | Try to get Code for class method (no Code for interface methods)
117 -> B.ByteString -- ^ Method name
118 -> Maybe B.ByteString
119 methodCode cls name = do
120 method <- methodByName cls name
121 attrByName method "Code"