Skip to content

Commit

Permalink
Code gen improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Aug 14, 2024
1 parent b6efc20 commit bad42a3
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 5 deletions.
1 change: 1 addition & 0 deletions example/lib/lib-build/src/main/cpp/custom/CustomCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "DefaultInterface.h"
#include "subpackage/ParamData.h"
#include "DefaultParamsClass.h"
#include "TestLib.h"

typedef NormalClass::EnumWithinClass EnumWithinClass;
typedef NormalClass::EnumClassWithinClass EnumClassWithinClass;
Expand Down
38 changes: 38 additions & 0 deletions example/lib/lib-build/src/main/cpp/exampleLib.idl
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
interface TestObjectClass {
void TestObjectClass();
attribute long intValue;
attribute float floatValue;
};

interface TestAttributeArrayClass {
void TestAttributeArrayClass();
[BoundsChecked] attribute long[] intArray;
attribute float[] floatArray;
attribute byte[] byteArray;
attribute boolean[] boolArray;
[Value] attribute TestObjectClass[] valueObjectArray;
attribute TestObjectClass[] pointerObjectArray;
};

interface TestAttributeClass {
void TestAttributeClass();
readonly attribute long readOnlyIntValue;
attribute long intValue;
attribute float floatValue;
attribute double doubleValue;
attribute boolean boolValue;
attribute any voidPointer;
[Value] attribute TestObjectClass valueObject;
attribute TestObjectClass pointerObject;
attribute TestObjectClass nullPointerObject;
};

interface TestConstructorClass {
void TestConstructorClass(long intParam);
void TestConstructorClass(float floatParam, boolean boolParam);
void TestConstructorClass(long intParam, long intParam2, float floatParam, float floatParam2, optional boolean boolParam);
void TestConstructorClass(long intParam, long[] intArray, float[] floatArray);
};



