classes.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright 2016 The Go 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. package javapkg
  5. import (
  6. "Java/java/beans"
  7. "Java/java/io"
  8. "Java/java/io/IOException"
  9. "Java/java/lang"
  10. "Java/java/lang/Character"
  11. "Java/java/lang/Integer"
  12. "Java/java/lang/Object"
  13. "Java/java/lang/Runnable"
  14. "Java/java/net"
  15. "Java/java/nio"
  16. "Java/java/util"
  17. "Java/java/util/concurrent"
  18. gopkg "Java/javapkg"
  19. xnet "Java/javax/net"
  20. )
  21. const (
  22. ToStringPrefix = "Go toString: "
  23. IOExceptionMessage = "GoInputStream IOException"
  24. )
  25. type GoRunnable struct {
  26. lang.Object
  27. lang.Runnable
  28. this lang.Runnable
  29. Field string
  30. }
  31. func (r *GoRunnable) ToString(this gopkg.GoRunnable) string {
  32. return ToStringPrefix
  33. }
  34. func (r *GoRunnable) Run(this gopkg.GoRunnable) {
  35. }
  36. func (r *GoRunnable) GetThis(this gopkg.GoRunnable) lang.Runnable {
  37. return this
  38. }
  39. type GoInputStream struct {
  40. io.InputStream
  41. }
  42. func (_ *GoInputStream) Read() (int32, error) {
  43. return 0, IOException.New(IOExceptionMessage)
  44. }
  45. func NewGoInputStream() *GoInputStream {
  46. return new(GoInputStream)
  47. }
  48. type GoFuture struct {
  49. concurrent.Future
  50. }
  51. func (_ *GoFuture) Cancel(_ bool) bool {
  52. return false
  53. }
  54. func (_ *GoFuture) Get() (lang.Object, error) {
  55. return nil, nil
  56. }
  57. // Use a trailing underscore to override multiple overloaded methods.
  58. func (_ *GoFuture) Get_(_ int64, _ concurrent.TimeUnit) (lang.Object, error) {
  59. return nil, nil
  60. }
  61. func (_ *GoFuture) IsCancelled() bool {
  62. return false
  63. }
  64. func (_ *GoFuture) IsDone() bool {
  65. return false
  66. }
  67. type GoObject struct {
  68. lang.Object
  69. this lang.Object
  70. }
  71. func (o *GoObject) ToString(this gopkg.GoObject) string {
  72. o.this = this
  73. return ToStringPrefix + this.Super().ToString()
  74. }
  75. func (_ *GoObject) HashCode() int32 {
  76. return 42
  77. }
  78. func RunRunnable(r lang.Runnable) {
  79. r.Run()
  80. }
  81. func RunnableRoundtrip(r lang.Runnable) lang.Runnable {
  82. return r
  83. }
  84. // Test constructing and returning Go instances of GoObject and GoRunnable
  85. // outside a constructor
  86. func ConstructGoRunnable() *GoRunnable {
  87. return new(GoRunnable)
  88. }
  89. func ConstructGoObject() *GoObject {
  90. return new(GoObject)
  91. }
  92. // java.beans.PropertyChangeEvent is a class a with no default constructors.
  93. type GoPCE struct {
  94. beans.PropertyChangeEvent
  95. }
  96. func NewGoPCE(_ lang.Object, _ string, _ lang.Object, _ lang.Object) *GoPCE {
  97. return new(GoPCE)
  98. }
  99. // java.util.ArrayList is a class with multiple constructors
  100. type GoArrayList struct {
  101. util.ArrayList
  102. }
  103. func NewGoArrayList() *GoArrayList {
  104. return new(GoArrayList)
  105. }
  106. func NewGoArrayListWithCap(_ int32) *GoArrayList {
  107. return new(GoArrayList)
  108. }
  109. func UnwrapGoArrayList(l gopkg.GoArrayList) {
  110. _ = l.Unwrap().(*GoArrayList)
  111. }
  112. func CallSubset(s Character.Subset) {
  113. s.ToString()
  114. }
  115. type GoSubset struct {
  116. Character.Subset
  117. }
  118. func NewGoSubset(_ string) *GoSubset {
  119. return new(GoSubset)
  120. }
  121. func NewJavaObject() lang.Object {
  122. return Object.New()
  123. }
  124. func NewJavaInteger() lang.Integer {
  125. i, _ := Integer.New(int32(42))
  126. return i
  127. }
  128. type NoargConstructor struct {
  129. util.BitSet // An otherwise unused class with a no-arg constructor
  130. }
  131. type GoRand struct {
  132. util.Random
  133. }
  134. func (_ *GoRand) Next(this gopkg.GoRand, i int32) int32 {
  135. return this.Super().Next(i)
  136. }
  137. type I interface{}
  138. func CastInterface(intf I) lang.Runnable {
  139. var r lang.Runnable = Runnable.Cast(intf)
  140. r.Run()
  141. return r
  142. }
  143. func CastRunnable(o lang.Object) lang.Runnable {
  144. defer func() {
  145. recover() // swallow the panic
  146. }()
  147. var r lang.Runnable = Runnable.Cast(o)
  148. r.Run()
  149. return r
  150. }
  151. // Test that extending classes from Java packages
  152. // with the same last component (in this case "net")
  153. // works.
  154. func NameClashingPackages(_ net.Socket, _ xnet.SocketFactory) {
  155. }
  156. func testReferenceToUnsupportedParameters() {
  157. var ib nio.IntBuffer
  158. ib.Put(nil)
  159. }