Skip to content

Commit c758499

Browse files
committed
* Add Info.addMemberPrefixes() to add JavaBeans-style prefixes to getter and setters of member variables generated by Parser
1 parent b4cf59a commit c758499

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-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+
addMemberPrefixes = c.addMemberPrefixes;
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 addMemberPrefixes = false;
6466
boolean inaccessible = false;
6567
boolean objectify = false;
6668
boolean virtualize = false;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public Info(Info i) {
9797
boolean flatten = false;
9898
/** Disables generation of setters for public data members of a class */
9999
boolean immutable = false;
100+
/** Adds JavaBeans-style prefixes to getters and setters of public data members of a class */
101+
boolean addMemberPrefixes = false;
100102
/** Map global functions to instance methods, without {@code static} modifier, to implement an interface, etc. */
101103
boolean objectify = false;
102104
/** Attempts to translate naively the statements of variable-like macros to Java. */
@@ -134,6 +136,8 @@ public Info(Info i) {
134136
public Info flatten(boolean flatten) { this.flatten = flatten; return this; }
135137
public Info immutable() { this.immutable = true; return this; }
136138
public Info immutable(boolean immutable) { this.immutable = immutable; return this; }
139+
public Info addMemberPrefixes() { this.addMemberPrefixes = true; return this; }
140+
public Info addMemberPrefixes(boolean add) { this.addMemberPrefixes = add; return this; }
137141
public Info objectify() { this.objectify = true; return this; }
138142
public Info objectify(boolean objectify) { this.objectify = objectify; return this; }
139143
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
@@ -2552,25 +2552,43 @@ boolean variable(Context context, DeclarationList declList) throws ParserExcepti
25522552
if (context.namespace != null && context.javaName == null) {
25532553
decl.text += "@Namespace(\"" + context.namespace + "\") ";
25542554
}
2555-
if (metadcl != null && metadcl.cppName != null && metadcl.cppName.length() > 0) {
2556-
decl.text += metadcl.indices == 0
2555+
final boolean hasMetadcl = metadcl != null && metadcl.cppName != null && metadcl.cppName.length() > 0;
2556+
String nameAnnotation = "";
2557+
if (hasMetadcl) {
2558+
nameAnnotation = metadcl.indices == 0
25572559
? "@Name(\"" + metadcl.cppName + "." + shortName + "\") "
25582560
: "@Name({\"" + metadcl.cppName + "\", \"." + shortName + "\"}) ";
2559-
dcl.type.annotations = dcl.type.annotations.replaceAll("@Name\\(.*\\) ", "");
25602561
javaName = metadcl.javaName + "_" + shortName;
25612562
}
2563+
final boolean addMemberPrefixes = context.addMemberPrefixes && indices.isEmpty();
2564+
String capitalizedJavaName = null;
2565+
if (addMemberPrefixes) {
2566+
if (!hasMetadcl) {
2567+
nameAnnotation = "@Name(\"" + shortName + "\") ";
2568+
}
2569+
capitalizedJavaName = javaName.substring(0, 1).toUpperCase() + javaName.substring(1);
2570+
javaName = "get" + capitalizedJavaName;
2571+
}
2572+
if (hasMetadcl || addMemberPrefixes) {
2573+
dcl.type.annotations = dcl.type.annotations.replaceAll("@Name\\(.*\\) ", "");
2574+
decl.text += nameAnnotation;
2575+
}
2576+
dcl.type.annotations = dcl.type.annotations.replace("@ByVal ", "@ByRef ");
25622577
final boolean hasSetter = !(dcl.type.constValue && dcl.indirections == 0) && !dcl.constPointer && !dcl.type.constExpr && !context.immutable;
2563-
if (!hasSetter) {
2578+
if (!hasSetter || addMemberPrefixes) {
25642579
decl.text += "@MemberGetter ";
25652580
}
2566-
decl.text += modifiers + dcl.type.annotations.replace("@ByVal ", "@ByRef ")
2567-
+ dcl.type.javaName + " " + javaName + "(" + indices + ");";
2581+
decl.text += modifiers + dcl.type.annotations + dcl.type.javaName + " " + javaName + "(" + indices + ");";
25682582
if (hasSetter) {
25692583
if (indices.length() > 0) {
25702584
indices += ", ";
25712585
}
2572-
String javaTypeWithoutAnnotations = dcl.type.javaName.substring(dcl.type.javaName.lastIndexOf(" ") + 1);
2573-
decl.text += " " + modifiers + setterType + javaName + "(" + indices + javaTypeWithoutAnnotations + " setter);";
2586+
if (addMemberPrefixes) {
2587+
decl.text += "\n" + nameAnnotation + "@MemberSetter " + modifiers + setterType + "set" + capitalizedJavaName + "(" + indices + dcl.type.annotations + dcl.type.javaName + " setter);";
2588+
} else {
2589+
String javaTypeWithoutAnnotations = dcl.type.javaName.substring(dcl.type.javaName.lastIndexOf(" ") + 1);
2590+
decl.text += " " + nameAnnotation + modifiers + setterType + javaName + "(" + indices + javaTypeWithoutAnnotations + " setter);";
2591+
}
25742592
}
25752593
decl.text += "\n";
25762594
if ((dcl.type.constValue || dcl.constPointer || dcl.type.constExpr) && dcl.type.staticMember && indices.length() == 0) {
@@ -3292,6 +3310,8 @@ boolean group(Context context, DeclarationList declList) throws ParserException
32923310
ctx.virtualize = true;
32933311
if (info.immutable)
32943312
ctx.immutable = true;
3313+
if (info.addMemberPrefixes)
3314+
ctx.addMemberPrefixes = true;
32953315
}
32963316
ctx.baseType = base.cppName;
32973317

0 commit comments

Comments
 (0)