blob: c624e5c6acdffdc0ed93c159cc3ec1a95b971e75 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Todd Wang232d6492015-02-25 18:04:54 -08005package java
6
7import (
8 "bytes"
9 "log"
10
11 "v.io/v23/vdl"
Jiri Simsaffceefa2015-02-28 11:03:34 -080012 "v.io/x/ref/lib/vdl/compile"
Todd Wang232d6492015-02-25 18:04:54 -080013)
14
Jiri Simsa67b8a262015-03-24 21:14:07 -070015const primitiveTmpl = header + `
Srdjan Petroviceee415c2015-05-18 15:50:04 -070016// Source: {{ .Source }}
17package {{ .PackagePath }};
Todd Wang232d6492015-02-25 18:04:54 -080018
Srdjan Petroviceee415c2015-05-18 15:50:04 -070019{{ .Doc }}
Todd Wang232d6492015-02-25 18:04:54 -080020@io.v.v23.vdl.GeneratedFromVdl(name = "{{.VdlTypeName}}")
Srdjan Petrovic34b8a522015-04-30 14:02:34 -070021{{ .AccessModifier }} class {{.Name}} extends {{.VdlType}} {
Srdjan Petroviceee415c2015-05-18 15:50:04 -070022 private static final long serialVersionUID = 1L;
Jiri Simsa87cea302015-02-26 10:39:41 -080023
Srdjan Petroviceee415c2015-05-18 15:50:04 -070024 /**
25 * Vdl type for {@link {{.Name}}}.
26 */
Todd Wang232d6492015-02-25 18:04:54 -080027 public static final io.v.v23.vdl.VdlType VDL_TYPE =
28 io.v.v23.vdl.Types.getVdlTypeFromReflect({{.Name}}.class);
29
Srdjan Petroviceee415c2015-05-18 15:50:04 -070030 /**
31 * Creates a new instance of {@link {{.Name}}} with the given value.
32 *
33 * @param value value
34 */
Todd Wang232d6492015-02-25 18:04:54 -080035 public {{.Name}}({{.ConstructorType}} value) {
36 super(VDL_TYPE, value);
37 }
38
Srdjan Petroviceee415c2015-05-18 15:50:04 -070039 /**
40 * Creates a new zero-value instance of {@link {{.Name}}}.
41 */
Todd Wang232d6492015-02-25 18:04:54 -080042 public {{.Name}}() {
43 super(VDL_TYPE);
44 }
45}
46`
47
48// javaConstructorType returns java type that is used as a constructor argument
49// type for a VDL primitive.
50func javaConstructorType(t *vdl.Type) string {
51 switch t.Kind() {
52 case vdl.Uint16:
53 return "short"
54 case vdl.Uint32:
55 return "int"
56 case vdl.Uint64:
57 return "long"
58 default:
59 constructorType, _ := javaBuiltInType(t, false)
60 return constructorType
61 }
62}
63
64// javaConstructorType returns java class that is used as a type adapter delegate
65// argument for a VDL primitive.
66func javaTypeAdapterDelegateClass(t *vdl.Type) string {
67 switch t.Kind() {
68 case vdl.Uint16:
69 return "java.lang.Short"
70 case vdl.Uint32:
71 return "java.lang.Integer"
72 case vdl.Uint64:
73 return "java.lang.Long"
74 default:
75 typeAdapterDelegateClass, _ := javaBuiltInType(t, true)
76 return typeAdapterDelegateClass
77 }
78}
79
80// genJavaPrimitiveFile generates the Java class file for the provided user-defined type.
81func genJavaPrimitiveFile(tdef *compile.TypeDef, env *compile.Env) JavaFileInfo {
Srdjan Petrovic7c1e50f2015-05-06 15:02:42 -070082 name, access := javaTypeName(tdef, env)
Todd Wang232d6492015-02-25 18:04:54 -080083 data := struct {
84 AccessModifier string
Srdjan Petroviceee415c2015-05-18 15:50:04 -070085 ConstructorType string
Todd Wang232d6492015-02-25 18:04:54 -080086 Doc string
Srdjan Petroviceee415c2015-05-18 15:50:04 -070087 FileDoc string
Todd Wang232d6492015-02-25 18:04:54 -080088 Name string
89 PackagePath string
90 Source string
Todd Wang232d6492015-02-25 18:04:54 -080091 TypeAdapterDelegateClass string
92 VdlType string
93 VdlTypeName string
94 VdlTypeString string
95 }{
Srdjan Petroviceee415c2015-05-18 15:50:04 -070096 AccessModifier: access,
97 Doc: javaDoc(tdef.Doc, tdef.DocSuffix),
98 ConstructorType: javaConstructorType(tdef.Type),
99 FileDoc: tdef.File.Package.FileDoc,
100 Name: name,
101 PackagePath: javaPath(javaGenPkgPath(tdef.File.Package.GenPath)),
102 Source: tdef.File.BaseName,
Todd Wang232d6492015-02-25 18:04:54 -0800103 TypeAdapterDelegateClass: javaTypeAdapterDelegateClass(tdef.Type),
104 VdlType: javaVdlPrimitiveType(tdef.Type.Kind()),
105 VdlTypeName: tdef.Type.Name(),
106 VdlTypeString: tdef.Type.String(),
107 }
108 var buf bytes.Buffer
109 err := parseTmpl("primitive", primitiveTmpl).Execute(&buf, data)
110 if err != nil {
111 log.Fatalf("vdl: couldn't execute primitive template: %v", err)
112 }
113 return JavaFileInfo{
Srdjan Petrovic7c1e50f2015-05-06 15:02:42 -0700114 Name: name + ".java",
Todd Wang232d6492015-02-25 18:04:54 -0800115 Data: buf.Bytes(),
116 }
117}