From 1721c1b48751b3dbc5528792b5ad5cea76adb03b Mon Sep 17 00:00:00 2001 From: Samuel Audet Date: Fri, 30 Nov 2018 11:14:25 +0900 Subject: [PATCH 1/4] Update version in the `pom.xml` file to 1.5-SNAPSHOT --- README.md | 20 +++++--------------- pom.xml | 2 +- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e3bc55e6c..5b21df75d 100644 --- a/README.md +++ b/README.md @@ -154,9 +154,7 @@ public class NativeLibrary { After compiling the Java source code in the usual way, we also need to build using JavaCPP before executing it as follows: ```bash -$ javac -cp javacpp.jar NativeLibrary.java -$ java -jar javacpp.jar NativeLibrary -$ java -cp javacpp.jar NativeLibrary +$ java -jar javacpp.jar NativeLibrary.java -exec Hello World! ``` @@ -213,9 +211,7 @@ public class VectorTest { Executing that program using these commands produces the following output: ```bash -$ javac -cp javacpp.jar VectorTest.java -$ java -jar javacpp.jar VectorTest -$ java -cp javacpp.jar VectorTest +$ java -jar javacpp.jar VectorTest.java -exec 13 42 org.bytedeco.javacpp.Pointer[address=0xdeadbeef,position=0,limit=0,capacity=0,deallocator=null] Exception in thread "main" java.lang.RuntimeException: vector::_M_range_check: __n (which is 42) >= this->size() (which is 13) at VectorTest$PointerVectorVector.at(Native Method) @@ -267,9 +263,7 @@ public class Processor { It would then compile and execute like this: ```bash -$ javac -cp javacpp.jar Processor.java -$ java -jar javacpp.jar Processor -$ java -cp javacpp.jar Processor +$ java -jar javacpp.jar Processor.java -exec Processing in C++... ``` @@ -325,8 +319,7 @@ public class Foo { Since functions also have pointers, we can use `FunctionPointer` instances accordingly, in ways similar to the [`FunPtr` type of Haskell FFI](http://hackage.haskell.org/packages/archive/base/4.6.0.0/doc/html/Foreign-Ptr.html#g:2), but where any `java.lang.Throwable` object thrown gets translated to `std::exception`. Building and running this sample code with these commands under Linux x86_64 produces the expected output: ```bash -$ javac -cp javacpp.jar Foo.java -$ java -jar javacpp.jar Foo -header +$ java -jar javacpp.jar Foo.java -header $ g++ -I/usr/lib/jvm/java/include/ -I/usr/lib/jvm/java/include/linux/ foo.cpp linux-x86_64/libjniFoo.so -o foo $ ./foo java.lang.Exception: bar 42 @@ -351,7 +344,6 @@ public: void callback(Foo *foo) { foo->bar(); } - ``` The function `Foo::bar()` can be overridden in Java if we declare the method in the peer class either as `native` or `abstract` and annotate it with `@Virtual`, for example: @@ -391,9 +383,7 @@ public class VirtualFoo { Which outputs what one would naturally assume: ```bash -$ javac -cp javacpp.jar VirtualFoo.java -$ java -jar javacpp.jar VirtualFoo -$ java -cp javacpp.jar VirtualFoo +$ java -jar javacpp.jar VirtualFoo.java -exec Callback in C++ (n == 13) Callback in Java (n == 42) Callback in C++ (n == 13) diff --git a/pom.xml b/pom.xml index 47bf7f06e..05d2b865d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.bytedeco javacpp - 1.4.4-SNAPSHOT + 1.5-SNAPSHOT JavaCPP The missing bridge between Java and native C++ From cd7b2c4b3185072e1b202a36a6ac778e53dc7c0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Guillemet?= Date: Mon, 7 Jan 2019 18:09:32 +0100 Subject: [PATCH 2/4] Use regex to translate Doxygen commands --- .../org/bytedeco/javacpp/tools/Parser.java | 68 ++++++++++++------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/bytedeco/javacpp/tools/Parser.java b/src/main/java/org/bytedeco/javacpp/tools/Parser.java index 1abd16b62..53aeccd77 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Parser.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Parser.java @@ -22,23 +22,17 @@ package org.bytedeco.javacpp.tools; +import org.bytedeco.javacpp.ClassProperties; +import org.bytedeco.javacpp.Loader; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.Writer; import java.nio.file.Files; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.bytedeco.javacpp.ClassProperties; -import org.bytedeco.javacpp.Loader; /** * The Parser, just like the Generator, is a mess that is not meant to support the @@ -1427,8 +1421,32 @@ Declarator declarator(Context context, String defaultName, int infoNumber, boole } /** Documentation tags, where we keep only the ones that could be compatible between Javadoc and Doxygen. */ - String[] docTags = {"author", "deprecated", "exception", "param", "return", "see", "since", "throws", "version", - /* "code", "docRoot", "inheritDoc", "link", "linkplain", "literal", "serial", "serialData", "serialField", "value" */}; + + private static String[][] docTagsStr = { + { "authors?\\b", "author" }, + { "deprecated\\b", "deprecated" }, + { "(?:exception|throws?)\\b", "throws" }, + { "param\\s*\\[[a-z,\\s]+\\]", "param" }, + { "param\\b", "param" }, + { "(?:returns?|result)\\b", "return" }, + { "(?:see|sa)\\b", "see" }, + { "since\\b", "since" }, + { "version\\b", "version" } + /* "code", "docRoot", "inheritDoc", "link", "linkplain", "literal", "serial", "serialData", "serialField", "value" */ + }; + private static class DocTag { + private Pattern pattern; + private String replacement; + DocTag(String p, String r) { + pattern = Pattern.compile(p); + replacement = r; + } + } + private static DocTag[] docTags = new DocTag[docTagsStr.length]; + static { + for (int i=0; i"); } else if (c == '\\' || c == '@') { - String foundTag = null; - for (String tag : docTags) { - if (ss.startsWith(tag)) { - foundTag = tag; + boolean tagFound = false; + for (DocTag tag : docTags) { + Matcher matcher = tag.pattern.matcher(ss); + if (matcher.lookingAt()) { + sb.replace(index + matcher.start() + 1, + index + matcher.end() + 1, + tag.replacement); + sb.setCharAt(index, '@'); + int n = index + 1 + tag.replacement.length(); + if (!Character.isWhitespace(sb.charAt(n))) + sb.insert(n, ' '); + tagFound = true; break; } } - if (foundTag != null) { - sb.setCharAt(index, '@'); - int n = index + foundTag.length() + 1; - if (sb.charAt(n) == 's' && !foundTag.endsWith("s")) { - sb.deleteCharAt(n); - } else if (!Character.isWhitespace(sb.charAt(n))) { - sb.insert(n, ' '); - } - } else { + if (!tagFound) { // keep unmapped tags around as part of the comments sb.setCharAt(index, '\\'); } From 15278462c914ba71d06fc0f7d193e27404ca8c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Guillemet?= Date: Tue, 8 Jan 2019 12:27:30 +0100 Subject: [PATCH 3/4] Translate \param[in] p x to @param p [in] x --- src/main/java/org/bytedeco/javacpp/tools/Parser.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/bytedeco/javacpp/tools/Parser.java b/src/main/java/org/bytedeco/javacpp/tools/Parser.java index 53aeccd77..d6966e8b0 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Parser.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Parser.java @@ -1426,7 +1426,7 @@ Declarator declarator(Context context, String defaultName, int infoNumber, boole { "authors?\\b", "author" }, { "deprecated\\b", "deprecated" }, { "(?:exception|throws?)\\b", "throws" }, - { "param\\s*\\[[a-z,\\s]+\\]", "param" }, + { "param\\s*(\\[[a-z,\\s]+\\])\\s+(\\S+)", "param $2 $1 " }, { "param\\b", "param" }, { "(?:returns?|result)\\b", "return" }, { "(?:see|sa)\\b", "see" }, @@ -1505,13 +1505,11 @@ String commentDoc(String s, int startIndex) { for (DocTag tag : docTags) { Matcher matcher = tag.pattern.matcher(ss); if (matcher.lookingAt()) { - sb.replace(index + matcher.start() + 1, - index + matcher.end() + 1, - tag.replacement); + StringBuffer sbuf = new StringBuffer(); + matcher.appendReplacement(sbuf, tag.replacement); + sb.replace(index + 1+ matcher.start(), + index + 1 + matcher.end(), sbuf.toString()); sb.setCharAt(index, '@'); - int n = index + 1 + tag.replacement.length(); - if (!Character.isWhitespace(sb.charAt(n))) - sb.insert(n, ' '); tagFound = true; break; } From 6349ce38e7c49e7c1f7d28bd7e5233c0f93bde42 Mon Sep 17 00:00:00 2001 From: Samuel Audet Date: Wed, 9 Jan 2019 11:20:58 +0900 Subject: [PATCH 4/4] Fix formatting and add copyright --- .../org/bytedeco/javacpp/tools/DocTag.java | 57 +++++++++++++++++++ .../org/bytedeco/javacpp/tools/Parser.java | 44 ++++---------- 2 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 src/main/java/org/bytedeco/javacpp/tools/DocTag.java diff --git a/src/main/java/org/bytedeco/javacpp/tools/DocTag.java b/src/main/java/org/bytedeco/javacpp/tools/DocTag.java new file mode 100644 index 000000000..557f82bfb --- /dev/null +++ b/src/main/java/org/bytedeco/javacpp/tools/DocTag.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2019 Samuel Audet, Hervé Guillemet + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE.txt file that accompanied this code. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.bytedeco.javacpp.tools; + +import java.util.regex.Pattern; + +/** + * Documentation tags, where we keep only the ones that could be compatible between Javadoc and Doxygen. + * + * @author Hervé Guillemet + */ +class DocTag { + static String[][] docTagsStr = { + { "authors?\\b", "author" }, + { "deprecated\\b", "deprecated" }, + { "(?:exception|throws?)\\b", "throws" }, + { "param\\s*(\\[[a-z,\\s]+\\])\\s+(\\S+)", "param $2 $1 " }, + { "param\\b", "param" }, + { "(?:returns?|result)\\b", "return" }, + { "(?:see|sa)\\b", "see" }, + { "since\\b", "since" }, + { "version\\b", "version" } + /* "code", "docRoot", "inheritDoc", "link", "linkplain", "literal", "serial", "serialData", "serialField", "value" */ + }; + static DocTag[] docTags = new DocTag[docTagsStr.length]; + static { + for (int i=0; i s.length()) { @@ -1502,7 +1480,7 @@ String commentDoc(String s, int startIndex) { sb.insert(index + 1, indent + "

"); } else if (c == '\\' || c == '@') { boolean tagFound = false; - for (DocTag tag : docTags) { + for (DocTag tag : DocTag.docTags) { Matcher matcher = tag.pattern.matcher(ss); if (matcher.lookingAt()) { StringBuffer sbuf = new StringBuffer();