From c52cc8a0c4ca08868fe807af15d42911d054c8d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Guillemet?= Date: Fri, 24 May 2024 18:45:21 +0200 Subject: [PATCH 1/5] Rename function parameters using a reserved keyword --- .../java/org/bytedeco/javacpp/tools/Parser.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/bytedeco/javacpp/tools/Parser.java b/src/main/java/org/bytedeco/javacpp/tools/Parser.java index a86cf3dd..8338a008 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Parser.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Parser.java @@ -89,6 +89,15 @@ public Parser(Logger logger, Properties properties, String encoding, String line this.lineSeparator = p.lineSeparator; } + /** Reserved keywords than cannot be used for Java identifier, but could be for C++ identifiers. */ + static final HashSet JAVA_KEYWORDS = new HashSet<>(); + static { + JAVA_KEYWORDS.addAll(Arrays.asList(new String[] { + "abstract", "assert", "boolean", "byte", "extends", "final", "finally", "implements", "import", + "instanceof", "interface", "native", "package", "strictfp", "super", "synchronized", "throws", "transient" + })); + } + final Logger logger; final Properties properties; final String encoding; @@ -1662,6 +1671,8 @@ Declarator declarator(Context context, String defaultName, int infoNumber, boole dcl.javaName = info.javaNames[0]; } + if (JAVA_KEYWORDS.contains(dcl.javaName)) dcl.javaName += '_'; + if (info != null && info.annotations != null) { for (String s : info.annotations) { if (!type.annotations.contains(s)) { @@ -1824,8 +1835,8 @@ Declarator declarator(Context context, String defaultName, int infoNumber, boole } } - // annotate with @Name if the Java name doesn't match with the C++ name - if (dcl.cppName != null) { + // annotate with @Name if the Java name doesn't match with the C++ name, and if we are not parsing a function parameter + if (dcl.cppName != null && defaultName == null) { String localName = dcl.cppName; if (context.namespace != null && localName.startsWith(context.namespace + "::")) { localName = dcl.cppName.substring(context.namespace.length() + 2); From 9b5b01776f68275d8b693440df9f634f886f86d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Guillemet?= Date: Sat, 25 May 2024 14:27:44 +0200 Subject: [PATCH 2/5] Use infoMap instead --- .../java/org/bytedeco/javacpp/tools/InfoMap.java | 5 +++++ .../java/org/bytedeco/javacpp/tools/Parser.java | 15 ++------------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/bytedeco/javacpp/tools/InfoMap.java b/src/main/java/org/bytedeco/javacpp/tools/InfoMap.java index a380b167..8f4fef77 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/InfoMap.java +++ b/src/main/java/org/bytedeco/javacpp/tools/InfoMap.java @@ -110,18 +110,23 @@ public class InfoMap extends HashMap> { .put(new Info("std::vector").annotations("@StdVector")) .put(new Info("abstract").javaNames("_abstract")) + .put(new Info("assert").javaNames("_assert")) .put(new Info("boolean").javaNames("_boolean")) .put(new Info("byte").javaNames("_byte")) .put(new Info("extends").javaNames("_extends")) + .put(new Info("final").javaNames("_final")) .put(new Info("finally").javaNames("_finally")) .put(new Info("implements").javaNames("_implements")) .put(new Info("import").javaNames("_import")) .put(new Info("instanceof").javaNames("_instanceof")) + .put(new Info("interface").javaNames("_interface")) .put(new Info("native").javaNames("_native")) .put(new Info("null").javaNames("_null")) .put(new Info("package").javaNames("_package")) + .put(new Info("strictfp").javaNames("_strictfp")) .put(new Info("super").javaNames("_super")) .put(new Info("synchronized").javaNames("_synchronized")) + .put(new Info("throws").javaNames("_throws")) .put(new Info("transient").javaNames("_transient")) .put(new Info("operator ->").javaNames("access")) diff --git a/src/main/java/org/bytedeco/javacpp/tools/Parser.java b/src/main/java/org/bytedeco/javacpp/tools/Parser.java index 8338a008..a86cf3dd 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Parser.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Parser.java @@ -89,15 +89,6 @@ public Parser(Logger logger, Properties properties, String encoding, String line this.lineSeparator = p.lineSeparator; } - /** Reserved keywords than cannot be used for Java identifier, but could be for C++ identifiers. */ - static final HashSet JAVA_KEYWORDS = new HashSet<>(); - static { - JAVA_KEYWORDS.addAll(Arrays.asList(new String[] { - "abstract", "assert", "boolean", "byte", "extends", "final", "finally", "implements", "import", - "instanceof", "interface", "native", "package", "strictfp", "super", "synchronized", "throws", "transient" - })); - } - final Logger logger; final Properties properties; final String encoding; @@ -1671,8 +1662,6 @@ Declarator declarator(Context context, String defaultName, int infoNumber, boole dcl.javaName = info.javaNames[0]; } - if (JAVA_KEYWORDS.contains(dcl.javaName)) dcl.javaName += '_'; - if (info != null && info.annotations != null) { for (String s : info.annotations) { if (!type.annotations.contains(s)) { @@ -1835,8 +1824,8 @@ Declarator declarator(Context context, String defaultName, int infoNumber, boole } } - // annotate with @Name if the Java name doesn't match with the C++ name, and if we are not parsing a function parameter - if (dcl.cppName != null && defaultName == null) { + // annotate with @Name if the Java name doesn't match with the C++ name + if (dcl.cppName != null) { String localName = dcl.cppName; if (context.namespace != null && localName.startsWith(context.namespace + "::")) { localName = dcl.cppName.substring(context.namespace.length() + 2); From 4b68052b935114e70d167151c631cef6046966e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Guillemet?= Date: Mon, 27 May 2024 08:48:56 +0200 Subject: [PATCH 3/5] Fix multiple ... generation --- src/main/java/org/bytedeco/javacpp/tools/Parser.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/bytedeco/javacpp/tools/Parser.java b/src/main/java/org/bytedeco/javacpp/tools/Parser.java index a86cf3dd..4c80f771 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Parser.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Parser.java @@ -357,7 +357,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio if (dim < 2 && !javaName.equals("int") && !javaName.equals("long")) { decl.text += " public " + containerType.javaName + "(" + javaName + " value) { this(1); put(0, value); }\n"; } - decl.text += " public " + containerType.javaName + "(" + javaName + arrayBrackets + " ... array) { this(array.length); put(array); }\n"; + decl.text += " public " + containerType.javaName + "(" + desugarVarargs(javaName) + arrayBrackets + " ... array) { this(array.length); put(array); }\n"; } } else if (indexType == null && dim == 0 && !constant && !purify) { int n = 0; @@ -597,7 +597,7 @@ void containers(Context context, DeclarationList declList) throws ParserExceptio + " return put(0, value);\n" + " }\n"; } - decl.text += " public " + containerType.javaName + " put(" + javaName + arrayBrackets + " ... array) {\n"; + decl.text += " public " + containerType.javaName + " put(" + desugarVarargs(javaName) + arrayBrackets + " ... array) {\n"; String indent = " ", indices = "", args = ""; separator = ""; for (int i = 0; i < dim; i++) { From 4256f5e052ce3f587420b08fccdabd8f4117b7ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Guillemet?= Date: Tue, 4 Jun 2024 13:21:28 +0200 Subject: [PATCH 4/5] Allow `@Deprecated` on constructors --- src/main/java/org/bytedeco/javacpp/tools/Parser.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/bytedeco/javacpp/tools/Parser.java b/src/main/java/org/bytedeco/javacpp/tools/Parser.java index 4c80f771..eaaf3222 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Parser.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Parser.java @@ -127,6 +127,10 @@ static String desugarVarargs(String s) { return s.trim().endsWith("...") ? s.trim().substring(0, s.length() - 3) + "[]" : s; } + static String filterJavaAnnotations(String s) { + return s.contains("@Deprecated") ? "@Deprecated " : ""; + } + static String upcastMethodName(String javaName) { String shortName = javaName.substring(javaName.lastIndexOf('.') + 1); return "as" + Character.toUpperCase(shortName.charAt(0)) + shortName.substring(1); @@ -2740,7 +2744,7 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti } } if (type.constructor && params != null) { - decl.text += "public " + context.shorten(context.javaName) + dcl.parameters.list + " { super((Pointer)null); allocate" + params.names + "; }\n" + + decl.text += filterJavaAnnotations(type.annotations) + "public " + context.shorten(context.javaName) + dcl.parameters.list + " { super((Pointer)null); allocate" + params.names + "; }\n" + type.annotations + "private native void allocate" + dcl.parameters.list + ";\n"; } else { String modifiers2 = modifiers; @@ -3494,7 +3498,7 @@ String downcast(Type derived, Type base, boolean virtual) { } String shortName = derived.javaName.substring(derived.javaName.lastIndexOf('.') + 1); String res = " /** Downcast constructor. */\n" - + " public " + shortName + "(" + base.javaName + " pointer) { super((Pointer)null); allocate(pointer); }\n"; + + " " + filterJavaAnnotations(annotations) + "public " + shortName + "(" + base.javaName + " pointer) { super((Pointer)null); allocate(pointer); }\n"; if (annotations.isEmpty()) { res += " @Namespace private native @Name(\"" + downcastType + "_cast<" + derived.cppName + "*>\") void allocate(" + base.javaName + " pointer);\n"; } else { @@ -3931,7 +3935,7 @@ boolean group(Context context, DeclarationList declList) throws ParserException if (implicitConstructor && (info == null || !info.purify) && (!abstractClass || ctx.virtualize)) { constructors += " /** Default native constructor. */\n" + - " public " + shortName + "() { super((Pointer)null); allocate(); }\n"; + " " + filterJavaAnnotations(constructorAnnotations) + "public " + shortName + "() { super((Pointer)null); allocate(); }\n"; if (constructorAnnotations.isEmpty()) { constructors += " /** Native array allocator. Access with {@link Pointer#position(long)}. */\n" + " public " + shortName + "(long size) { super((Pointer)null); allocateArray(size); }\n"; From efb8b15269075923507cbd5404f12a5fb2354c33 Mon Sep 17 00:00:00 2001 From: Samuel Audet Date: Fri, 7 Jun 2024 20:36:05 +0900 Subject: [PATCH 5/5] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45120733..eea3fc76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ + * Let `Parser` annotate Java constructors with `@Deprecated` when appropriate ([pull #757](https://github.com/bytedeco/javacpp/pull/757)) + * Add to `InfoMap.defaults` more names that are reserved in Java, but not in C++ ([pull #757](https://github.com/bytedeco/javacpp/pull/757)) * Fix inconsistencies when using `@Platform(inherit=..., library=...)` together ([pull #747](https://github.com/bytedeco/javacpp/pull/747)) * Let `Parser` support templates with unnamed type parameters ([pull #742](https://github.com/bytedeco/javacpp/pull/742)) * Prevent `Parser` from producing duplicate declarations for basic containers ([pull #741](https://github.com/bytedeco/javacpp/pull/741))