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 (parseClass, parseClassFile,
6 classFile2Direct, classDirect2File,
14 import Control.Monad.Exception
19 import qualified Data.ByteString.Lazy as B
20 import qualified Data.Set as S
21 import qualified Data.Map as M
27 -- | Parse .class file data
28 parseClass :: B.ByteString -> Class Direct
29 parseClass bstr = classFile2Direct $ decode bstr
31 -- | Parse class data from file
32 parseClassFile :: FilePath -> IO (Class Direct)
33 parseClassFile path = classFile2Direct `fmap` decodeFile path
35 encodeClass :: (Class Direct) -> B.ByteString
36 encodeClass cls = encode $ classDirect2File cls
38 classFile2Direct :: Class File -> Class Direct
39 classFile2Direct (Class {..}) =
40 let pool = poolFile2Direct constsPool
41 superName = className $ pool ! superClass
46 constsPoolSize = fromIntegral (M.size pool),
48 accessFlags = accessFile2Direct accessFlags,
49 thisClass = className $ pool ! thisClass,
50 superClass = if superClass == 0 then "" else superName,
51 interfacesCount = interfacesCount,
52 interfaces = map (\i -> className $ pool ! i) interfaces,
53 classFieldsCount = classFieldsCount,
54 classFields = map (fieldFile2Direct pool) classFields,
55 classMethodsCount = classMethodsCount,
56 classMethods = map (methodFile2Direct pool) classMethods,
57 classAttributesCount = classAttributesCount,
58 classAttributes = attributesFile2Direct pool classAttributes }
60 classDirect2File :: Class Direct -> Class File
61 classDirect2File (Class {..}) = Class {
65 constsPoolSize = fromIntegral (M.size poolInfo + 1),
66 constsPool = poolInfo,
67 accessFlags = access2word16 accessFlags,
68 thisClass = force "this" $ poolClassIndex poolInfo thisClass,
69 superClass = force "super" $ poolClassIndex poolInfo superClass,
70 interfacesCount = fromIntegral (length interfaces),
71 interfaces = map (force "ifaces" . poolIndex poolInfo) interfaces,
72 classFieldsCount = fromIntegral (length classFields),
73 classFields = map (fieldDirect2File poolInfo) classFields,
74 classMethodsCount = fromIntegral (length classMethods),
75 classMethods = map (methodDirect2File poolInfo) classMethods,
76 classAttributesCount = fromIntegral $ arsize classAttributes,
77 classAttributes = to (arlist classAttributes) }
79 poolInfo = poolDirect2File constsPool
80 to :: [(B.ByteString, B.ByteString)] -> Attributes File
81 to pairs = AP (map (attrInfo poolInfo) pairs)
83 poolDirect2File :: Pool Direct -> Pool File
84 poolDirect2File pool = result
86 result = M.map cpInfo pool
88 cpInfo :: Constant Direct -> Constant File
89 cpInfo (CClass name) = CClass (force "class" $ poolIndex result name)
90 cpInfo (CField cls name) =
91 CField (force "field a" $ poolClassIndex result cls) (force "field b" $ poolNTIndex result name)
92 cpInfo (CMethod cls name) =
93 CMethod (force "method a" $ poolClassIndex result cls) (force ("method b: " ++ show name) $ poolNTIndex result name)
94 cpInfo (CIfaceMethod cls name) =
95 CIfaceMethod (force "iface method a" $ poolIndex result cls) (force "iface method b" $ poolNTIndex result name)
96 cpInfo (CString s) = CString (force "string" $ poolIndex result s)
97 cpInfo (CInteger x) = CInteger x
98 cpInfo (CFloat x) = CFloat x
99 cpInfo (CLong x) = CLong (fromIntegral x)
100 cpInfo (CDouble x) = CDouble x
101 cpInfo (CNameType n t) =
102 CNameType (force "name" $ poolIndex result n) (force "type" $ poolIndex result t)
103 cpInfo (CUTF8 s) = CUTF8 s
104 cpInfo (CUnicode s) = CUnicode s
106 -- | Find index of given string in the list of constants
107 poolIndex :: (Throws NoItemInPool e) => Pool File -> B.ByteString -> EM e Word16
108 poolIndex list name = case findIndex test (M.elems list) of
109 Nothing -> throw (NoItemInPool name)
110 Just i -> return $ fromIntegral $ i+1
112 test (CUTF8 s) | s == name = True
113 test (CUnicode s) | s == name = True
116 -- | Find index of given string in the list of constants
117 poolClassIndex :: (Throws NoItemInPool e) => Pool File -> B.ByteString -> EM e Word16
118 poolClassIndex list name = case findIndex checkString (M.elems list) of
119 Nothing -> throw (NoItemInPool name)
120 Just i -> case findIndex (checkClass $ fromIntegral $ i+1) (M.elems list) of
121 Nothing -> throw (NoItemInPool $ i+1)
122 Just j -> return $ fromIntegral $ j+1
124 checkString (CUTF8 s) | s == name = True
125 checkString (CUnicode s) | s == name = True
126 checkString _ = False
128 checkClass i (CClass x) | i == x = True
129 checkClass _ _ = False
131 poolNTIndex list x@(NameType n t) = do
132 ni <- poolIndex list n
133 ti <- poolIndex list (byteString t)
134 case findIndex (check ni ti) (M.elems list) of
135 Nothing -> throw (NoItemInPool x)
136 Just i -> return $ fromIntegral (i+1)
138 check ni ti (CNameType n' t')
139 | (ni == n') && (ti == t') = True
142 fieldDirect2File :: Pool File -> Field Direct -> Field File
143 fieldDirect2File pool (Field {..}) = Field {
144 fieldAccessFlags = access2word16 fieldAccessFlags,
145 fieldName = force "field name" $ poolIndex pool fieldName,
146 fieldSignature = force "signature" $ poolIndex pool (encode fieldSignature),
147 fieldAttributesCount = fromIntegral (arsize fieldAttributes),
148 fieldAttributes = to (arlist fieldAttributes) }
150 to :: [(B.ByteString, B.ByteString)] -> Attributes File
151 to pairs = AP (map (attrInfo pool) pairs)
153 methodDirect2File :: Pool File -> Method Direct -> Method File
154 methodDirect2File pool (Method {..}) = Method {
155 methodAccessFlags = access2word16 methodAccessFlags,
156 methodName = force "method name" $ poolIndex pool methodName,
157 methodSignature = force "method sig" $ poolIndex pool (encode methodSignature),
158 methodAttributesCount = fromIntegral (arsize methodAttributes),
159 methodAttributes = to (arlist methodAttributes) }
161 to :: [(B.ByteString, B.ByteString)] -> Attributes File
162 to pairs = AP (map (attrInfo pool) pairs)
164 attrInfo :: Pool File -> (B.ByteString, B.ByteString) -> Attribute
165 attrInfo pool (name, value) = Attribute {
166 attributeName = force "attr name" $ poolIndex pool name,
167 attributeLength = fromIntegral (B.length value),
168 attributeValue = value }
170 poolFile2Direct :: Pool File -> Pool Direct
171 poolFile2Direct ps = pool
174 pool = M.map convert ps
176 n = fromIntegral $ M.size ps
178 convertNameType :: (HasSignature a) => Word16 -> NameType a
180 let (CNameType n s) = pool ! i
181 in NameType n (decode s)
183 convert (CClass i) = CClass $ getString $ pool ! i
184 convert (CField i j) = CField (className $ pool ! i) (convertNameType j)
185 convert (CMethod i j) = CMethod (className $ pool ! i) (convertNameType j)
186 convert (CIfaceMethod i j) = CIfaceMethod (className $ pool ! i) (convertNameType j)
187 convert (CString i) = CString $ getString $ pool ! i
188 convert (CInteger x) = CInteger x
189 convert (CFloat x) = CFloat x
190 convert (CLong x) = CLong (fromIntegral x)
191 convert (CDouble x) = CDouble x
192 convert (CNameType i j) = CNameType (getString $ pool ! i) (getString $ pool ! j)
193 convert (CUTF8 bs) = CUTF8 bs
194 convert (CUnicode bs) = CUnicode bs
196 accessFile2Direct :: AccessFlags File -> AccessFlags Direct
197 accessFile2Direct w = S.fromList $ concat $ zipWith (\i f -> if testBit w i then [f] else []) [0..] $ [
210 access2word16 :: AccessFlags Direct -> AccessFlags File
211 access2word16 fs = bitsOr $ map toBit $ S.toList fs
213 bitsOr = foldl (.|.) 0
214 toBit f = 1 `shiftL` (fromIntegral $ fromEnum f)
216 fieldFile2Direct :: Pool Direct -> Field File -> Field Direct
217 fieldFile2Direct pool (Field {..}) = Field {
218 fieldAccessFlags = accessFile2Direct fieldAccessFlags,
219 fieldName = getString $ pool ! fieldName,
220 fieldSignature = decode $ getString $ pool ! fieldSignature,
221 fieldAttributesCount = fromIntegral (apsize fieldAttributes),
222 fieldAttributes = attributesFile2Direct pool fieldAttributes }
224 methodFile2Direct :: Pool Direct -> Method File -> Method Direct
225 methodFile2Direct pool (Method {..}) = Method {
226 methodAccessFlags = accessFile2Direct methodAccessFlags,
227 methodName = getString $ pool ! methodName,
228 methodSignature = decode $ getString $ pool ! methodSignature,
229 methodAttributesCount = fromIntegral (apsize methodAttributes),
230 methodAttributes = attributesFile2Direct pool methodAttributes }
232 attributesFile2Direct :: Pool Direct -> Attributes File -> Attributes Direct
233 attributesFile2Direct pool (AP attrs) = AR (M.fromList $ map go attrs)
235 go :: Attribute -> (B.ByteString, B.ByteString)
236 go (Attribute {..}) = (getString $ pool ! attributeName,
239 -- | Try to get class method by name
240 methodByName :: Class Direct -> B.ByteString -> Maybe (Method Direct)
241 methodByName cls name =
242 find (\m -> methodName m == name) (classMethods cls)
244 -- | Try to get object attribute by name
245 attrByName :: (HasAttributes a) => a Direct -> B.ByteString -> Maybe B.ByteString
247 let (AR m) = attributes x
250 -- | Try to get Code for class method (no Code for interface methods)
251 methodCode :: Class Direct
252 -> B.ByteString -- ^ Method name
253 -> Maybe B.ByteString
254 methodCode cls name = do
255 method <- methodByName cls name
256 attrByName method "Code"