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,
19 import qualified Data.ByteString.Lazy as B
21 import qualified Data.Set as S
22 import qualified Data.Map as M
29 -- | Parse .class file data
30 decompile :: B.ByteString -> Class
31 decompile bstr = convertClass $ decode bstr
33 -- | Parse class data from file
34 decompileFile :: FilePath -> IO Class
35 decompileFile path = convertClass `fmap` decodeFile path
37 convertClass :: ClassFile -> Class
38 convertClass (ClassFile {..}) =
39 let pool = constantPoolArray constsPool
40 superName = className $ pool ! superClass
43 classAccess = convertAccess accessFlags,
44 this = className $ pool ! thisClass,
45 super = if superClass == 0 then Nothing else Just superName,
46 implements = map (\i -> className $ pool ! i) interfaces,
47 fields = map (convertField pool) classFields,
48 methods = map (convertMethod pool) classMethods,
49 classAttrs = convertAttrs pool classAttributes }
51 constantPoolArray :: [CpInfo] -> Pool
52 constantPoolArray list = pool
55 pool = listArray (1,n) $ map convert list
56 n = fromIntegral $ length list
58 convertNameType :: (HasSignature a, Binary (Signature a)) => Word16 -> NameType a
60 let (CNameType n s) = pool ! i
61 in NameType n (decode s)
63 convert (CONSTANT_Class i) = CClass $ getString $ pool ! i
64 convert (CONSTANT_Fieldref i j) = CField (className $ pool ! i) (convertNameType j)
65 convert (CONSTANT_Methodref i j) = CMethod (className $ pool ! i) (convertNameType j)
66 convert (CONSTANT_InterfaceMethodref i j) = CIfaceMethod (className $ pool ! i) (convertNameType j)
67 convert (CONSTANT_String i) = CString $ getString $ pool ! i
68 convert (CONSTANT_Integer x) = CInteger x
69 convert (CONSTANT_Float x) = CFloat x
70 convert (CONSTANT_Long x) = CLong (fromIntegral x)
71 convert (CONSTANT_Double x) = CDouble x
72 convert (CONSTANT_NameAndType i j) = CNameType (getString $ pool ! i) (getString $ pool ! j)
73 convert (CONSTANT_Utf8 _ bs) = CUTF8 bs
74 convert (CONSTANT_Unicode _ bs) = CUnicode bs
76 convertAccess :: Word16 -> Access
77 convertAccess w = S.fromList $ concat $ zipWith (\i f -> if testBit w i then [f] else []) [0..] $ [
90 convertField :: Pool -> FieldInfo -> Field
91 convertField pool (FieldInfo {..}) = Field {
92 fieldAccess = convertAccess fieldAccessFlags,
93 fieldName = getString $ pool ! fieldNameIndex,
94 fieldSignature = decode $ getString $ pool ! fieldSignatureIndex,
95 fieldAttrs = convertAttrs pool fieldAttributes }
97 convertMethod :: Pool -> MethodInfo -> Method
98 convertMethod pool (MethodInfo {..}) = Method {
99 methodAccess = convertAccess methodAccessFlags,
100 methodName = getString $ pool ! methodNameIndex,
101 methodSignature = decode $ getString $ pool ! methodSignatureIndex,
102 methodAttrs = convertAttrs pool methodAttributes }
104 convertAttrs :: Pool -> [AttributeInfo] -> Attributes
105 convertAttrs pool attrs = M.fromList $ map go attrs
107 go (AttributeInfo {..}) = (getString $ pool ! attributeName,
110 -- | Try to get class method by name
111 methodByName :: Class -> B.ByteString -> Maybe Method
112 methodByName cls name =
113 find (\m -> methodName m == name) (methods cls)
115 -- | Try to get object attribute by name
116 attrByName :: (HasAttributes a) => a -> B.ByteString -> Maybe B.ByteString
117 attrByName x name = M.lookup name (attributes x)
119 -- | Try to get Code for class method (no Code for interface methods)
121 -> B.ByteString -- ^ Method name
122 -> Maybe B.ByteString
123 methodCode cls name = do
124 method <- methodByName cls name
125 attrByName method "Code"