Class: MessagePack::IDL::Lang::JavaGenerator::Context

Inherits:
Object
  • Object
show all
Includes:
Tenjin::ContextHelper
Defined in:
lib/msgpack/idl/lang/java.rb

Constant Summary collapse

PRIMITIVE_TYPEMAP =
{
	'void'   => 'void',
	'byte'   => 'byte',
	'short'  => 'short',
	'int'    => 'int',
	'long'   => 'long',
	'ubyte'  => 'short',
	'ushort' => 'int',
	'uint'   => 'long',
	'ulong'  => 'BigInteger',
	'float'  => 'float',
	'double' => 'double',
	'bool'   => 'boolean',
	'raw'    => 'byte[]',
	'string' => 'String',
	'list'   => 'List',
	'map'    => 'Map',
}
NULLABLE_REMAP =
{
	'byte'    => 'Byte',
	'short'   => 'Short',
	'int'     => 'Integer',
	'long'    => 'Long',
	'float'   => 'Float',
	'double'  => 'Double',
	'boolean' => 'Boolean',
}
TYPE_PARAMETER_REMAP =
NULLABLE_REMAP.merge({
	:void     => 'Void',
})
IFACE_CLASS_REMAP =
{
	'Map'     => 'HashMap',
	'List'    => 'ArrayList',
}
PRIMITIVE_DEFAULT =
{
	'byte'    => '0',
	'short'   => '0',
	'int'     => '0',
	'long'    => '0',
	'float'   => '0.0f',
	'double'  => '0.0',
	'boolean' => 'false',
	'byte[]'  => 'new byte[0]',
	'String'  => '""',
}
PRIMITIVE_UNPACK =
{
	'byte'   => 'unpackByte()',
	'short'  => 'unpackShort()',
	'int'    => 'unpackInt()',
	'long'   => 'unpackLong()',
	'ubyte'  => 'unpackShort()',
	'ushort' => 'unpackInt()',
	'uint'   => 'unpackLong()',
	'ulong'  => 'unpackBigInteger()',
	'float'  => 'unpackFloat()',
	'double' => 'unpackDouble()',
	'bool'   => 'unpackBoolean()',
	'raw'    => 'unpackByteArray()',
	'string' => 'unpackString()',
}
PRIMITIVE_CONVERT =
{
	'byte'   => 'asByte()',
	'short'  => 'asShort()',
	'int'    => 'asInt()',
	'long'   => 'asLong()',
	'ubyte'  => 'asShort()',
	'ushort' => 'asInt()',
	'uint'   => 'asLong()',
	'ulong'  => 'asBigInteger()',
	'float'  => 'asFloat()',
	'double' => 'asDouble()',
	'bool'   => 'asBoolean()',
	'raw'    => 'asByteArray()',
	'string' => 'asString()',
}

Instance Method Summary collapse

Constructor Details

#initialize(gen, *member) ⇒ Context

Returns a new instance of Context.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/msgpack/idl/lang/java.rb', line 189

def initialize(gen, *member)
	@gen = gen

	(class << self; self; end).module_eval {
		member.each {|m|
			define_method("#{m}") {
				instance_variable_get("@#{m}")
			}
			define_method("#{m}=") {|v|
				instance_variable_set("@#{m}", v)
			}
		}
	}
end

Instance Method Details

#format_convert(to, obj, t) ⇒ Object



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/msgpack/idl/lang/java.rb', line 430

