| 1026 | } |
| 1027 | |
| 1028 | func (g *JavaGen) genConst(o *types.Const) { |
| 1029 | if _, ok := o.Type().(*types.Basic); !ok || !g.isSupported(o.Type()) { |
| 1030 | g.Printf("// skipped const %s with unsupported type: %s\n\n", o.Name(), o.Type()) |
| 1031 | return |
| 1032 | } |
| 1033 | // TODO(hyangah): should const names use upper cases + "_"? |
| 1034 | // TODO(hyangah): check invalid names. |
| 1035 | jType := g.javaType(o.Type()) |
| 1036 | val := o.Val().ExactString() |
| 1037 | switch b := o.Type().(*types.Basic); b.Kind() { |
| 1038 | case types.Int64, types.UntypedInt: |
| 1039 | i, exact := constant.Int64Val(o.Val()) |
| 1040 | if !exact { |
| 1041 | g.errorf("const value %s for %s cannot be represented as %s", val, o.Name(), jType) |
| 1042 | return |
| 1043 | } |
| 1044 | val = fmt.Sprintf("%dL", i) |
| 1045 | |
| 1046 | case types.Float32: |
| 1047 | f, _ := constant.Float32Val(o.Val()) |
| 1048 | val = fmt.Sprintf("%gf", f) |
| 1049 | |
| 1050 | case types.Float64, types.UntypedFloat: |
| 1051 | f, _ := constant.Float64Val(o.Val()) |
| 1052 | if math.IsInf(f, 0) || math.Abs(f) > math.MaxFloat64 { |
| 1053 | g.errorf("const value %s for %s cannot be represented as %s", val, o.Name(), jType) |
| 1054 | return |
| 1055 | } |
| 1056 | val = fmt.Sprintf("%g", f) |
| 1057 | } |
| 1058 | g.javadoc(g.docs[o.Name()].Doc()) |
| 1059 | g.Printf("public static final %s %s = %s;\n", g.javaType(o.Type()), o.Name(), val) |
| 1060 | } |
| 1061 | |
| 1062 | func (g *JavaGen) genJNIField(o *types.TypeName, f *types.Var) { |
| 1063 | if t := f.Type(); !g.isSupported(t) { |