Skip to content

Commit

Permalink
[jnigen] Generate enum values non-nullable (#1902)
Browse files Browse the repository at this point in the history
  • Loading branch information
HosseinYousefi authored Jan 16, 2025
1 parent 7295bae commit 817c76e
Show file tree
Hide file tree
Showing 15 changed files with 1,197 additions and 234 deletions.
1 change: 1 addition & 0 deletions pkgs/jnigen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Fixed a bug where Kotlin wildcards would crash the code generation.
- Support nullability annotations that are on Java elements like methods and
fields instead of directly on the return type or field type.
- Fixed a bug where enum values were generated as nullable.

## 0.13.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public FieldVisitor visitField(
field.defaultValue = value;
field.modifiers = TypeUtils.access(access);
if ((access & ACC_ENUM) != 0) {
field.type.type.annotations.add(JavaAnnotation.nonNull);
peekVisiting().values.add(name);
}
if (signature != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.github.dart_lang.jnigen.apisummarizer.doclet;

import com.github.dart_lang.jnigen.apisummarizer.elements.ClassDecl;
import com.github.dart_lang.jnigen.apisummarizer.elements.JavaAnnotation;
import com.github.dart_lang.jnigen.apisummarizer.elements.Method;
import com.github.dart_lang.jnigen.apisummarizer.elements.Package;
import com.github.dart_lang.jnigen.apisummarizer.util.Log;
Expand Down Expand Up @@ -119,6 +120,9 @@ public Void visitVariable(VariableElement e, SummaryCollector collector) {
var cls = collector.types.peek();
switch (vk) {
case ENUM_CONSTANT:
var field = builders.field(e);
field.type.type.annotations.add(JavaAnnotation.nonNull);
cls.fields.add(field);
cls.values.add(e.getSimpleName().toString());
break;
case FIELD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public class JavaAnnotation {
public String binaryName;
public Map<String, Object> properties = new HashMap<>();

public JavaAnnotation() {}

public JavaAnnotation(String binaryName) {
this.binaryName = binaryName;
}

public static final JavaAnnotation nonNull = new JavaAnnotation("androidx.annotation.NonNull");

public static class EnumVal {
public String enumClass;
public String value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,95 @@ class JsonFactory$Feature extends jni$_.JObject {
/// The type which includes information such as the signature of this class.
static const nullableType = $JsonFactory$Feature$NullableType();
static const type = $JsonFactory$Feature$Type();
static final _id_INTERN_FIELD_NAMES = _class.staticFieldId(
r'INTERN_FIELD_NAMES',
r'Lcom/fasterxml/jackson/core/JsonFactory$Feature;',
);

/// from: `static public final com.fasterxml.jackson.core.JsonFactory.Feature INTERN_FIELD_NAMES`
/// The returned object must be released after use, by calling the [release] method.
///
/// Feature that determines whether JSON object field names are
/// to be canonicalized using String\#intern or not:
/// if enabled, all field names will be intern()ed (and caller
/// can count on this being true for all such names); if disabled,
/// no intern()ing is done. There may still be basic
/// canonicalization (that is, same String will be used to represent
/// all identical object property names for a single document).
///
/// Note: this setting only has effect if
/// \#CANONICALIZE_FIELD_NAMES is true -- otherwise no
/// canonicalization of any sort is done.
///
/// This setting is enabled by default.
static JsonFactory$Feature get INTERN_FIELD_NAMES =>
_id_INTERN_FIELD_NAMES.get(_class, const $JsonFactory$Feature$Type());

static final _id_CANONICALIZE_FIELD_NAMES = _class.staticFieldId(
r'CANONICALIZE_FIELD_NAMES',
r'Lcom/fasterxml/jackson/core/JsonFactory$Feature;',
);

/// from: `static public final com.fasterxml.jackson.core.JsonFactory.Feature CANONICALIZE_FIELD_NAMES`
/// The returned object must be released after use, by calling the [release] method.
///
/// Feature that determines whether JSON object field names are
/// to be canonicalized (details of how canonicalization is done
/// then further specified by
/// \#INTERN_FIELD_NAMES).
///
/// This setting is enabled by default.
static JsonFactory$Feature get CANONICALIZE_FIELD_NAMES =>
_id_CANONICALIZE_FIELD_NAMES.get(
_class, const $JsonFactory$Feature$Type());

static final _id_FAIL_ON_SYMBOL_HASH_OVERFLOW = _class.staticFieldId(
r'FAIL_ON_SYMBOL_HASH_OVERFLOW',
r'Lcom/fasterxml/jackson/core/JsonFactory$Feature;',
);

/// from: `static public final com.fasterxml.jackson.core.JsonFactory.Feature FAIL_ON_SYMBOL_HASH_OVERFLOW`
/// The returned object must be released after use, by calling the [release] method.
///
/// Feature that determines what happens if we encounter a case in symbol
/// handling where number of hash collisions exceeds a safety threshold
/// -- which almost certainly means a denial-of-service attack via generated
/// duplicate hash codes.
/// If feature is enabled, an IllegalStateException is
/// thrown to indicate the suspected denial-of-service attack; if disabled, processing continues but
/// canonicalization (and thereby <code>intern()</code>ing) is disabled) as protective
/// measure.
///
/// This setting is enabled by default.
///@since 2.4
static JsonFactory$Feature get FAIL_ON_SYMBOL_HASH_OVERFLOW =>
_id_FAIL_ON_SYMBOL_HASH_OVERFLOW.get(
_class, const $JsonFactory$Feature$Type());

static final _id_USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING = _class.staticFieldId(
r'USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING',
r'Lcom/fasterxml/jackson/core/JsonFactory$Feature;',
);

/// from: `static public final com.fasterxml.jackson.core.JsonFactory.Feature USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING`
/// The returned object must be released after use, by calling the [release] method.
///
/// Feature that determines whether we will use BufferRecycler with
/// ThreadLocal and SoftReference, for efficient reuse of
/// underlying input/output buffers.
/// This usually makes sense on normal J2SE/J2EE server-side processing;
/// but may not make sense on platforms where SoftReference handling
/// is broken (like Android), or if there are retention issues due to
/// ThreadLocal (see
/// <a href="https://github.com/FasterXML/jackson-core/issues/189">jackson-core\#189</a>
/// for a possible case)
///
/// This setting is enabled by default.
///@since 2.6
static JsonFactory$Feature get USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING =>
_id_USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING.get(
_class, const $JsonFactory$Feature$Type());

static final _id_values = _class.staticMethodId(
r'values',
r'()[Lcom/fasterxml/jackson/core/JsonFactory$Feature;',
Expand Down
Loading

0 comments on commit 817c76e

Please sign in to comment.