1 {-# LANGUAGE TypeFamilies, StandaloneDeriving, FlexibleInstances, FlexibleContexts, UndecidableInstances #-}
2 -- | This module declares `high-level' data types for Java classes, methods etc.
5 import Codec.Binary.UTF8.String hiding (encode, decode)
6 import Control.Applicative
10 import qualified Data.ByteString.Lazy as B
13 import qualified Data.Set as S
14 import qualified Data.Map as M
18 instance IsString B.ByteString where
19 fromString s = B.pack $ map (fromIntegral . ord) $ encodeString s
21 toString :: B.ByteString -> String
22 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
24 toCharList :: B.ByteString -> [Int]
25 toCharList bstr = map fromIntegral $ B.unpack bstr
28 type Pool = Array Word16 Constant
30 asize :: (Ix i) => Array i e -> Int
31 asize = length . elems
33 showListIx :: (Show a) => [a] -> String
34 showListIx list = unlines $ zipWith s [1..] list
35 where s i x = show i ++ ":\t" ++ show x
37 class HasAttributes a where
38 attributes :: a -> Attributes
43 classAccess :: Access,
44 this :: B.ByteString, -- ^ this class name
45 super :: Maybe B.ByteString, -- ^ super class name
46 implements :: [B.ByteString], -- ^ implemented interfaces
49 classAttrs :: Attributes
53 instance HasAttributes Class where
54 attributes = classAttrs
56 class HasSignature a where
59 -- | Name and signature pair. Used for methods and fields.
60 data NameType a = NameType {
61 ntName :: B.ByteString,
62 ntSignature :: Signature a }
64 instance Show (Signature a) => Show (NameType a) where
65 show (NameType n t) = toString n ++ ": " ++ show t
67 deriving instance Eq (Signature a) => Eq (NameType a)
69 -- | Constant pool item
72 | CField {refClass :: B.ByteString, fieldNameType :: NameType Field}
73 | CMethod {refClass :: B.ByteString, nameType :: NameType Method}
74 | CIfaceMethod {refClass :: B.ByteString, nameType :: NameType Method}
75 | CString B.ByteString
80 | CNameType B.ByteString B.ByteString
81 | CUTF8 {getString :: B.ByteString}
82 | CUnicode {getString :: B.ByteString}
85 className :: Constant -> B.ByteString
86 className (CClass s) = s
87 className x = error $ "Not a class: " ++ show x
89 instance Show Constant where
90 show (CClass name) = "class " ++ toString name
91 show (CField cls nt) = "field " ++ toString cls ++ "." ++ show nt
92 show (CMethod cls nt) = "method " ++ toString cls ++ "." ++ show nt
93 show (CIfaceMethod cls nt) = "interface method " ++ toString cls ++ "." ++ show nt
94 show (CString s) = "String \"" ++ toString s ++ "\""
95 show (CInteger x) = show x
96 show (CFloat x) = show x
97 show (CLong x) = show x
98 show (CDouble x) = show x
99 show (CNameType name tp) = toString name ++ ": " ++ toString tp
100 show (CUTF8 s) = "UTF8 \"" ++ toString s ++ "\""
101 show (CUnicode s) = "Unicode \"" ++ toString s ++ "\""
105 fieldAccess :: Access,
106 fieldName :: B.ByteString,
107 fieldSignature :: FieldSignature,
108 fieldAttrs :: Attributes }
111 instance HasSignature Field where
112 type Signature Field = FieldSignature
114 instance HasAttributes Field where
115 attributes = fieldAttrs
118 data Method = Method {
119 methodAccess :: Access,
120 methodName :: B.ByteString,
121 methodSignature :: MethodSignature,
122 methodAttrs :: Attributes }
125 instance HasSignature Method where
126 type Signature Method = MethodSignature
128 instance HasAttributes Method where
129 attributes = methodAttrs
131 -- | Set of access flags
132 type Access = S.Set AccessFlag
134 -- | Access flags. Used for classess, methods, variables.
136 ACC_PUBLIC -- ^ 0x0001 Visible for all
137 | ACC_PRIVATE -- ^ 0x0002 Visible only for defined class
138 | ACC_PROTECTED -- ^ 0x0004 Visible only for subclasses
139 | ACC_STATIC -- ^ 0x0008 Static method or variable
140 | ACC_FINAL -- ^ 0x0010 No further subclassing or assignments
141 | ACC_SYNCHRONIZED -- ^ 0x0020 Uses monitors
142 | ACC_VOLATILE -- ^ 0x0040 Could not be cached
143 | ACC_TRANSIENT -- ^ 0x0080
144 | ACC_NATIVE -- ^ 0x0100 Implemented in other language
145 | ACC_INTERFACE -- ^ 0x0200 Class is interface
146 | ACC_ABSTRACT -- ^ 0x0400
147 deriving (Eq, Show, Ord, Enum)
149 -- | Generic attribute
150 data Attribute = Attribute {
151 attrName :: B.ByteString,
152 attrValue :: B.ByteString }
155 -- | Set of attributes
156 type Attributes = M.Map B.ByteString B.ByteString
158 instance (Binary (Signature a)) => Binary (NameType a) where
159 put (NameType n t) = putLazyByteString n >> put t
161 get = NameType <$> get <*> get
163 byteString :: (Binary t) => t -> B.ByteString
164 byteString x = runPut (put x)