Skip to content

Commit b0fe0c3

Browse files
committed
* Add Info.beanify() to add JavaBeans-style prefixes to getter and setters of member variables generated by Parser
1 parent 82b8bde commit b0fe0c3

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/main/java/org/bytedeco/javacpp/tools/Context.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Context {
4444
constName = c.constName;
4545
constBaseName = c.constBaseName;
4646
immutable = c.immutable;
47+
beanify = c.beanify;
4748
inaccessible = c.inaccessible;
4849
objectify = c.objectify;
4950
virtualize = c.virtualize;
@@ -61,6 +62,7 @@ class Context {
6162
String constName = null;
6263
String constBaseName = null;
6364
boolean immutable = false;
65+
boolean beanify = false;
6466
boolean inaccessible = false;
6567
boolean objectify = false;
6668
boolean virtualize = false;

src/main/java/org/bytedeco/javacpp/tools/Info.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public Info(Info i) {
5353
define = i.define;
5454
enumerate = i.enumerate;
5555
flatten = i.flatten;
56+
immutable = i.immutable;
57+
beanify = i.beanify;
5658
objectify = i.objectify;
5759
translate = i.translate;
5860
skip = i.skip;
@@ -97,6 +99,8 @@ public Info(Info i) {
9799
boolean flatten = false;
98100
/** Disables generation of setters for public data members of a class */
99101
boolean immutable = false;
102+
/** Adds JavaBeans-style prefixes to getters and setters of public data members of a class */
103+
boolean beanify = false;
100104
/** Map global functions to instance methods, without {@code static} modifier, to implement an interface, etc. */
101105
boolean objectify = false;
102106
/** Attempts to translate naively the statements of variable-like macros to Java. */
@@ -134,6 +138,8 @@ public Info(Info i) {
134138
public Info flatten(boolean flatten) { this.flatten = flatten; return this; }
135139
public Info immutable() { this.immutable = true; return this; }
136140
public Info immutable(boolean immutable) { this.immutable = immutable; return this; }
141+
public Info beanify() { this.beanify = true; return this; }
142+
public Info beanify(boolean beanify) { this.beanify = beanify; return this; }
137143
public Info objectify() { this.objectify = true; return this; }
138144
public Info objectify(boolean objectify) { this.objectify = objectify; return this; }
139145
public Info translate() { this.translate = true; return this; }

src/main/java/org/bytedeco/javacpp/tools/Parser.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,25 +2573,43 @@ boolean variable(Context context, DeclarationList declList) throws ParserExcepti
25732573
if (context.namespace != null && context.javaName == null) {
25742574
decl.text += "@Namespace(\"" + context.namespace + "\") ";
25752575
}
2576-
if (metadcl != null && metadcl.cppName != null && metadcl.cppName.length() > 0) {
2577-
decl.text += metadcl.indices == 0
2576+
final boolean hasMetadcl = metadcl != null && metadcl.cppName != null && metadcl.cppName.length() > 0;
2577+
String nameAnnotation = "";
2578+
if (hasMetadcl) {
2579+
nameAnnotation = metadcl.indices == 0
25782580
? "@Name(\"" + metadcl.cppName + "." + shortName + "\") "
25792581
: "@Name({\"" + metadcl.cppName + "\", \"." + shortName + "\"}) ";
2580-
dcl.type.annotations = dcl.type.annotations.replaceAll("@Name\\(.*\\) ", "");
25812582
javaName = metadcl.javaName + "_" + shortName;
25822583
}
2584+
final boolean beanify = context.beanify && indices.isEmpty();
2585+
String capitalizedJavaName = null;
2586+
if (beanify) {
2587+
if (!hasMetadcl) {
2588+
nameAnnotation = "@Name(\"" + shortName + "\") ";
2589+
}
2590+
capitalizedJavaName = javaName.substring(0, 1).toUpperCase() + javaName.substring(1);
2591+
javaName = "get" + capitalizedJavaName;
2592+
}
2593+
if (hasMetadcl || beanify) {
2594+
dcl.type.annotations = dcl.type.annotations.replaceAll("@Name\\(.*\\) ", "");
2595+
decl.text += nameAnnotation;
2596+
}
2597+
dcl.type.annotations = dcl.type.annotations.replace("@ByVal ", "@ByRef ");
25832598
final boolean hasSetter = !(dcl.type.constValue && dcl.indirections == 0) && !dcl.constPointer && !dcl.type.constExpr && !context.immutable;
2584-
if (!hasSetter) {
2599+
if (!hasSetter || beanify) {
25852600
decl.text += "@MemberGetter ";
25862601
}
2587-
decl.text += modifiers + dcl.type.annotations.replace("@ByVal ", "@ByRef ")
2588-
+ dcl.type.javaName + " " + javaName + "(" + indices + ");";
2602+
decl.text += modifiers + dcl.type.annotations + dcl.type.javaName + " " + javaName + "(" + indices + ");";
25892603
if (hasSetter) {
25902604
if (indices.length() > 0) {
25912605
indices += ", ";
25922606
}
2593-
String javaTypeWithoutAnnotations = dcl.type.javaName.substring(dcl.type.javaName.lastIndexOf(" ") + 1);
2594-
decl.text += " " + modifiers + setterType + javaName + "(" + indices + javaTypeWithoutAnnotations + " setter);";
2607+
if (beanify) {
2608+
decl.text += "\n" + nameAnnotation + "@MemberSetter " + modifiers + setterType + "set" + capitalizedJavaName + "(" + indices + dcl.type.annotations + dcl.type.javaName + " setter);";
2609+
} else {
2610+
String javaTypeWithoutAnnotations = dcl.type.javaName.substring(dcl.type.javaName.lastIndexOf(" ") + 1);
2611+
decl.text += " " + nameAnnotation + modifiers + setterType + javaName + "(" + indices + javaTypeWithoutAnnotations + " setter);";
2612+
}
25952613
}
25962614
decl.text += "\n";
25972615
if ((dcl.type.constValue || dcl.constPointer || dcl.type.constExpr) && dcl.type.staticMember && indices.length() == 0) {
@@ -3313,6 +3331,8 @@ boolean group(Context context, DeclarationList declList) throws ParserException
33133331
ctx.virtualize = true;
33143332
if (info.immutable)
33153333
ctx.immutable = true;
3334+
if (info.beanify)
3335+
ctx.beanify = true;
33163336
}
33173337
ctx.baseType = base.cppName;
33183338

0 commit comments

Comments
 (0)