MCPcopy Create free account
hub / github.com/golang/mobile / genObjectMethods

Method genObjectMethods

bind/genjava.go:479–543  ·  view source on GitHub ↗
(n string, fields []*types.Var, isStringer bool)

Source from the content-addressed store, hash-verified

477}
478
479func (g *JavaGen) genObjectMethods(n string, fields []*types.Var, isStringer bool) {
480 g.Printf("@Override public boolean equals(Object o) {\n")
481 g.Indent()
482 g.Printf("if (o == null || !(o instanceof %s)) {\n return false;\n}\n", n)
483 g.Printf("%s that = (%s)o;\n", n, n)
484 for _, f := range fields {
485 if t := f.Type(); !g.isSupported(t) {
486 g.Printf("// skipped field %s.%s with unsupported type: %s\n\n", n, f.Name(), t)
487 continue
488 }
489 nf := f.Name()
490 g.Printf("%s this%s = get%s();\n", g.javaType(f.Type()), nf, nf)
491 g.Printf("%s that%s = that.get%s();\n", g.javaType(f.Type()), nf, nf)
492 if isJavaPrimitive(f.Type()) {
493 g.Printf("if (this%s != that%s) {\n return false;\n}\n", nf, nf)
494 } else {
495 g.Printf("if (this%s == null) {\n", nf)
496 g.Indent()
497 g.Printf("if (that%s != null) {\n return false;\n}\n", nf)
498 g.Outdent()
499 g.Printf("} else if (!this%s.equals(that%s)) {\n return false;\n}\n", nf, nf)
500 }
501 }
502 g.Printf("return true;\n")
503 g.Outdent()
504 g.Printf("}\n\n")
505
506 g.Printf("@Override public int hashCode() {\n")
507 g.Printf(" return java.util.Arrays.hashCode(new Object[] {")
508 idx := 0
509 for _, f := range fields {
510 if t := f.Type(); !g.isSupported(t) {
511 continue
512 }
513 if idx > 0 {
514 g.Printf(", ")
515 }
516 idx++
517 g.Printf("get%s()", f.Name())
518 }
519 g.Printf("});\n")
520 g.Printf("}\n\n")
521
522 g.Printf("@Override public String toString() {\n")
523 g.Indent()
524 if isStringer {
525 g.Printf("return string();\n")
526 } else {
527 g.Printf("StringBuilder b = new StringBuilder();\n")
528 g.Printf(`b.append("%s").append("{");`, n)
529 g.Printf("\n")
530 for _, f := range fields {
531 if t := f.Type(); !g.isSupported(t) {
532 continue
533 }
534 n := f.Name()
535 g.Printf(`b.append("%s:").append(get%s()).append(",");`, n, n)
536 g.Printf("\n")

Callers 1

genStructMethod · 0.95

Calls 7

javaTypeMethod · 0.95
isJavaPrimitiveFunction · 0.85
PrintfMethod · 0.80
IndentMethod · 0.80
TypeMethod · 0.80
OutdentMethod · 0.80
isSupportedMethod · 0.45

Tested by

no test coverage detected