/** * Return the protocol for a Java interface. * <p>The correct name of the method parameters needs the <code>-parameters</code> * java compiler argument. More info at https://openjdk.java.net/jeps/118 */ @Override public Protocol getProtocol(Class iface) { Protocol protocol = new Protocol(iface.getSimpleName(), iface.getPackage()==null?"":iface.getPackage().getName()); Map<String,Schema> names = new LinkedHashMap<>(); Map<String,Message> messages = protocol.getMessages(); for (Method method : iface.getMethods()) if ((method.getModifiers() & Modifier.STATIC) == 0) { String name = method.getName(); if (messages.containsKey(name)) throw new AvroTypeException("Two methods with same name: "+name); messages.put(name, getMessage(method, protocol, names)); } // reverse types, since they were defined in reference order List<Schema> types = new ArrayList<>(); types.addAll(names.values()); Collections.reverse(types); protocol.setTypes(types); return protocol; }
/** Return the protocol for a Java interface. * <p>Note that this requires that <a * href="http://paranamer.codehaus.org/">Paranamer</a> is run over compiled * interface declarations, since Java 6 reflection does not provide access to * method parameter names. See Avro's build.xml for an example. */ @Override public Protocol getProtocol(Class iface) { Protocol protocol = new Protocol(iface.getSimpleName(), iface.getPackage()==null?"":iface.getPackage().getName()); Map<String,Schema> names = new LinkedHashMap<String,Schema>(); Map<String,Message> messages = protocol.getMessages(); for (Method method : iface.getMethods()) if ((method.getModifiers() & Modifier.STATIC) == 0) { String name = method.getName(); if (messages.containsKey(name)) throw new AvroTypeException("Two methods with same name: "+name); messages.put(name, getMessage(method, protocol, names)); } // reverse types, since they were defined in reference order List<Schema> types = new ArrayList<Schema>(); types.addAll(names.values()); Collections.reverse(types); protocol.setTypes(types); return protocol; }
private Protocol addStringType(Protocol p) { if (stringType != StringType.String) return p; Protocol newP = new Protocol(p.getName(), p.getDoc(), p.getNamespace()); Map<Schema,Schema> types = new LinkedHashMap<>(); for (Map.Entry<String, Object> a : p.getObjectProps().entrySet()) { newP.addProp(a.getKey(), a.getValue()); } // annotate types Collection<Schema> namedTypes = new LinkedHashSet<>(); for (Schema s : p.getTypes()) namedTypes.add(addStringType(s, types)); newP.setTypes(namedTypes); // annotate messages Map<String,Message> newM = newP.getMessages(); for (Message m : p.getMessages().values()) newM.put(m.getName(), m.isOneWay() ? newP.createMessage(m, addStringType(m.getRequest(), types)) : newP.createMessage(m, addStringType(m.getRequest(), types), addStringType(m.getResponse(), types), addStringType(m.getErrors(), types))); return newP; }
newSchemas.add(Schemas.visit(schema, new ResolvingVisitor(schema, replacements, new SymbolTable(protocol)))); result.setTypes(newSchemas); // replace types with resolved ones
p.setTypes(names.values());
/** Return the protocol for a Java interface. * <p>Note that this requires that <a * href="http://paranamer.codehaus.org/">Paranamer</a> is run over compiled * interface declarations, since Java 6 reflection does not provide access to * method parameter names. See Avro's build.xml for an example. */ @Override public Protocol getProtocol(Class iface) { Protocol protocol = new Protocol(iface.getSimpleName(), iface.getPackage().getName()); Map<String,Schema> names = new LinkedHashMap<String,Schema>(); Map<String,Message> messages = protocol.getMessages(); for (Method method : iface.getMethods()) if ((method.getModifiers() & Modifier.STATIC) == 0) { String name = method.getName(); if (messages.containsKey(name)) throw new AvroTypeException("Two methods with same name: "+name); messages.put(name, getMessage(method, protocol, names)); } // reverse types, since they were defined in reference order List<Schema> types = new ArrayList<Schema>(); types.addAll(names.values()); Collections.reverse(types); protocol.setTypes(types); return protocol; }
/** Return the protocol for a Java interface. * <p>Note that this requires that <a * href="http://paranamer.codehaus.org/">Paranamer</a> is run over compiled * interface declarations, since Java 6 reflection does not provide access to * method parameter names. See Avro's build.xml for an example. */ @Override public Protocol getProtocol(Class iface) { Protocol protocol = new Protocol(iface.getSimpleName(), iface.getPackage()==null?"":iface.getPackage().getName()); Map<String,Schema> names = new LinkedHashMap<String,Schema>(); Map<String,Message> messages = protocol.getMessages(); for (Method method : iface.getMethods()) if ((method.getModifiers() & Modifier.STATIC) == 0) { String name = method.getName(); if (messages.containsKey(name)) throw new AvroTypeException("Two methods with same name: "+name); messages.put(name, getMessage(method, protocol, names)); } // reverse types, since they were defined in reference order List<Schema> types = new ArrayList<Schema>(); types.addAll(names.values()); Collections.reverse(types); protocol.setTypes(types); return protocol; }
/** Return the protocol for a Java interface. * <p>Note that this requires that <a * href="http://paranamer.codehaus.org/">Paranamer</a> is run over compiled * interface declarations, since Java 6 reflection does not provide access to * method parameter names. See Avro's build.xml for an example. */ @Override public Protocol getProtocol(Class iface) { Protocol protocol = new Protocol(iface.getSimpleName(), iface.getPackage()==null?"":iface.getPackage().getName()); Map<String,Schema> names = new LinkedHashMap<String,Schema>(); Map<String,Message> messages = protocol.getMessages(); for (Method method : iface.getMethods()) if ((method.getModifiers() & Modifier.STATIC) == 0) { String name = method.getName(); if (messages.containsKey(name)) throw new AvroTypeException("Two methods with same name: "+name); messages.put(name, getMessage(method, protocol, names)); } // reverse types, since they were defined in reference order List<Schema> types = new ArrayList<Schema>(); types.addAll(names.values()); Collections.reverse(types); protocol.setTypes(types); return protocol; }
/** Return the protocol for a Java interface. * <p>Note that this requires that <a * href="http://paranamer.codehaus.org/">Paranamer</a> is run over compiled * interface declarations, since Java 6 reflection does not provide access to * method parameter names. See Avro's build.xml for an example. */ @Override public Protocol getProtocol(Class iface) { Protocol protocol = new Protocol(iface.getSimpleName(), iface.getPackage()==null?"":iface.getPackage().getName()); Map<String,Schema> names = new LinkedHashMap<String,Schema>(); Map<String,Message> messages = protocol.getMessages(); for (Method method : iface.getMethods()) if ((method.getModifiers() & Modifier.STATIC) == 0) { String name = method.getName(); if (messages.containsKey(name)) throw new AvroTypeException("Two methods with same name: "+name); messages.put(name, getMessage(method, protocol, names)); } // reverse types, since they were defined in reference order List<Schema> types = new ArrayList<Schema>(); types.addAll(names.values()); Collections.reverse(types); protocol.setTypes(types); return protocol; }
private void generateClasses(final GenericData.StringType st, final Schema... schemas) { String[] namespaces = new String[schemas.length]; for (int i = 0; i < schemas.length; i++) { String namespace = schemas[i].getNamespace(); if (namespace == null) { namespace = ""; } namespaces[i] = namespace; } String commonPrefix = org.spf4j.base.Strings.commonPrefix(namespaces); if (commonPrefix.endsWith(".")) { commonPrefix = commonPrefix.substring(0, commonPrefix.length() - 1); } Protocol proto = new Protocol("generated", commonPrefix); proto.setTypes(Arrays.asList(schemas)); SpecificCompiler sc = new SpecificCompiler(proto); sc.setStringType(st); // use a custom template that does not contain the builder (janino can't compile builder). sc.setTemplateDir("org/spf4j/avro/"); try { sc.compileToDestination(null, tmpDir); } catch (IOException ex) { throw new UncheckedIOException(ex); } try { Files.walkFileTree(tmpDir.toPath(), new SetFilesReadOnlyVisitor()); } catch (IOException ex) { throw new UncheckedIOException(ex); } }