Skip to content

Commit

Permalink
Update CHANGELOG.md and fix nits
Browse files Browse the repository at this point in the history
  • Loading branch information
saudet committed Jan 30, 2024
1 parent ddf9e4b commit 4a0f738
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

* Prevent `Parser` from producing duplicate declarations for basic containers ([pull #741](https://github.com/bytedeco/javacpp/pull/741))

### January 29, 2024 version 1.5.10
* Move native `Loader` methods to `Helper` class to avoid deadlocks ([issue #737](https://github.com/bytedeco/javacpp/issues/737))
* Fix `Parser` failing to pick up `Info` for constructors with template arguments ([pull #739](https://github.com/bytedeco/javacpp/pull/739))
Expand Down
35 changes: 17 additions & 18 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,19 @@ public Parser(Logger logger, Properties properties, String encoding, String line
/** Java names of classes recognized as polymorphic. */
Set<String> polymorphicClasses = new HashSet<>();

private void addDowncast(String base, Type from, boolean virtual) {
void addDowncast(String base, Type from, boolean virtual) {
Map<Type, Boolean> inheritance = downcasts.get(base);
if (inheritance == null) {
downcasts.put(base, inheritance = new HashMap<>(1));
}
inheritance.put(from, virtual);
}

/** De-deduplicate an array of strings, keeping order. */
static String[] removeDuplicates(String[] a) {
return a != null && a.length > 1 ? new LinkedHashSet<>(Arrays.asList(a)).toArray(new String[0]) : a;
}

static String removeAnnotations(String s) {
return s.substring(s.lastIndexOf(' ') + 1);
}
Expand Down Expand Up @@ -181,12 +186,6 @@ String translate(String text) {
return text;
}

/** De-deduplicate an array of strings, keeping order. */
private static String[] uniq(String[] a) {
if (a == null || a.length <= 1) return a;
return new LinkedHashSet<>(Arrays.asList(a)).toArray(new String[0]);
}

void containers(Context context, DeclarationList declList) throws ParserException {
List<String> basicContainers = new ArrayList<>();
for (Info info : infoMap.get("basic/containers")) {
Expand Down Expand Up @@ -345,16 +344,16 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
+ " public " + containerType.javaName + "(Pointer p) { super(p); }\n";
boolean purify = info != null && info.purify;
if (!constant && !purify && (dim == 0 || (containerType.arguments.length == 1 && indexType != null)) && firstType != null && secondType != null) {
String[] firstNames = firstType.javaNames != null ? uniq(firstType.javaNames) : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? uniq(secondType.javaNames) : new String[] {secondType.javaName};
String[] firstNames = firstType.javaNames != null ? removeDuplicates(firstType.javaNames) : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? removeDuplicates(secondType.javaNames) : new String[] {secondType.javaName};
String brackets = arrayBrackets + (dim > 0 ? "[]" : "");
for (int n = 0; n < firstNames.length || n < secondNames.length; n++) {
decl.text += " public " + containerType.javaName + "(" + firstNames[Math.min(n, firstNames.length - 1)] + brackets + " firstValue, "
+ secondNames[Math.min(n, secondNames.length - 1)] + brackets + " secondValue) "
+ "{ this(" + (dim > 0 ? "Math.min(firstValue.length, secondValue.length)" : "") + "); put(firstValue, secondValue); }\n";
}
} else if (resizable && !purify && firstType == null && secondType == null) {
for (String javaName : valueType.javaNames != null ? uniq(valueType.javaNames) : new String[] {valueType.javaName}) {
for (String javaName : valueType.javaNames != null ? removeDuplicates(valueType.javaNames) : new String[] {valueType.javaName}) {
if (dim < 2 && !javaName.equals("int") && !javaName.equals("long")) {
decl.text += " public " + containerType.javaName + "(" + javaName + " value) { this(1); put(0, value); }\n";
}
Expand All @@ -369,7 +368,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
valueNames2 += separator + "value" + n;
separator = ", ";
n++;
} else for (String javaName : type.javaNames != null ? uniq(type.javaNames) : new String[] {type.javaName}) {
} else for (String javaName : type.javaNames != null ? removeDuplicates(type.javaNames) : new String[] {type.javaName}) {
// variant, optional, etc
if (!javaName.substring(javaName.indexOf(' ') + 1).equals("Pointer")) {
decl.text += " public " + containerType.javaName + "(" + javaName + " value) { this(); put(value); }\n";
Expand Down Expand Up @@ -422,8 +421,8 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
+ " public native " + containerType.javaName + " first(" + params + separator + removeAnnotations(firstType.javaName) + " first);\n"
+ " " + indexAnnotation + "public native " + secondType.annotations + secondType.javaName + " second(" + params + "); "
+ " public native " + containerType.javaName + " second(" + params + separator + removeAnnotations(secondType.javaName) + " second);\n";
firstType.javaNames = uniq(firstType.javaNames);
secondType.javaNames = uniq(secondType.javaNames);
firstType.javaNames = removeDuplicates(firstType.javaNames);
secondType.javaNames = removeDuplicates(secondType.javaNames);
for (int i = 1; !constant && firstType.javaNames != null && i < firstType.javaNames.length; i++) {
decl.text += " @MemberSetter @Index" + indexFunction + " public native " + containerType.javaName + " first(" + params + separator + firstType.annotations + firstType.javaNames[i] + " first);\n";
}
Expand All @@ -441,7 +440,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
if (!constant) {
decl.text += " public native " + containerType.javaName + " put(" + params + separator + removeAnnotations(valueType.javaName) + " value);\n";
}
valueType.javaNames = uniq(valueType.javaNames);
valueType.javaNames = removeDuplicates(valueType.javaNames);
for (int i = 1; !constant && valueType.javaNames != null && i < valueType.javaNames.length; i++) {
decl.text += " @ValueSetter @Index" + indexFunction + " public native " + containerType.javaName + " put(" + params + separator + valueType.annotations + valueType.javaNames[i] + " value);\n";
}
Expand All @@ -461,7 +460,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
if (!constant && !tuple) {
decl.text += " @ValueSetter public native " + containerType.javaName + " put(" + type.annotations + type.javaName + " value);\n";
}
type.javaNames = uniq(type.javaNames);
type.javaNames = removeDuplicates(type.javaNames);
for (int i = 1; !constant && !tuple && type.javaNames != null && i < type.javaNames.length; i++) {
decl.text += " @ValueSetter public native " + containerType.javaName + " put(" + type.annotations + type.javaNames[i] + " value);\n";
}
Expand Down Expand Up @@ -542,8 +541,8 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
}

if (!constant && (dim == 0 || (containerType.arguments.length == 1 && indexType != null)) && firstType != null && secondType != null) {
String[] firstNames = firstType.javaNames != null ? uniq(firstType.javaNames) : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? uniq(secondType.javaNames) : new String[] {secondType.javaName};
String[] firstNames = firstType.javaNames != null ? removeDuplicates(firstType.javaNames) : new String[] {firstType.javaName};
String[] secondNames = secondType.javaNames != null ? removeDuplicates(secondType.javaNames) : new String[] {secondType.javaName};
String brackets = arrayBrackets + (dim > 0 ? "[]" : "");
for (int n = 0; n < firstNames.length || n < secondNames.length; n++) {
String firstName = firstNames[Math.min(n, firstNames.length - 1)];
Expand Down Expand Up @@ -576,7 +575,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio
}
} else if (resizable && firstType == null && secondType == null) {
boolean first = true;
for (String javaName : valueType.javaNames != null ? uniq(valueType.javaNames) : new String[] {valueType.javaName}) {
for (String javaName : valueType.javaNames != null ? removeDuplicates(valueType.javaNames) : new String[] {valueType.javaName}) {
javaName = removeAnnotations(javaName);
decl.text += "\n";
if (dim < 2) {
Expand Down

0 comments on commit 4a0f738

Please sign in to comment.