From d229c4a4be3b2412b5fcb40887c96d711171a7e0 Mon Sep 17 00:00:00 2001 From: louxiu Date: Wed, 31 Jan 2018 16:44:27 +0800 Subject: [PATCH] Fix generate of @Const on function --- .../bytedeco/javacpp/annotation/Const.java | 14 ++-- .../org/bytedeco/javacpp/tools/Generator.java | 76 ++++++++++++------- .../org/bytedeco/javacpp/tools/Parser.java | 13 ++-- 3 files changed, 65 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/bytedeco/javacpp/annotation/Const.java b/src/main/java/org/bytedeco/javacpp/annotation/Const.java index e4394a599..db5f39650 100644 --- a/src/main/java/org/bytedeco/javacpp/annotation/Const.java +++ b/src/main/java/org/bytedeco/javacpp/annotation/Const.java @@ -5,14 +5,18 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.bytedeco.javacpp.FunctionPointer; -import org.bytedeco.javacpp.tools.Generator; /** - * A shortcut annotation to {@link Cast} that simply adds {@code const} to the parameter type. - * Can also be declared on a {@link FunctionPointer} in the case of {@code const} functions. + * A shortcut annotation to {@link Cast} that simply adds {@code const} to the parameter type, function or class. * - * @see Generator + *

+ * + * @see {@link org.bytedeco.javacpp.tools.Generator} * * @author Samuel Audet */ diff --git a/src/main/java/org/bytedeco/javacpp/tools/Generator.java b/src/main/java/org/bytedeco/javacpp/tools/Generator.java index 0ac3a0e61..3a94f40bc 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Generator.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Generator.java @@ -22,32 +22,6 @@ package org.bytedeco.javacpp.tools; -import java.io.Closeable; -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.Writer; -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.nio.Buffer; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.DoubleBuffer; -import java.nio.FloatBuffer; -import java.nio.IntBuffer; -import java.nio.LongBuffer; -import java.nio.ShortBuffer; -import java.util.ArrayDeque; -import java.util.Arrays; -import java.util.Deque; -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.Set; import org.bytedeco.javacpp.BoolPointer; import org.bytedeco.javacpp.BytePointer; import org.bytedeco.javacpp.CLongPointer; @@ -90,6 +64,30 @@ import org.bytedeco.javacpp.annotation.ValueSetter; import org.bytedeco.javacpp.annotation.Virtual; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.nio.Buffer; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.DoubleBuffer; +import java.nio.FloatBuffer; +import java.nio.IntBuffer; +import java.nio.LongBuffer; +import java.nio.ShortBuffer; +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.Set; + /** * The Generator is where all the C++ source code that we need gets generated. * It has not been designed in any meaningful way since the requirements were @@ -3210,6 +3208,27 @@ static String valueTypeName(String ... typeName) { return type; } + static boolean constFunction(Class classType, Method functionMethod) { + if (classType.isAnnotationPresent(Const.class)) { + return true; + } + + if (!functionMethod.isAnnotationPresent(Const.class)) { + return false; + } + + for (Annotation a : functionMethod.getDeclaredAnnotations()) { + if (a instanceof Const) { + boolean[] b = ((Const) a).value(); + if (b.length > 2 && b[2]) { + return true; + } + return false; + } + } + return false; + } + String[] cppAnnotationTypeName(Class type, Annotation ... annotations) { String[] typeName = cppCastTypeName(type, annotations); String prefix = typeName[0]; @@ -3387,6 +3406,9 @@ String[] cppFunctionTypeName(Method... functionMethods) { for (int j = namespace == null ? 0 : 1; j < parameterTypes.length; j++) { String[] paramTypeName = cppAnnotationTypeName(parameterTypes[j], parameterAnnotations[j]); AdapterInformation paramAdapterInfo = adapterInformation(false, valueTypeName(paramTypeName), parameterAnnotations[j]); + if (paramAdapterInfo != null && paramAdapterInfo.constant) { + suffix += "const "; + } if (paramAdapterInfo != null && paramAdapterInfo.cast.length() > 0) { suffix += paramAdapterInfo.cast + " arg" + j; } else { @@ -3398,7 +3420,7 @@ String[] cppFunctionTypeName(Method... functionMethods) { } suffix += ")"; } - if (type.isAnnotationPresent(Const.class)) { + if (constFunction(type, functionMethod)) { suffix += " const"; } return new String[] { prefix, suffix }; diff --git a/src/main/java/org/bytedeco/javacpp/tools/Parser.java b/src/main/java/org/bytedeco/javacpp/tools/Parser.java index fef7c9af6..e50a47748 100644 --- a/src/main/java/org/bytedeco/javacpp/tools/Parser.java +++ b/src/main/java/org/bytedeco/javacpp/tools/Parser.java @@ -22,6 +22,9 @@ 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; @@ -32,8 +35,6 @@ import java.util.List; import java.util.Map; import java.util.Properties; -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 @@ -1907,7 +1908,7 @@ boolean function(Context context, DeclarationList declList) throws ParserExcepti // add @Virtual annotation on user request only, inherited through context if (type.virtual && context.virtualize) { - modifiers = "@Virtual" + (decl.abstractMember ? "(true) " : " ") + modifiers = (decl.constMember ? "@Const({false, false, true}) " : " ") + "@Virtual" + (decl.abstractMember ? "(true) " : " ") + (context.inaccessible ? "protected native " : "public native "); } @@ -2733,7 +2734,7 @@ boolean group(Context context, DeclarationList declList) throws ParserException String modifiers = "public static ", constructors = ""; boolean implicitConstructor = true, defaultConstructor = false, longConstructor = false, pointerConstructor = false, abstractClass = info != null && info.purify && !ctx.virtualize, - havePureConst = false, haveVariables = false; + allPureConst = true, haveVariables = false; for (Declaration d : declList2) { if (d.declarator != null && d.declarator.type != null && d.declarator.type.using && decl.text != null) { // inheriting constructors @@ -2754,10 +2755,10 @@ boolean group(Context context, DeclarationList declList) throws ParserException pointerConstructor |= paramDcls.length == 1 && paramDcls[0].type.javaName.equals("Pointer") && !d.inaccessible; } abstractClass |= d.abstractMember; - havePureConst |= d.constMember && d.abstractMember; + allPureConst &= d.constMember && d.abstractMember; haveVariables |= d.variable; } - if (havePureConst && ctx.virtualize) { + if (allPureConst && ctx.virtualize) { modifiers = "@Const " + modifiers; } if (!anonymous) {