interface ParentClass {
float addFloatValue(float a, float b);
boolean invertBoolean(boolean value);
Expand Down
76 changes: 76 additions & 0 deletions example/lib/lib-build/src/main/cpp/source/exampleLib/src/TestLib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

using namespace std;

class TestObjectClass {
private:

public:
int intValue;
float floatValue;
};

class TestAttributeArrayClass {
private:

public:
int intArray[3];
float floatArray[3];
char byteArray[3];
bool boolArray[3];
TestObjectClass valueObjectArray[3];
TestObjectClass* pointerObjectArray[3];
};

class TestAttributeClass {
private:

public:
const int readOnlyIntValue;
int intValue;
float floatValue;
double doubleValue;
bool boolValue;
void* voidPointer;
TestObjectClass valueObject;
TestObjectClass* pointerObject;
TestObjectClass* nullPointerObject;

TestAttributeClass(): readOnlyIntValue(7) {
pointerObject = new TestObjectClass();
nullPointerObject = NULL;
};
~TestAttributeClass() {
delete pointerObject;
};
};

class TestConstructorClass {
private:

public:
int intParam;
float floatParam;
bool boolParam;

TestConstructorClass(int intParam) {
this->intParam = intParam;
};
TestConstructorClass(float floatParam, bool boolParam) {
this->floatParam = floatParam;
this->boolParam = boolParam;
};
TestConstructorClass(int intParam, int intParam2, float floatParam, float floatParam2, bool boolParam = true) {
this->intParam = intParam;
this->floatParam = floatParam;
this->boolParam = boolParam;
};
TestConstructorClass(int intParam, int* intArray, float* floatArray) {
this->intParam = intParam;
};
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.github.xpenatan.jparser.core;

import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.comments.BlockComment;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.utils.PositionUtils;
import com.github.xpenatan.jparser.core.codeparser.CodeParser;
import com.github.xpenatan.jparser.core.codeparser.CodeParserItem;
Expand Down Expand Up @@ -87,6 +92,13 @@ public static void generate(CodeParser wrapper, String sourceDir, String genDir,
}
System.out.println("***** GENERATING CODE *****");
JParser jParser = new JParser(sourceD, genD);

CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver());
// Configure JavaParser to use type resolution
JavaSymbolSolver symbolSolver = new JavaSymbolSolver(combinedTypeSolver);
StaticJavaParser.getParserConfiguration().setSymbolResolver(symbolSolver);

processDirectory(jParser, fileSourceDir, fileGenDir, excludes, fileSourceDir);
wrapper.onParseStart(jParser);
parseJavaFiles(jParser, wrapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,21 @@ private static String getParam(IDLFile idlFile, Type type, String paramName, Str
else if(isAny) {
paramName = "( void* )" + paramName;
}
else {
// basic param cast jni java primitive to c++ primitive.
if(classType.equals("int")) {
paramName = "(int)" + paramName;
}
else if(classType.equals("float")) {
paramName = "(float)" + paramName;
}
else if(classType.equals("double")) {
paramName = "(double)" + paramName;
}
else if(classType.equals("boolean")) {
paramName = "(bool)" + paramName;
}
}

IDLEnum anEnum = idlFile.getEnum(classType);
if(anEnum != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.github.xpenatan.jparser.core.util.CustomFileDescriptor;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.xpenatan.jparser.idl;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type;
import java.io.ByteArrayInputStream;

public class IDLHelper {
public static String removeMultipleSpaces(String in) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ private CompilationUnit setupClass(IDLClassOrEnum idlClass, String subPackage) {
IDLMethodParser.generateFieldName("T_02", classDeclaration, className, true, Modifier.Keyword.PUBLIC, true);
IDLMethodParser.generateFieldName("T_03", classDeclaration, className, true, Modifier.Keyword.PUBLIC, true);
}

return compilationUnit;
// Hack to inject internal dependencies
return StaticJavaParser.parse(compilationUnit.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import com.github.javaparser.ast.type.ArrayType;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.types.ResolvedArrayType;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.xpenatan.jparser.core.JParser;
import com.github.xpenatan.jparser.core.JParserHelper;
import com.github.xpenatan.jparser.core.JParserItem;
Expand Down Expand Up @@ -679,6 +683,7 @@ private void convertNativeMethodLongType(ClassOrInterfaceDeclaration classDeclar
for(int i = 0; i < constructorDeclarations.size(); i++) {
ConstructorDeclaration constructorDeclaration = constructorDeclarations.get(i);
List<MethodCallExpr> methodCallerExprList = constructorDeclaration.findAll(MethodCallExpr.class);
NodeList<Parameter> constructorParameters = constructorDeclaration.getParameters();
updateLongToInt(classDeclaration, methodCallerExprList);
}

Expand Down Expand Up @@ -739,10 +744,45 @@ private void updateLongToInt(ClassOrInterfaceDeclaration classDeclaration, List<
}
}

public MethodDeclaration getNativeMethod(ClassOrInterfaceDeclaration classDeclaration, MethodCallExpr methodCallExpr) {
private MethodDeclaration getNativeMethod(ClassOrInterfaceDeclaration classDeclaration, MethodCallExpr methodCallExpr) {
String nativeMethodName = methodCallExpr.getNameAsString();
NodeList<Expression> arguments = methodCallExpr.getArguments();
List<MethodDeclaration> methodsByName = getNativeMethodsByName(classDeclaration, nativeMethodName, arguments.size(), null);
ArrayList<String> paramTypes = new ArrayList<>();
if(arguments.size() > 0) {
for(int i = 0; i < arguments.size(); i++) {
Expression expression = arguments.get(i);
ResolvedType resolvedType = null;
try {
resolvedType = expression.calculateResolvedType();
}
catch(Throwable t) {

if(t instanceof UnsolvedSymbolException) {
UnsolvedSymbolException unE = (UnsolvedSymbolException)t;
String name = unE.getName();
paramTypes.add(name);
}
continue;
}
String type = null;
if(resolvedType.isPrimitive()) {
type = resolvedType.asPrimitive().describe();
}
else if(resolvedType.isReferenceType()) {
ResolvedReferenceType referenceType1 = resolvedType.asReferenceType();
String[] split = referenceType1.describe().split("\\.");
type = split[split.length-1];
}
else if(resolvedType.isArray()) {
ResolvedArrayType arrayType = resolvedType.asArrayType();
type = arrayType.describe();
}
paramTypes.add(type);
}
}
String[] paramT = new String[paramTypes.size()];
paramTypes.toArray(paramT);
List<MethodDeclaration> methodsByName = getNativeMethodsByName(classDeclaration, nativeMethodName, arguments.size(), paramT);
int size = methodsByName.size();
if(size == 0) {
return null;
Expand Down

0 comments on commit bad42a3

Please sign in to comment.