private Pair<String, String> packageClassName(FileOptions options) { String packageName = null; String className = null; for (Map.Entry<FieldDescriptor, Object> entry : options.getAllFields().entrySet()) { if (entry.getKey().getName().equals("java_package")) { packageName = entry.getValue().toString(); } if (entry.getKey().getName().equals("java_outer_classname")) { className = entry.getValue().toString(); } } if (packageName != null && className != null) { return new ImmutablePair<String, String>(packageName, className); } return null; }
public static TypeMap of(FileDescriptorProto protoFile) { ImmutableMap.Builder<String, JavaType> types = ImmutableMap.builder(); FileOptions options = protoFile.getOptions(); String protoPackage = "." + (protoFile.hasPackage() ? protoFile.getPackage() : ""); String javaPackage = options.hasJavaPackage() ? options.getJavaPackage() : protoFile.hasPackage() ? protoFile.getPackage() : null; String enclosingClass = options.getJavaMultipleFiles() ? null : options.hasJavaOuterClassname() ? options.getJavaOuterClassname() : createOuterJavaClassname(protoFile.getName()); for (DescriptorProto message : protoFile.getMessageTypeList()) { types.put(protoPackage + "." + message.getName(), new JavaType(javaPackage, enclosingClass, message.getName())); } return new TypeMap(types.build()); }
private String extractPackageName(FileDescriptorProto proto) { FileOptions options = proto.getOptions(); if (options != null) { String javaPackage = options.getJavaPackage(); if (!Strings.isNullOrEmpty(javaPackage)) { return javaPackage; } } return Strings.nullToEmpty(proto.getPackage()); }
static String getPackage(FileDescriptorProto file, ProtoFlavor flavor) { FileOptions fileOptions = file.getOptions(); StringBuilder sb = new StringBuilder(); if (fileOptions.hasJavaPackage()) { sb.append(fileOptions.getJavaPackage()); } else { if (!file.getPackage().isEmpty()) { sb.append(file.getPackage()); } } return sb.toString(); }
private static boolean multipleJavaFiles(FileDescriptorProto fd, ProtoFlavor flavor) { FileOptions options = fd.getOptions(); switch (flavor) { case PROTO2: return options.getJavaMultipleFiles(); default: throw new AssertionError(); } }
public void generateProtoFromDescriptor(FileDescriptor descriptor, Appendable out, Descriptor wrapperMessage) throws IOException { String package1 = descriptor.getPackage(); if (package1 != null) { out.append("package " + package1 + ";\n"); } FileOptions options = descriptor.getOptions(); String javaPackage = options.getJavaPackage(); if (javaPackage != null) { out.append("option java_package = \"" + javaPackage + "\";\n"); } String javaOuterClassname = options.getJavaOuterClassname(); if (javaOuterClassname != null) { out.append("option java_outer_classname = \"" + javaOuterClassname + "\";\n"); } for (ServiceDescriptor serviceDescriptor : descriptor.getServices()) { generateProtoFromDescriptor(serviceDescriptor, out); } for (Descriptor messageDescriptor : descriptor.getMessageTypes()) { if (wrapperMessage != null && messageDescriptor.equals(wrapperMessage)) { out.append("// This is the message you can send to this service (wrapper message):\n"); } generateProtoFromDescriptor(messageDescriptor, out, "", new LinkedHashMap<Descriptor, Boolean>()); } for (EnumDescriptor enumDescriptor : descriptor.getEnumTypes()) { generateProtoFromDescriptor(enumDescriptor, out, ""); } }
/** Get the {@code FileOptions}, defined in {@code descriptor.proto}. */ public FileOptions getOptions() { return proto.getOptions(); }
/** * Get the {@code FileOptions}, defined in {@code descriptor.proto}. */ public FileOptions getOptions () { return proto.getOptions (); }
@Override public void exitFileOption(final FileOptionContext ctx) { super.exitFileOption(ctx); if (ctx.customFileOption() != null) { final CustomOptionContext customOptionCtx = ctx.customFileOption().customOption(); locationBuilder.addLocation().setAllSpan(ctx) .addPath(FileDescriptorProto.OPTIONS_FIELD_NUMBER); final int optionIndex = (treatStandardOptionAsUninterpreted ? getTotalFieldCount(fileOptionsBuilder) : fileOptionsBuilder.getUninterpretedOptionCount()) - 1; locationBuilder.addLocationClone().addPath(FileOptions.UNINTERPRETED_OPTION_FIELD_NUMBER) .addPath(optionIndex); locationBuilder.addLocationClone().clearComments() .addPath(UninterpretedOption.NAME_FIELD_NUMBER) .setAllSpan(customOptionCtx.customOptionName()); int i = -1; for (final CustomOptionNamePartContext namePart : customOptionCtx.customOptionName() .customOptionNamePart()) { locationBuilder.addLocation().addPath(FileDescriptorProto.OPTIONS_FIELD_NUMBER) .addPath(FileOptions.UNINTERPRETED_OPTION_FIELD_NUMBER).addPath(optionIndex) .addPath(UninterpretedOption.NAME_FIELD_NUMBER).addPath(++i).setAllSpan(namePart); locationBuilder .addLocationClone() .addPath(UninterpretedOption.NamePart.NAME_PART_FIELD_NUMBER) .setAllSpan( namePart.customOptionNamePartId() == null ? namePart.identifier() : namePart .customOptionNamePartId()); } // customOption value locations: can be scalar or aggregate! final CustomOptionValueContext customOptionValue = customOptionCtx.customOptionValue(); int valuePath; if (customOptionValue.optionAggregateValue() != null) { valuePath = UninterpretedOption.AGGREGATE_VALUE_FIELD_NUMBER; } else { final OptionScalarValueContext optionScalarValue = customOptionValue.optionScalarValue(); if (optionScalarValue.doubleValue() != null) { valuePath = UninterpretedOption.DOUBLE_VALUE_FIELD_NUMBER; } else if (optionScalarValue.identifier() != null) { valuePath = UninterpretedOption.IDENTIFIER_VALUE_FIELD_NUMBER; } else if (optionScalarValue.StringLiteral() != null || optionScalarValue.BooleanLiteral() != null) { valuePath = UninterpretedOption.STRING_VALUE_FIELD_NUMBER; } else if (optionScalarValue.NegativeIntegerLiteral() != null) { valuePath = UninterpretedOption.NEGATIVE_INT_VALUE_FIELD_NUMBER; } else if (optionScalarValue.IntegerLiteral() != null) { valuePath = UninterpretedOption.POSITIVE_INT_VALUE_FIELD_NUMBER; } else { // we shouldn't arrive here! throw new RuntimeException("custom option value has unidentified type!"); } } locationBuilder.addLocation().addPath(FileDescriptorProto.OPTIONS_FIELD_NUMBER) .addPath(FileOptions.UNINTERPRETED_OPTION_FIELD_NUMBER).addPath(optionIndex) .addPath(valuePath).setAllSpan(customOptionValue); // should the aggregate locations be added here? BTW, protoc fails on aggregates! } }
private void doStandardOptionSource(final StandardFileOptionContext ctx, final int customOptionValueType) { final FileOptionContext parentCtx = (FileOptionContext) ctx.getParent(); locationBuilder.addLocation().setAllSpan(parentCtx) .addPath(FileDescriptorProto.OPTIONS_FIELD_NUMBER); if (treatStandardOptionAsUninterpreted) { final int optionIndex = getTotalFieldCount(fileOptionsBuilder) - 1; locationBuilder.addLocation().comments(parentCtx) .addPath(FileDescriptorProto.OPTIONS_FIELD_NUMBER) .addPath(FileOptions.UNINTERPRETED_OPTION_FIELD_NUMBER).addPath(optionIndex) .setAllSpan(parentCtx); locationBuilder.addLocation().addPath(FileDescriptorProto.OPTIONS_FIELD_NUMBER) .addPath(FileOptions.UNINTERPRETED_OPTION_FIELD_NUMBER).addPath(optionIndex) .addPath(UninterpretedOption.NAME_FIELD_NUMBER) .setAllSpan((TerminalNode) ctx.getChild(0)); locationBuilder.addLocation().addPath(FileDescriptorProto.OPTIONS_FIELD_NUMBER) .addPath(FileOptions.UNINTERPRETED_OPTION_FIELD_NUMBER).addPath(optionIndex) .addPath(UninterpretedOption.NAME_FIELD_NUMBER).addPath(0) .setAllSpan((TerminalNode) ctx.getChild(0)); locationBuilder.addLocation().addPath(FileDescriptorProto.OPTIONS_FIELD_NUMBER) .addPath(FileOptions.UNINTERPRETED_OPTION_FIELD_NUMBER).addPath(optionIndex) .addPath(UninterpretedOption.NAME_FIELD_NUMBER).addPath(0) .addPath(UninterpretedOption.NamePart.NAME_PART_FIELD_NUMBER) .setAllSpan((TerminalNode) ctx.getChild(0)); locationBuilder.addLocation().addPath(FileDescriptorProto.OPTIONS_FIELD_NUMBER) .addPath(FileOptions.UNINTERPRETED_OPTION_FIELD_NUMBER).addPath(optionIndex) .addPath(customOptionValueType).setAllSpan(ctx.getChild(2)); } else { locationBuilder .addLocation() .comments(parentCtx) .addPath(FileDescriptorProto.OPTIONS_FIELD_NUMBER) .addPath( FileOptions.getDescriptor().findFieldByName(ctx.getChild(0).getText()).getNumber()) .setAllSpan(ctx.getChild(2)); } }
public FileOptions.Builder getFileOptions() { return currentScope.getFileOptions(); }
protected FileOptions.Builder getFileOptions() { throw new RuntimeException(NOT_APPLICABLE_IN_CURRENT_SCOPE); }
@Override protected FileOptions.Builder getFileOptions() { return protoBuilder.getOptionsBuilder(); }
@Override protected FileOptions.Builder getFileOptions() { return getParent().getFileOptions(); }
public FileOptions getOptions() { return delegate.getOptions(); }
@VisibleForTesting static String inferJavaPackage(FileDescriptorProto file) { FileOptions options = file.getOptions(); return options.hasJavaPackage() ? options.getJavaPackage() : file.hasPackage() ? file.getPackage() : null; }