lookupMethod searches the Java class descriptor for a method that matches the Go method.
(m *types.Func, hasThis bool)
| 144 | // lookupMethod searches the Java class descriptor for a method |
| 145 | // that matches the Go method. |
| 146 | func (j *javaClassInfo) lookupMethod(m *types.Func, hasThis bool) *java.Func { |
| 147 | jm := j.methods[m.Name()] |
| 148 | if jm == nil { |
| 149 | // If an exact match is not found, try the method with trailing underscores |
| 150 | // stripped. This way, name clashes can be avoided when overriding multiple |
| 151 | // overloaded methods from Go. |
| 152 | base := strings.TrimRight(m.Name(), "_") |
| 153 | jm = j.methods[base] |
| 154 | if jm == nil { |
| 155 | return nil |
| 156 | } |
| 157 | } |
| 158 | // A name match was found. Now use the parameter and return types to locate |
| 159 | // the correct variant. |
| 160 | sig := m.Type().(*types.Signature) |
| 161 | params := sig.Params() |
| 162 | // Convert Go parameter types to their Java counterparts, if possible. |
| 163 | var jparams []*java.Type |
| 164 | i := 0 |
| 165 | if hasThis { |
| 166 | i = 1 |
| 167 | } |
| 168 | for ; i < params.Len(); i++ { |
| 169 | jparams = append(jparams, j.toJavaType(params.At(i).Type())) |
| 170 | } |
| 171 | var ret *java.Type |
| 172 | var throws bool |
| 173 | if results := sig.Results(); results.Len() > 0 { |
| 174 | ret = j.toJavaType(results.At(0).Type()) |
| 175 | if results.Len() > 1 { |
| 176 | throws = isErrorType(results.At(1).Type()) |
| 177 | } |
| 178 | } |
| 179 | loop: |
| 180 | for _, f := range jm.Funcs { |
| 181 | if len(f.Params) != len(jparams) { |
| 182 | continue |
| 183 | } |
| 184 | if throws != (f.Throws != "") { |
| 185 | continue |
| 186 | } |
| 187 | if !reflect.DeepEqual(ret, f.Ret) { |
| 188 | continue |
| 189 | } |
| 190 | for i, p := range f.Params { |
| 191 | if !reflect.DeepEqual(p, jparams[i]) { |
| 192 | continue loop |
| 193 | } |
| 194 | } |
| 195 | return f |
| 196 | } |
| 197 | return nil |
| 198 | } |
| 199 | |
| 200 | // ClassNames returns the list of names of the generated Java classes and interfaces. |
| 201 | func (g *JavaGen) ClassNames() []string { |
no test coverage detected