def format_convert(to, obj, t)
	if t.parameterized_type?
		if t.list_type?
			e = t.type_params[0]
			anon_id = next_anon_id
			ve = "e#{anon_id}"
			vo = "o#{anon_id}"
			return %[{
				#{to} = new #{format_type_impl(t)}();
				#{format_type(e)} #{ve};
				for(MessagePackObject #{vo} : #{obj}.asArray()) {
					#{format_convert("#{ve}", "#{vo}", e)}
					#{to}.add(#{ve});
				}
			}]
		elsif t.map_type?
			k = t.type_params[0]
			v = t.type_params[1]
			anon_id = next_anon_id
			vk = "k#{anon_id}"
			vv = "v#{anon_id}"
			vkv = "kv#{anon_id}"
			return %[{
				#{to} = new #{format_type_impl(t)}();
				#{format_type(k)} #{vk};
				#{format_type(v)} #{vv};
				for(Map.Entry<MessagePackObject,MessagePackObject> #{vkv} : #{obj}.asMap().entrySet()) {
					#{format_convert("#{vk}", "#{vkv}.getKey()", k)}
					#{format_convert("#{vv}", "#{vkv}.getValue()", v)}
					#{to}.put(#{vk}, #{vv});
				}
			}]
		end

	elsif t.is_a?(IR::Message)
		return %[#{to} = new #{t.name}();
			#{to}.messageConvert(#{obj});]

	elsif t.is_a?(IR::Enum)
		return "#{to} = #{t.name}.enumOf(#{obj}.asInt());"
	end

	method = PRIMITIVE_CONVERT[t.name] || "convert(new #{t.name}())"
	"#{to} = #{obj}.#{method};"
end

#format_initial_value(to, f) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/msgpack/idl/lang/java.rb', line 313

def format_initial_value(to, f)
	v = f.value
	case v
	when IR::NilValue
		"#{to} = null;"

	when IR::IntValue
		tt, name = format_nullable_type(f.type)
		if name == "BigInteger"
			"#{to} = BigInteger.valueOf(#{v.int}L);"
		elsif name == "long"
			"#{to} = #{v.int}L;"
		else
			"#{to} = #{v.int};"
		end

	when IR::BoolValue
		if v.bool
			"#{to} = true;"
		else
			"#{to} = false;"
		end

	when IR::EnumValue
		"#{to} = #{v.enum.name}.#{v.field.name};"
	when IR::EmptyValue
		tt, name = format_nullable_type(f.type)
		v = PRIMITIVE_DEFAULT[name]
		v ||= "new #{format_type_impl(f.type)}()"
		"#{to} = #{v};"
	else
		raise SemanticsError, "unknown initial value type: #{v.class}"
	end
end

#format_message(t, name = t.name) ⇒ Object



285
286
287
# File 'lib/msgpack/idl/lang/java.rb', line 285

def format_message(t, name = t.name)
	@gen.format_message(t, name)
end

#format_message_class(s, version) ⇒ Object



481
482
483
# File 'lib/msgpack/idl/lang/java.rb', line 481

def format_message_class(s, version)
	(namespace + ["#{s.name}_#{version}"]).join('.')
end

#format_nullable_type(t) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/msgpack/idl/lang/java.rb', line 256

def format_nullable_type(t)
	if t.nullable_type?
		real_type = t.real_type
		name = PRIMITIVE_TYPEMAP[real_type.name] || real_type.name
		name = NULLABLE_REMAP[name] || name
		return real_type, name
	else
		name = PRIMITIVE_TYPEMAP[t.name] || t.name
		return t, name
	end
end

#format_package(*extra) ⇒ Object



204
205
206
207
208
209
210
211
# File 'lib/msgpack/idl/lang/java.rb', line 204

def format_package(*extra)
	name = format_package_name(*extra)
	if name.empty?
		""
	else
		"package #{name};"
	end
end

#format_package_name(*extra) ⇒ Object



213
214
215
216
# File 'lib/msgpack/idl/lang/java.rb', line 213

def format_package_name(*extra)
	package = namespace + extra
	package.join('.')
end

#format_parameterized_type(t, name) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/msgpack/idl/lang/java.rb', line 268

def format_parameterized_type(t, name)
	if t.parameterized_type?
		name + '<' +
			t.type_params.map {|tp|
				n = format_type(tp)
				TYPE_PARAMETER_REMAP[n] || n
			}.join(', ') + '>'
	else
		name
	end
end

#format_type(t) ⇒ Object



280
281
282
283
# File 'lib/msgpack/idl/lang/java.rb', line 280

def format_type(t)
	t, name = format_nullable_type(t)
	format_parameterized_type(t, name)
end

#format_type_impl(t) ⇒ Object



295
296
297
298
299
# File 'lib/msgpack/idl/lang/java.rb', line 295

def format_type_impl(t)
	t, name = format_nullable_type(t)
	name = IFACE_CLASS_REMAP[name] || name
	format_parameterized_type(t, name)
end

#format_type_parameter(t) ⇒ Object



289
290
291
292
293
# File 'lib/msgpack/idl/lang/java.rb', line 289

def format_type_parameter(t)
	t, name = format_nullable_type(t)
	name = TYPE_PARAMETER_REMAP[name] || name
	format_parameterized_type(t, name)
end

#format_unpack(to, pac, t) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/msgpack/idl/lang/java.rb', line 364

def format_unpack(to, pac, t)
	if t.parameterized_type?
		if t.list_type?
			e = t.type_params[0]
			anon_id = next_anon_id
			vn = "n#{anon_id}"
			ve = "e#{anon_id}"
			vi = "i#{anon_id}"
			return %[{
				#{to} = new #{format_type_impl(t)}();
				int #{vn} = #{pac}.unpackArray();
				#{format_type(e)} #{ve};
				for(int #{vi}=0; #{vi} < #{vn}; #{vi}++) {
					#{format_unpack("#{ve}", pac, e)}
					#{to}.add(#{ve});
				}
			}]
		elsif t.map_type?
			anon_id = next_anon_id
			k = t.type_params[0]
			v = t.type_params[1]
			vn = "n#{anon_id}"
			vk = "k#{anon_id}"
			vv = "v#{anon_id}"
			vi = "i#{anon_id}"
			return %[{
				#{to} = new #{format_type_impl(t)}();
				int #{vn} = #{pac}.unpackMap();
				#{format_type(k)} #{vk};
				#{format_type(v)} #{vv};
				for(int #{vi}=0; #{vi} < #{vn}; #{vi}++) {
					#{format_unpack("#{vk}", pac, k)}
					#{format_unpack("#{vv}", pac, v)}
					#{to}.put(#{vk}, #{vv});
				}
			}]
		end

	elsif t.is_a?(IR::Message)
		return %[#{to} = new #{t.name}();
			#{to}.messageUnpack(#{pac});]

	elsif t.is_a?(IR::Enum)
		return "#{to} = #{t.name}.enumOf(#{pac}.unpackInt());"
	end

	method = PRIMITIVE_UNPACK[t.name] || "unpack(#{t.name}.class)"
	"#{to} = #{pac}.#{method};"
end

#next_anon_idObject



476
477
478
479
# File 'lib/msgpack/idl/lang/java.rb', line 476

def next_anon_id
	@anon_id ||= -1
	@anon_id += 1
end