unageanu-javaclass 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,194 +1,194 @@
1
- require "javaclass/base"
2
-
3
- module JavaClass
4
-
5
- #
6
- #=== Field,Methodの基底クラス
7
- #
8
- class Member
9
- include JavaClass::Base
10
- include JavaClass::Converters
11
- include JavaClass::Item
12
-
13
- #
14
- #===コンストラクタ
15
- #
16
- #*java_class::メンバーの所有者であるJavaクラス
17
- #
18
- def initialize( java_class )
19
- @java_class = java_class
20
- @attributes = {}
21
- end
22
- #
23
- #===名前を取得する。
24
- #
25
- #<b>戻り値</b>::名前
26
- #
27
- def name
28
- @java_class.get_constant_value(@name_index)
29
- end
30
- #
31
- #===ディスクリプタを取得する。
32
- #
33
- #<b>戻り値</b>::ディスクリプタ
34
- #
35
- def descriptor
36
- @java_class.get_constant_value(@descriptor_index)
37
- end
38
- def to_bytes()
39
- bytes = @access_flag.to_bytes()
40
- bytes += to_byte( @name_index, 2)
41
- bytes += to_byte( @descriptor_index, 2)
42
- bytes += to_byte( @attributes.size, 2)
43
- @attributes.keys.sort!.each {|k|
44
- bytes += @attributes[k].to_bytes()
45
- }
46
- return bytes
47
- end
48
- # JavaClass
49
- attr :java_class, true
50
- # アクセスフラグ
51
- attr :access_flag, true
52
- # 名前を示すconstant_poolのインデックス
53
- attr :name_index, true
54
- # ディスクリプタを示すconstant_poolのインデックス
55
- attr :descriptor_index, true
56
- # 属性名をキーとする属性のハッシュ
57
- attr :attributes, true
58
- end
59
-
60
- #
61
- #=== Field
62
- #
63
- class Field < Member
64
- #
65
- #===コンストラクタ
66
- #
67
- #*java_class::Fieldの所有者であるJavaクラス
68
- #
69
- def initialize( java_class )
70
- super( java_class )
71
- end
72
- #
73
- #=== 定数フィールドの初期値を取得する
74
- #
75
- #<b>戻り値</b>::定数フィールドの初期値。定数でない場合や初期値が設定されていない場合nil
76
- #
77
- def static_value
78
- (attributes.key? "ConstantValue") ? attributes["ConstantValue"].value : nil
79
- end
80
- def to_s
81
- str = ""
82
- str << attributes["Signature"].to_s << "\n" if attributes.key? "Signature"
83
- str << "// !deprecated!\n" if deprecated?
84
- str << annotations.inject( "" ){|s, e| s << e.to_s << "\n" }
85
- datas = []
86
- datas << access_flag.to_s if access_flag.to_s.length > 0
87
- datas << convert_field_descriptor(descriptor)
88
- datas << name
89
- str << datas.join(" ")
90
- str << " = " << attributes["ConstantValue"].to_s if attributes.key? "ConstantValue"
91
- return str
92
- end
93
- end
94
-
95
- #
96
- #=== Method
97
- #
98
- class Method < Member
99
- #
100
- #===コンストラクタ
101
- #
102
- #*java_class::Methodの所有者であるJavaクラス
103
- #
104
- def initialize( java_class )
105
- super( java_class )
106
- end
107
-
108
- #
109
- #=== メソッドで発生する例外のクラス名を配列で取得する
110
- #
111
- #<b>戻り値</b>::メソッドで発生する例外クラス名の配列
112
- #
113
- def exceptions
114
- (attributes.key? "Exceptions") ?
115
- attributes["Exceptions"].exceptions.map{|i|i.name} : []
116
- end
117
- #
118
- #=== 引数のクラス名を配列で取得する
119
- #
120
- #<b>戻り値</b>::引数のクラス名の配列
121
- #
122
- def parameters
123
- convert_method_descriptor( descriptor )[:args]
124
- end
125
- #
126
- #=== メソッドの戻り値クラス名を取得する
127
- #
128
- #<b>戻り値</b>::メソッドの戻り値クラス名
129
- #
130
- def return_type
131
- convert_method_descriptor( descriptor )[:return]
132
- end
133
- #
134
- #===指定したパラメータに設定されているアノテーションを配列で取得する。
135
- #
136
- #<b>戻り値</b>::アノテーションの配列
137
- #
138
- def parameter_annotations(index)
139
- ['RuntimeVisibleParameterAnnotations',
140
- 'RuntimeInvisibleParameterAnnotations'].inject([]) { |l, k|
141
- l.concat( attributes[k][index] ) if attributes.key? k
142
- l
143
- }
144
- end
145
-
146
- def to_s
147
- str = ""
148
- str << attributes["Signature"].to_s << "\n" if attributes.key? "Signature"
149
- str << "// !deprecated!\n" if deprecated?
150
- str << annotations.inject( "" ){|s, e| s << e.to_s << "\n" }
151
- d = convert_method_descriptor( descriptor )
152
- i = 0
153
- args = d[:args].map(){|item|
154
- a = parameter_annotations(i)
155
- tmp = a.length > 0 ? a.map(){|a| a.to_s}.join("\n") << " " : ""
156
- i+=1
157
- tmp << "#{item} arg#{i}"
158
- }.join(", ")
159
- datas = []
160
- datas << access_flag.to_s if access_flag.to_s.length > 0
161
- datas << d[:return]
162
- datas << name
163
- datas << "("
164
- datas << args
165
- datas << ")"
166
- str << datas.join(" ")
167
- str << "\n" << attributes["Exceptions"].to_s if attributes.key? "Exceptions"
168
- if ( attributes.key? "Code")
169
- str << " {\n"
170
- # codes = attributes["Code"]
171
- # local_types = codes.attributes["LocalVariableTypeTable"] if codes.attributes.key? "LocalVariableTypeTable"
172
- #
173
- # if codes.attributes.key? "LocalVariableTable"
174
- # codes.attributes["LocalVariableTable"].local_variable_table.each {|l|
175
- # type = local_types.find_by_index(l.index) if local_types != nil
176
- # str << " // signature " << type.signature << "\n" if type != nil
177
- # str << " " << convert_field_descriptor(l.descriptor)
1
+ require "javaclass/base"
2
+
3
+ module JavaClass
4
+
5
+ #
6
+ #=== Field,Methodの基底クラス
7
+ #
8
+ class Member
9
+ include JavaClass::Base
10
+ include JavaClass::Converters
11
+ include JavaClass::Item
12
+
13
+ #
14
+ #===コンストラクタ
15
+ #
16
+ #*java_class::メンバーの所有者であるJavaクラス
17
+ #
18
+ def initialize( java_class )
19
+ @java_class = java_class
20
+ @attributes = {}
21
+ end
22
+ #
23
+ #===名前を取得する。
24
+ #
25
+ #<b>戻り値</b>::名前
26
+ #
27
+ def name
28
+ @java_class.get_constant_value(@name_index)
29
+ end
30
+ #
31
+ #===ディスクリプタを取得する。
32
+ #
33
+ #<b>戻り値</b>::ディスクリプタ
34
+ #
35
+ def descriptor
36
+ @java_class.get_constant_value(@descriptor_index)
37
+ end
38
+ def to_bytes()
39
+ bytes = @access_flag.to_bytes()
40
+ bytes += to_byte( @name_index, 2)
41
+ bytes += to_byte( @descriptor_index, 2)
42
+ bytes += to_byte( @attributes.size, 2)
43
+ @attributes.keys.sort!.each {|k|
44
+ bytes += @attributes[k].to_bytes()
45
+ }
46
+ return bytes
47
+ end
48
+ # JavaClass
49
+ attr :java_class, true
50
+ # アクセスフラグ
51
+ attr :access_flag, true
52
+ # 名前を示すconstant_poolのインデックス
53
+ attr :name_index, true
54
+ # ディスクリプタを示すconstant_poolのインデックス
55
+ attr :descriptor_index, true
56
+ # 属性名をキーとする属性のハッシュ
57
+ attr :attributes, true
58
+ end
59
+
60
+ #
61
+ #=== Field
62
+ #
63
+ class Field < Member
64
+ #
65
+ #===コンストラクタ
66
+ #
67
+ #*java_class::Fieldの所有者であるJavaクラス
68
+ #
69
+ def initialize( java_class )
70
+ super( java_class )
71
+ end
72
+ #
73
+ #=== 定数フィールドの初期値を取得する
74
+ #
75
+ #<b>戻り値</b>::定数フィールドの初期値。定数でない場合や初期値が設定されていない場合nil
76
+ #
77
+ def static_value
78
+ (attributes.key? "ConstantValue") ? attributes["ConstantValue"].value : nil
79
+ end
80
+ def to_s
81
+ str = ""
82
+ str << attributes["Signature"].to_s << "\n" if attributes.key? "Signature"
83
+ str << "// !deprecated!\n" if deprecated?
84
+ str << annotations.inject( "" ){|s, e| s << e.to_s << "\n" }
85
+ datas = []
86
+ datas << access_flag.to_s if access_flag.to_s.length > 0
87
+ datas << convert_field_descriptor(descriptor)
88
+ datas << name
89
+ str << datas.join(" ")
90
+ str << " = " << attributes["ConstantValue"].to_s if attributes.key? "ConstantValue"
91
+ return str
92
+ end
93
+ end
94
+
95
+ #
96
+ #=== Method
97
+ #
98
+ class Method < Member
99
+ #
100
+ #===コンストラクタ
101
+ #
102
+ #*java_class::Methodの所有者であるJavaクラス
103
+ #
104
+ def initialize( java_class )
105
+ super( java_class )
106
+ end
107
+
108
+ #
109
+ #=== メソッドで発生する例外のクラス名を配列で取得する
110
+ #
111
+ #<b>戻り値</b>::メソッドで発生する例外クラス名の配列
112
+ #
113
+ def exceptions
114
+ (attributes.key? "Exceptions") ?
115
+ attributes["Exceptions"].exceptions.map{|i|i.name} : []
116
+ end
117
+ #
118
+ #=== 引数のクラス名を配列で取得する
119
+ #
120
+ #<b>戻り値</b>::引数のクラス名の配列
121
+ #
122
+ def parameters
123
+ convert_method_descriptor( descriptor )[:args]
124
+ end
125
+ #
126
+ #=== メソッドの戻り値クラス名を取得する
127
+ #
128
+ #<b>戻り値</b>::メソッドの戻り値クラス名
129
+ #
130
+ def return_type
131
+ convert_method_descriptor( descriptor )[:return]
132
+ end
133
+ #
134
+ #===指定したパラメータに設定されているアノテーションを配列で取得する。
135
+ #
136
+ #<b>戻り値</b>::アノテーションの配列
137
+ #
138
+ def parameter_annotations(index)
139
+ ['RuntimeVisibleParameterAnnotations',
140
+ 'RuntimeInvisibleParameterAnnotations'].inject([]) { |l, k|
141
+ l.concat( attributes[k][index] ) if attributes.key? k
142
+ l
143
+ }
144
+ end
145
+
146
+ def to_s
147
+ str = ""
148
+ str << attributes["Signature"].to_s << "\n" if attributes.key? "Signature"
149
+ str << "// !deprecated!\n" if deprecated?
150
+ str << annotations.inject( "" ){|s, e| s << e.to_s << "\n" }
151
+ d = convert_method_descriptor( descriptor )
152
+ i = 0
153
+ args = d[:args].map(){|item|
154
+ a = parameter_annotations(i)
155
+ tmp = a.length > 0 ? a.map(){|a| a.to_s}.join("\n") << " " : ""
156
+ i+=1
157
+ tmp << "#{item} arg#{i}"
158
+ }.join(", ")
159
+ datas = []
160
+ datas << access_flag.to_s if access_flag.to_s.length > 0
161
+ datas << d[:return]
162
+ datas << name
163
+ datas << "("
164
+ datas << args
165
+ datas << ")"
166
+ str << datas.join(" ")
167
+ str << "\n" << attributes["Exceptions"].to_s if attributes.key? "Exceptions"
168
+ if ( attributes.key? "Code")
169
+ str << " {\n"
170
+ # codes = attributes["Code"]
171
+ # local_types = codes.attributes["LocalVariableTypeTable"] if codes.attributes.key? "LocalVariableTypeTable"
172
+ #
173
+ # if codes.attributes.key? "LocalVariableTable"
174
+ # codes.attributes["LocalVariableTable"].local_variable_table.each {|l|
175
+ # type = local_types.find_by_index(l.index) if local_types != nil
176
+ # str << " // signature " << type.signature << "\n" if type != nil
177
+ # str << " " << convert_field_descriptor(l.descriptor)
178
178
  # str << " " << l.name << ";\n"
179
- # }
180
- # end
181
- # str << "\n"
182
- # lines = codes.attributes["LineNumberTable"] if codes.attributes.key? "LineNumberTable"
183
- # codes.codes.each_index {|i|
184
- # str << " " << convert_code(codes.codes[i])
185
- # str << " // line : #{lines.line_number(i)}" if lines != nil && lines.line_number(i) != nil
186
- # str << "\n";
187
- # }
188
- str << "}"
189
- end
190
- str << " #{attributes['AnnotationDefault'].to_s}" if attributes.key? 'AnnotationDefault'
191
- return str
192
- end
193
- end
179
+ # }
180
+ # end
181
+ # str << "\n"
182
+ # lines = codes.attributes["LineNumberTable"] if codes.attributes.key? "LineNumberTable"
183
+ # codes.codes.each_index {|i|
184
+ # str << " " << convert_code(codes.codes[i])
185
+ # str << " // line : #{lines.line_number(i)}" if lines != nil && lines.line_number(i) != nil
186
+ # str << "\n";
187
+ # }
188
+ str << "}"
189
+ end
190
+ str << " #{attributes['AnnotationDefault'].to_s}" if attributes.key? 'AnnotationDefault'
191
+ return str
192
+ end
193
+ end
194
194
  end