Import returns Java Class descriptors for a list of references. The javap command from the Java SDK is used to dump class information. Its output looks like this: Compiled from "System.java" public final class java.lang.System { public static final java.io.InputStream in; descriptor: Ljava/io
(refs *importers.References)
| 192 | // |
| 193 | // } |
| 194 | func (j *Importer) Import(refs *importers.References) ([]*Class, error) { |
| 195 | if j.clsMap == nil { |
| 196 | j.clsMap = make(map[string]*Class) |
| 197 | } |
| 198 | clsSet := make(map[string]struct{}) |
| 199 | var names []string |
| 200 | for _, ref := range refs.Refs { |
| 201 | // The reference could be to some/pkg.Class or some/pkg/Class.Identifier. Include both. |
| 202 | pkg := strings.Replace(ref.Pkg, "/", ".", -1) |
| 203 | for _, cls := range []string{pkg, pkg + "." + ref.Name} { |
| 204 | if _, exists := clsSet[cls]; !exists { |
| 205 | clsSet[cls] = struct{}{} |
| 206 | names = append(names, cls) |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | // Make sure toString() is included; it is called when wrapping Java exception types to Go |
| 211 | // errors. |
| 212 | refs.Names["ToString"] = struct{}{} |
| 213 | funcRefs := make(map[funcRef]struct{}) |
| 214 | for _, ref := range refs.Refs { |
| 215 | pkgName := strings.Replace(ref.Pkg, "/", ".", -1) |
| 216 | funcRefs[funcRef{pkgName, ref.Name}] = struct{}{} |
| 217 | } |
| 218 | classes, err := j.importClasses(names, true) |
| 219 | if err != nil { |
| 220 | return nil, err |
| 221 | } |
| 222 | j.filterReferences(classes, refs, funcRefs) |
| 223 | supers, err := j.importReferencedClasses(classes) |
| 224 | if err != nil { |
| 225 | return nil, err |
| 226 | } |
| 227 | j.filterReferences(supers, refs, funcRefs) |
| 228 | // Embedders refer to every exported Go struct that will have its class |
| 229 | // generated. Allow Go code to reverse bind to those classes by synthesizing |
| 230 | // their class descriptors. |
| 231 | for _, emb := range refs.Embedders { |
| 232 | n := emb.Pkg + "." + emb.Name |
| 233 | if j.JavaPkg != "" { |
| 234 | n = j.JavaPkg + "." + n |
| 235 | } |
| 236 | if _, exists := j.clsMap[n]; exists { |
| 237 | continue |
| 238 | } |
| 239 | clsSet[n] = struct{}{} |
| 240 | cls := &Class{ |
| 241 | Name: n, |
| 242 | FindName: n, |
| 243 | JNIName: JNIMangle(n), |
| 244 | PkgName: emb.Name, |
| 245 | HasNoArgCon: true, |
| 246 | } |
| 247 | for _, ref := range emb.Refs { |
| 248 | jpkg := strings.Replace(ref.Pkg, "/", ".", -1) |
| 249 | super := jpkg + "." + ref.Name |
| 250 | if _, exists := j.clsMap[super]; !exists { |
| 251 | return nil, fmt.Errorf("failed to find Java class %s, embedded by %s", super, n) |