Skip to content

Commit

Permalink
Generate attribute method
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jul 23, 2023
1 parent f34b1bb commit 98d538a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
3 changes: 1 addition & 2 deletions example/example-build/jni/cpp/src/NormalClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

class NormalClass : public ParentClass
{
public:
int hiddenInt = 1;

ReturnClass valueReturnClass;
ReturnClass * pointerReturnClass;
ReturnClass * nullPointerReturnClass;

public:
NormalClass();
NormalClass(int c, ParamClass & refParamClass);
NormalClass(ParamClass * pointerParamClass, ParamClass & refParamClass, ParamClass valueParamClass);
Expand Down
4 changes: 4 additions & 0 deletions example/example-build/src/main/resources/idl/Test.idl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ interface ParentClass {
};

interface NormalClass {
// attribute long hiddenInt;
// attribute ReturnClass pointerReturnClass;
// [Value]attribute ReturnClass valueReturnClass;

void NormalClass();
void NormalClass(long c, [Ref]ParamClass refParamClass);
void NormalClass(ParamClass pointerParamClass, [Ref]ParamClass refParamClass, [Value]ParamClass valueParamClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public void test_invert_boolean_should_be_true() {
boolean ret = normalClass.invertBoolean(false);
assertTrue(ret);
}
//
// @Test
// public void test_attribute() {
// NormalClass normalClass = new NormalClass();
// normalClass.hiddenInt(10);
// int retValue = normalClass.hiddenInt();
// assertEquals(10, retValue);
// }

}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.comments.BlockComment;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.type.Type;
import com.github.xpenatan.jparser.core.JParser;
import com.github.xpenatan.jparser.core.JParserHelper;
Expand All @@ -16,6 +19,7 @@
import com.github.xpenatan.jparser.core.codeparser.DefaultCodeParser;
import com.github.xpenatan.jparser.idl.IDLAttribute;
import com.github.xpenatan.jparser.idl.IDLClass;
import com.github.xpenatan.jparser.idl.IDLMethod;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -94,6 +98,9 @@ public static void generateAttribute(IDLDefaultCodeParser idlParser, JParser jPa
if(!idlParser.generateClass) {
idlParser.onIDLMethodGenerated(jParser, idlClass, null, unit, classOrInterfaceDeclaration, getMethodDeclaration, true);
}
else {
setupAttributeMethod(idlParser, jParser, idlAttribute, classOrInterfaceDeclaration, getMethodDeclaration);
}
}
if(addSet) {
if(setMethodDeclaration != null) {
Expand All @@ -108,6 +115,16 @@ public static void generateAttribute(IDLDefaultCodeParser idlParser, JParser jPa
if(!idlParser.generateClass) {
idlParser.onIDLMethodGenerated(jParser, idlClass, null, unit, classOrInterfaceDeclaration, setMethodDeclaration, true);
}
else {
setupAttributeMethod(idlParser, jParser, idlAttribute, classOrInterfaceDeclaration, setMethodDeclaration);
}
}
}

private static void setupAttributeMethod(IDLDefaultCodeParser idlParser, JParser jParser, IDLAttribute idlAttribute, ClassOrInterfaceDeclaration classDeclaration, MethodDeclaration methodDeclaration) {
MethodDeclaration nativeMethodDeclaration = IDLMethodParser.prepareNativeMethod(idlAttribute.isStatic, idlAttribute.isValue, classDeclaration, methodDeclaration);
if(nativeMethodDeclaration != null) {
idlParser.onIDLAttributeGenerated(jParser, idlAttribute, classDeclaration, methodDeclaration, nativeMethodDeclaration);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ public void onIDLMethodGenerated(JParser jParser, IDLClass idlClass, IDLMethod i
public void onIDLMethodGenerated(JParser jParser, IDLMethod idlMethod, ClassOrInterfaceDeclaration classDeclaration, MethodDeclaration methodDeclaration, MethodDeclaration nativeMethodDeclaration) {
}

public void onIDLAttributeGenerated(JParser jParser, IDLAttribute idlAttribute, ClassOrInterfaceDeclaration classDeclaration, MethodDeclaration methodDeclaration, MethodDeclaration nativeMethodDeclaration) {
}

/**
* true to accept the idl method
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ public static void generateMethods(IDLDefaultCodeParser idlParser, JParser jPars
}

private static void setupMethod(IDLDefaultCodeParser idlParser, JParser jParser, IDLMethod idlMethod, ClassOrInterfaceDeclaration classDeclaration, MethodDeclaration methodDeclaration) {
MethodDeclaration nativeMethodDeclaration = generateNativeMethod(idlMethod, classDeclaration, methodDeclaration);
MethodDeclaration nativeMethodDeclaration = IDLMethodParser.prepareNativeMethod(idlMethod.isStaticMethod, idlMethod.isReturnValue, classDeclaration, methodDeclaration);
if(nativeMethodDeclaration != null) {
idlParser.onIDLMethodGenerated(jParser, idlMethod, classDeclaration, methodDeclaration, nativeMethodDeclaration);
}
}

public static MethodDeclaration prepareNativeMethod(boolean isStatic, boolean isReturnValue, ClassOrInterfaceDeclaration classDeclaration, MethodDeclaration methodDeclaration) {
MethodDeclaration nativeMethodDeclaration = generateNativeMethod(isReturnValue, classDeclaration, methodDeclaration);
if(!JParserHelper.containsMethod(classDeclaration, nativeMethodDeclaration)) {
//Add native method if it does not exist
classDeclaration.getMembers().add(nativeMethodDeclaration);
Expand All @@ -142,36 +149,35 @@ private static void setupMethod(IDLDefaultCodeParser idlParser, JParser jParser,

if(methodReturnType.isVoidType()) {
// void types just call the method.
setupCallerParam(idlMethod, caller, methodDeclaration, null);
IDLMethodParser.setupCallerParam(isStatic, false, caller, methodDeclaration, null);
BlockStmt blockStmt = methodDeclaration.getBody().get();
blockStmt.addStatement(caller);
}
else if(methodReturnType.isClassOrInterfaceType()) {
// Class object needs to generate some additional code.
// Needs to obtain the pointer and return a temp object.
BlockStmt blockStmt = generateTempObjects(idlMethod, classDeclaration, methodDeclaration, nativeMethodDeclaration, caller);
BlockStmt blockStmt = IDLMethodParser.generateTempObjects(isReturnValue, classDeclaration, methodDeclaration, nativeMethodDeclaration, caller);
methodDeclaration.setBody(blockStmt);
}
else {
// Should be a primitive return type.
ReturnStmt returnStmt = getReturnStmt(methodDeclaration);
setupCallerParam(idlMethod, caller, methodDeclaration, null);
ReturnStmt returnStmt = IDLMethodParser.getReturnStmt(methodDeclaration);
IDLMethodParser.setupCallerParam(isStatic, false, caller, methodDeclaration, null);
returnStmt.setExpression(caller);
}
idlParser.onIDLMethodGenerated(jParser, idlMethod, classDeclaration, methodDeclaration, nativeMethodDeclaration);
return nativeMethodDeclaration;
}
return null;
}

private static MethodCallExpr createCaller(MethodDeclaration nativeMethodDeclaration) {
public static MethodCallExpr createCaller(MethodDeclaration nativeMethodDeclaration) {
String nativeMethodName = nativeMethodDeclaration.getNameAsString();
MethodCallExpr caller = new MethodCallExpr();
caller.setName(nativeMethodName);
return caller;
}

private static void setupCallerParam(IDLMethod idlMethod, MethodCallExpr caller, MethodDeclaration methodDeclaration, String tempFieldName) {
boolean isStatic = idlMethod.isStaticMethod;
boolean isReturnValue = idlMethod.isReturnValue;
public static void setupCallerParam(boolean isStatic, boolean isReturnValue, MethodCallExpr caller, MethodDeclaration methodDeclaration, String tempFieldName) {
NodeList<Parameter> methodParameters = methodDeclaration.getParameters();

if(isReturnValue && tempFieldName != null) {
Expand All @@ -197,18 +203,17 @@ else if(type.isArrayType()) {
}
}

private static BlockStmt generateTempObjects(IDLMethod idlMethod, ClassOrInterfaceDeclaration classDeclaration, MethodDeclaration methodDeclaration, MethodDeclaration nativeMethodDeclaration, MethodCallExpr caller) {
public static BlockStmt generateTempObjects(boolean isReturnValue, ClassOrInterfaceDeclaration classDeclaration, MethodDeclaration methodDeclaration, MethodDeclaration nativeMethodDeclaration, MethodCallExpr caller) {
Type methodReturnType = methodDeclaration.getType();
String returnTypeName = methodReturnType.toString();
String newBody = null;

boolean isReturnValue = idlMethod.isReturnValue;
boolean isStatic = methodDeclaration.isStatic();
boolean isTemp = !isReturnValue;

String fieldName = generateFieldName(classDeclaration, returnTypeName, isTemp, isStatic);

setupCallerParam(idlMethod, caller, methodDeclaration, fieldName);
IDLMethodParser.setupCallerParam(isStatic, isReturnValue, caller, methodDeclaration, fieldName);

String methodCaller = caller.toString();

Expand Down Expand Up @@ -290,12 +295,11 @@ private static String getFieldName(String type, int number, boolean isTemp, boo
}
}

private static MethodDeclaration generateNativeMethod(IDLMethod idlMethod, ClassOrInterfaceDeclaration classDeclaration, MethodDeclaration methodDeclaration) {
public static MethodDeclaration generateNativeMethod(boolean isReturnValue, ClassOrInterfaceDeclaration classDeclaration, MethodDeclaration methodDeclaration) {
String methodName = methodDeclaration.getNameAsString();
NodeList<Parameter> methodParameters = methodDeclaration.getParameters();
Type methodReturnType = methodDeclaration.getType();
boolean isStatic = methodDeclaration.isStatic();
boolean isReturnValue = idlMethod.isReturnValue;
boolean isClassOrInterfaceType = methodReturnType.isClassOrInterfaceType();

// Clone some generated idl method settings
Expand Down Expand Up @@ -346,7 +350,7 @@ private static MethodDeclaration generateNativeMethod(IDLMethod idlMethod, Class
return nativeMethod;
}

private static ReturnStmt getReturnStmt(MethodDeclaration idlMethodDeclaration) {
public static ReturnStmt getReturnStmt(MethodDeclaration idlMethodDeclaration) {
BlockStmt blockStmt = idlMethodDeclaration.getBody().get();
NodeList<Statement> statements = blockStmt.getStatements();
if(statements.size() > 0) {
Expand Down

0 comments on commit 98d538a

Please sign in to comment.