Skip to content

Commit

Permalink
* Add Info.beanify() to add JavaBeans-style prefixes to getter and…
Browse files Browse the repository at this point in the history
… setters of member variables generated by `Parser`
  • Loading branch information
equeim committed Jun 11, 2021
1 parent 82b8bde commit b0fe0c3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Context {
constName = c.constName;
constBaseName = c.constBaseName;
immutable = c.immutable;
beanify = c.beanify;
inaccessible = c.inaccessible;
objectify = c.objectify;
virtualize = c.virtualize;
Expand All @@ -61,6 +62,7 @@ class Context {
String constName = null;
String constBaseName = null;
boolean immutable = false;
boolean beanify = false;
boolean inaccessible = false;
boolean objectify = false;
boolean virtualize = false;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/bytedeco/javacpp/tools/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public Info(Info i) {
define = i.define;
enumerate = i.enumerate;
flatten = i.flatten;
immutable = i.immutable;
beanify = i.beanify;
objectify = i.objectify;
translate = i.translate;
skip = i.skip;
Expand Down Expand Up @@ -97,6 +99,8 @@ public Info(Info i) {
boolean flatten = false;
/** Disables generation of setters for public data members of a class */
boolean immutable = false;
/** Adds JavaBeans-style prefixes to getters and setters of public data members of a class */
boolean beanify = false;
/** Map global functions to instance methods, without {@code static} modifier, to implement an interface, etc. */
boolean objectify = false;
/** Attempts to translate naively the statements of variable-like macros to Java. */
Expand Down Expand Up @@ -134,6 +138,8 @@ public Info(Info i) {
public Info flatten(boolean flatten) { this.flatten = flatten; return this; }
public Info immutable() { this.immutable = true; return this; }
public Info immutable(boolean immutable) { this.immutable = immutable; return this; }
public Info beanify() { this.beanify = true; return this; }
public Info beanify(boolean beanify) { this.beanify = beanify; return this; }
public Info objectify() { this.objectify = true; return this; }
public Info objectify(boolean objectify) { this.objectify = objectify; return this; }
public Info translate() { this.translate = true; return this; }
Expand Down
36 changes: 28 additions & 8 deletions src/main/java/org/bytedeco/javacpp/tools/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2573,25 +2573,43 @@ boolean variable(Context context, DeclarationList declList) throws ParserExcepti
if (context.namespace != null && context.javaName == null) {
decl.text += "@Namespace(\"" + context.namespace + "\") ";
}
if (metadcl != null && metadcl.cppName != null && metadcl.cppName.length() > 0) {
decl.text += metadcl.indices == 0
final boolean hasMetadcl = metadcl != null && metadcl.cppName != null && metadcl.cppName.length() > 0;
String nameAnnotation = "";
if (hasMetadcl) {
nameAnnotation = metadcl.indices == 0
? "@Name(\"" + metadcl.cppName + "." + shortName + "\") "
: "@Name({\"" + metadcl.cppName + "\", \"." + shortName + "\"}) ";
dcl.type.annotations = dcl.type.annotations.replaceAll("@Name\\(.*\\) ", "");
javaName = metadcl.javaName + "_" + shortName;
}
final boolean beanify = context.beanify && indices.isEmpty();
String capitalizedJavaName = null;
if (beanify) {
if (!hasMetadcl) {
nameAnnotation = "@Name(\"" + shortName + "\") ";
}
capitalizedJavaName = javaName.substring(0, 1).toUpperCase() + javaName.substring(1);
javaName = "get" + capitalizedJavaName;
}
if (hasMetadcl || beanify) {
dcl.type.annotations = dcl.type.annotations.replaceAll("@Name\\(.*\\) ", "");
decl.text += nameAnnotation;
}
dcl.type.annotations = dcl.type.annotations.replace("@ByVal ", "@ByRef ");
final boolean hasSetter = !(dcl.type.constValue && dcl.indirections == 0) && !dcl.constPointer && !dcl.type.constExpr && !context.immutable;
if (!hasSetter) {
if (!hasSetter || beanify) {
decl.text += "@MemberGetter ";
}
decl.text += modifiers + dcl.type.annotations.replace("@ByVal ", "@ByRef ")
+ dcl.type.javaName + " " + javaName + "(" + indices + ");";
decl.text += modifiers + dcl.type.annotations + dcl.type.javaName + " " + javaName + "(" + indices + ");";
if (hasSetter) {
if (indices.length() > 0) {
indices += ", ";
}
String javaTypeWithoutAnnotations = dcl.type.javaName.substring(dcl.type.javaName.lastIndexOf(" ") + 1);
decl.text += " " + modifiers + setterType + javaName + "(" + indices + javaTypeWithoutAnnotations + " setter);";
if (beanify) {
decl.text += "\n" + nameAnnotation + "@MemberSetter " + modifiers + setterType + "set" + capitalizedJavaName + "(" + indices + dcl.type.annotations + dcl.type.javaName + " setter);";
} else {
String javaTypeWithoutAnnotations = dcl.type.javaName.substring(dcl.type.javaName.lastIndexOf(" ") + 1);
decl.text += " " + nameAnnotation + modifiers + setterType + javaName + "(" + indices + javaTypeWithoutAnnotations + " setter);";
}
}
decl.text += "\n";
if ((dcl.type.constValue || dcl.constPointer || dcl.type.constExpr) && dcl.type.staticMember && indices.length() == 0) {
Expand Down Expand Up @@ -3313,6 +3331,8 @@ boolean group(Context context, DeclarationList declList) throws ParserException
ctx.virtualize = true;
if (info.immutable)
ctx.immutable = true;
if (info.beanify)
ctx.beanify = true;
}
ctx.baseType = base.cppName;

Expand Down

0 comments on commit b0fe0c3

Please sign in to comment.