Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Info.beanify() to add JavaBeans-style prefixes for getter and setters of member variables generated by Parser #495

Merged
merged 2 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

* Add `Info.beanify` to have `Parser` generate JavaBeans-style getters and setters ([pull #495](https://github.com/bytedeco/javacpp/pull/495))
* Allow `Parser` to use `Info.javaNames` for function names containing parameters as well ([issue #492](https://github.com/bytedeco/javacpp/issues/492))
* Fix `Parser` producing incorrect calls to function templates with non-type parameters ([issue #491](https://github.com/bytedeco/javacpp/issues/491))
* Add missing `presets/package-info.java` required for OSGi and add profile for M2Eclipse ([pull #490](https://github.com/bytedeco/javacpp/pull/490))
Expand Down
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 @@ -45,6 +45,7 @@ class Context {
constBaseName = c.constBaseName;
immutable = c.immutable;
inaccessible = c.inaccessible;
beanify = c.beanify;
objectify = c.objectify;
virtualize = c.virtualize;
variable = c.variable;
Expand All @@ -62,6 +63,7 @@ class Context {
String constBaseName = null;
boolean immutable = false;
boolean inaccessible = false;
boolean beanify = false;
boolean objectify = false;
boolean virtualize = false;
Declarator variable = null;
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
34 changes: 27 additions & 7 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 + "\") ";
}
String nameAnnotation = "";
if (metadcl != null && metadcl.cppName != null && metadcl.cppName.length() > 0) {
decl.text += metadcl.indices == 0
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 (nameAnnotation.length() == 0) {
nameAnnotation = "@Name(\"" + shortName + "\") ";
}
capitalizedJavaName = javaName.substring(0, 1).toUpperCase() + javaName.substring(1);
javaName = "get" + capitalizedJavaName;
}
if (nameAnnotation.length() > 0) {
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 += " " + 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