blob: 06d4c5496b6b350639c9c657b8670209c773e345 [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
Jiri Simsaffceefa2015-02-28 11:03:34 -080011 "v.io/x/ref/lib/vdl/compile"
Todd Wang53a4e2e2015-03-18 10:54:54 -070012 "v.io/x/ref/lib/vdl/vdlutil"
Todd Wang232d6492015-02-25 18:04:54 -080013)
14
Jiri Simsa67b8a262015-03-24 21:14:07 -070015const listTmpl = header + `
Todd Wang232d6492015-02-25 18:04:54 -080016// Source: {{.SourceFile}}
17package {{.Package}};
18
19/**
20 * type {{.Name}} {{.VdlTypeString}} {{.Doc}}
21 **/
22@io.v.v23.vdl.GeneratedFromVdl(name = "{{.VdlTypeName}}")
23{{ .AccessModifier }} final class {{.Name}} extends io.v.v23.vdl.VdlList<{{.ElemType}}> {
Jiri Simsa87cea302015-02-26 10:39:41 -080024 private static final long serialVersionUID = 1L;
25
Todd Wang232d6492015-02-25 18:04:54 -080026 public static final io.v.v23.vdl.VdlType VDL_TYPE =
27 io.v.v23.vdl.Types.getVdlTypeFromReflect({{.Name}}.class);
28
29 public {{.Name}}(java.util.List<{{.ElemType}}> impl) {
30 super(VDL_TYPE, impl);
31 }
32
33 public {{.Name}}() {
34 this(new java.util.ArrayList<{{.ElemType}}>());
35 }
Todd Wang232d6492015-02-25 18:04:54 -080036}
37`
38
39// genJavaListFile generates the Java class file for the provided named list type.
40func genJavaListFile(tdef *compile.TypeDef, env *compile.Env) JavaFileInfo {
Todd Wang53a4e2e2015-03-18 10:54:54 -070041 javaTypeName := vdlutil.FirstRuneToUpper(tdef.Name)
Todd Wang232d6492015-02-25 18:04:54 -080042 data := struct {
Todd Wangb90b8de2015-03-31 20:04:13 -070043 FileDoc string
Todd Wang232d6492015-02-25 18:04:54 -080044 AccessModifier string
45 Doc string
46 ElemType string
47 Name string
48 Package string
49 SourceFile string
50 VdlTypeName string
51 VdlTypeString string
52 }{
Todd Wangb90b8de2015-03-31 20:04:13 -070053 FileDoc: tdef.File.Package.FileDoc,
Todd Wang232d6492015-02-25 18:04:54 -080054 AccessModifier: accessModifierForName(tdef.Name),
55 Doc: javaDocInComment(tdef.Doc),
56 ElemType: javaType(tdef.Type.Elem(), true, env),
57 Name: javaTypeName,
58 Package: javaPath(javaGenPkgPath(tdef.File.Package.GenPath)),
59 SourceFile: tdef.File.BaseName,
60 VdlTypeName: tdef.Type.Name(),
61 VdlTypeString: tdef.Type.String(),
62 }
63 var buf bytes.Buffer
64 err := parseTmpl("list", listTmpl).Execute(&buf, data)
65 if err != nil {
66 log.Fatalf("vdl: couldn't execute list template: %v", err)
67 }
68 return JavaFileInfo{
69 Name: javaTypeName + ".java",
70 Data: buf.Bytes(),
71 }
72}