Fun project I am working on

Daestrum

Expert
Licensed User
Longtime User
I have currently got working, the ability to inline java into B4J not like #If JAVA ...#End If, but inside a sub if needed. It's still rough at present, but a few sneak views of what it looks like.


B4J code:
Sub listSort(mylist As List)
    java("_mylist.getObject().sort((a, b) -> ((Comparable)a).compareTo(b))")
    java("System.out.println(@@Class: @@ + _mylist.getClass())")
    java("for (Field f : _mylist.getClass().getDeclaredFields()) System.out.println(@@Declared: @@ + f)")
    java("for (Field f : _mylist.getClass().getFields()) System.out.println(@@Public: @@ + f)")
    java("for (Field f : _mylist.getClass().getSuperclass().getDeclaredFields()) System.out.println(@@Super: @@ + f)")
End Sub
(in the above read @@ as ")

The generated Java:
public String  _listsort(anywheresoftware.b4a.objects.collections.List _mylist) throws Exception{
 //BA.debugLineNum = 13;BA.debugLine="Sub listSort(mylist As List)";
 //BA.debugLineNum = 14;BA.debugLine="java(\"_mylist.getObject().sort((a, b) -> ((Compar";
_mylist.getObject().sort((a, b) -> ((Comparable)a).compareTo(b));
 //BA.debugLineNum = 15;BA.debugLine="java(\"System.out.println("Class: " + _mylist.ge";
System.out.println("Class: " + _mylist.getClass());
 //BA.debugLineNum = 16;BA.debugLine="java(\"for (Field f : _mylist.getClass().getDeclar";
for (Field f : _mylist.getClass().getDeclaredFields()) System.out.println("Declared: " + f);
 //BA.debugLineNum = 17;BA.debugLine="java(\"for (Field f : _mylist.getClass().getFields";
for (Field f : _mylist.getClass().getFields()) System.out.println("Public: " + f);
 //BA.debugLineNum = 18;BA.debugLine="java(\"for (Field f : _mylist.getClass().getSuperc";
for (Field f : _mylist.getClass().getSuperclass().getDeclaredFields()) System.out.println("Super: " + f);
 //BA.debugLineNum = 19;BA.debugLine="End Sub";
return "";
}

Just a fun project as it allows java access to local variables inside subs.

Currently working on release , but doesnt break debug, just ignores the java inserts.
 

Daestrum

Expert
Licensed User
Longtime User
Got two ways for the code now, single lines like in above example, and now got multiline working too (still in release not debug yet)


multiline:
    javam($"Integer a = 10;
for (int b=0; b<a;b++){
    System.out.println(b);
}"$)

creates this java


generated java code:
 //BA.debugLineNum = 24;BA.debugLine="javam($\"Integer a = 10; for (int b=0; b<a;b++){";
Integer a = 10;
for (int b=0; b<a;b++){
    System.out.println(b);
}

Only side effect - b4j error line numbers get a bit skewed from the added java code.
 
Top