Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // 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 Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 5 | package java |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "log" |
| 10 | |
Jiri Simsa | ffceefa | 2015-02-28 11:03:34 -0800 | [diff] [blame] | 11 | "v.io/x/ref/lib/vdl/compile" |
Todd Wang | 53a4e2e | 2015-03-18 10:54:54 -0700 | [diff] [blame] | 12 | "v.io/x/ref/lib/vdl/vdlutil" |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 13 | ) |
| 14 | |
Jiri Simsa | 67b8a26 | 2015-03-24 21:14:07 -0700 | [diff] [blame] | 15 | const listTmpl = header + ` |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 16 | // Source: {{.SourceFile}} |
| 17 | package {{.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 Simsa | 87cea30 | 2015-02-26 10:39:41 -0800 | [diff] [blame] | 24 | private static final long serialVersionUID = 1L; |
| 25 | |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 26 | 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 Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 36 | } |
| 37 | ` |
| 38 | |
| 39 | // genJavaListFile generates the Java class file for the provided named list type. |
| 40 | func genJavaListFile(tdef *compile.TypeDef, env *compile.Env) JavaFileInfo { |
Todd Wang | 53a4e2e | 2015-03-18 10:54:54 -0700 | [diff] [blame] | 41 | javaTypeName := vdlutil.FirstRuneToUpper(tdef.Name) |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 42 | data := struct { |
Todd Wang | b90b8de | 2015-03-31 20:04:13 -0700 | [diff] [blame] | 43 | FileDoc string |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 44 | 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 Wang | b90b8de | 2015-03-31 20:04:13 -0700 | [diff] [blame] | 53 | FileDoc: tdef.File.Package.FileDoc, |
Todd Wang | 232d649 | 2015-02-25 18:04:54 -0800 | [diff] [blame] | 54 | 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 | } |