Skip to content

Commit

Permalink
Java issues - OSAC-42, OSAC-43, OSAC-45 (#76)
Browse files Browse the repository at this point in the history
* Fix for issue #51

* Update on fix for Issue 51

* Added tests for Java Invoker timeout

* Fix for issue #31 on empty array

* fix for #31 for null parameters

* fix issue with comparison of parameter types

* implemented typed null solution for #31

* small fix for array indexing #31

* TestCases for issues #30 and #31

* Unit Tests of Generic Methods for issues #30 and #31

* merged

* TestProgram.jar refactor

* Removed unused

* updated

* Changes

* changes

* done

* Suppressions

* Bug fixes

* Fixed comments

* recompiled JAR file

Co-authored-by: alcxs <[email protected]>
Co-authored-by: petcua1 <[email protected]>
Co-authored-by: Vlad Nicula <[email protected]>
  • Loading branch information
4 people authored Dec 8, 2020
1 parent 6eabf9e commit 886cf69
Show file tree
Hide file tree
Showing 38 changed files with 707 additions and 793 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
################################################################################

/.vs
/Activities/Java/Invoke/out
/Activities/Java/Invoke/.idea
/Activities/Java/Invoke/.idea/artifacts
/Activities/Java/TestProgram/.idea
/Activities/Java/TestProgram/out
821 changes: 63 additions & 758 deletions Activities/Java/Invoke/.idea/workspace.xml

Large diffs are not rendered by default.

Binary file modified Activities/Java/Invoke/artifacts/InvokeJava.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,25 @@ public class ArrayTypeSerializer implements TypeSerializerInterface {

public JavaObject DeserializeToJavaObject(JSONObject obj) {
JSONArray array = null;
String runtimeArrayType = null;
try {
array = obj.getJSONArray("value");
runtimeArrayType = obj.getString("runtime_arrayType");
}
catch (JSONException e) { }

if (array == null) {
return new JavaObject(new EmptyClass(), boolean.class);
try {
runtimeArrayType = obj.getString("runtime_arrayType");
}
catch (JSONException e) { }
return EmptyTypeSerializer.DeserializeToNullJavaObject(runtimeArrayType);
}
Class<?> arrayType = getArrayType(context, array);
int length = array.length();
if (length == 0){
return EmptyTypeSerializer.DeserializeToJavaObject(runtimeArrayType);
}
Object result = Array.newInstance(arrayType, length);

for (int i = 0; i < length; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public JavaObject DeserializeToJavaObject(JSONObject obj) {
return new JavaObject (value, boolean.class);
} catch (JSONException e) { }
}
return new JavaObject(new EmptyClass(), boolean.class);
return new JavaObject(null, boolean.class);
}

public JSONObject SerializeToDotNet(ObjectInstance obj) throws JSONException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public JavaObject DeserializeToJavaObject(JSONObject obj) {
return new JavaObject(value, byte.class);
} catch (JSONException e) { }
}
return new JavaObject(new EmptyClass(), EmptyClass.class);
return new JavaObject(null, byte.class);
}

public JSONObject SerializeToDotNet(ObjectInstance obj) throws JSONException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public JavaObject DeserializeToJavaObject(JSONObject obj) {
return new JavaObject(value, char.class);
} catch (JSONException e) { }
}
return new JavaObject(new EmptyClass(), EmptyClass.class);
return new JavaObject(null, char.class);
}

public JSONObject SerializeToDotNet(ObjectInstance obj) throws JSONException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public JavaObject DeserializeToJavaObject(JSONObject obj) {
return new JavaObject(value, double.class);
} catch (JSONException e) { }
}
return new JavaObject(new EmptyClass(), EmptyClass.class);
return new JavaObject(null, double.class);
}

public JSONObject SerializeToDotNet(ObjectInstance obj) throws JSONException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,59 @@
import com.uipath.instance.ObjectInstance;
import org.json.JSONObject;

import java.lang.reflect.Array;

public class EmptyTypeSerializer implements TypeSerializerInterface {

public JavaObject DeserializeToJavaObject(JSONObject obj) {
return new JavaObject(new EmptyClass(), EmptyClass.class);
}

public static JavaObject DeserializeToJavaObject(String type){
if ("System.Int32[]".equals(type)) {
return new JavaObject(new int[0], Array.newInstance(int.class, 0).getClass());
} else if ("System.Boolean[]".equals(type)) {
return new JavaObject(new boolean[0], Array.newInstance(boolean.class, 0).getClass());
} else if ("System.Byte[]".equals(type)) {
return new JavaObject(new byte[0], Array.newInstance(byte.class, 0).getClass());
} else if ("System.Char[]".equals(type)) {
return new JavaObject(new char[0], Array.newInstance(char.class, 0).getClass());
} else if ("System.Double[]".equals(type)) {
return new JavaObject(new double[0], Array.newInstance(double.class, 0).getClass());
} else if ("System.Single[]".equals(type)) {
return new JavaObject(new float[0], Array.newInstance(float.class, 0).getClass());
} else if ("System.Int64[]".equals(type)) {
return new JavaObject(new long[0], Array.newInstance(long.class, 0).getClass());
} else if ("System.String[]".equals(type)) {
return new JavaObject(new String[0], Array.newInstance(String.class, 0).getClass());
} else if ("System.Int16[]".equals(type)) {
return new JavaObject(new short[0], Array.newInstance(short.class, 0).getClass());
}
return new JavaObject(new Object[0], Array.newInstance(Object.class, 0).getClass());
}
public static JavaObject DeserializeToNullJavaObject(String type){
if ("System.Int32[]".equals(type)) {
return new JavaObject(null, Array.newInstance(int.class, 0).getClass());
} else if ("System.Boolean[]".equals(type)) {
return new JavaObject(null, Array.newInstance(boolean.class, 0).getClass());
} else if ("System.Byte[]".equals(type)) {
return new JavaObject(null, Array.newInstance(byte.class, 0).getClass());
} else if ("System.Char[]".equals(type)) {
return new JavaObject(null, Array.newInstance(char.class, 0).getClass());
} else if ("System.Double[]".equals(type)) {
return new JavaObject(null, Array.newInstance(double.class, 0).getClass());
} else if ("System.Single[]".equals(type)) {
return new JavaObject(null, Array.newInstance(float.class, 0).getClass());
} else if ("System.Int64[]".equals(type)) {
return new JavaObject(null, Array.newInstance(long.class, 0).getClass());
} else if ("System.String[]".equals(type)) {
return new JavaObject(null, Array.newInstance(String.class, 0).getClass());
} else if ("System.Int16[]".equals(type)) {
return new JavaObject(null, Array.newInstance(short.class, 0).getClass());
}
return new JavaObject(new Object[0], Array.newInstance(Object.class, 0).getClass());
}

public JSONObject SerializeToDotNet(ObjectInstance obj) {
return new JSONObject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public JavaObject DeserializeToJavaObject(JSONObject obj) {
return new JavaObject(value, float.class);
} catch (JSONException e) { }
}
return new JavaObject(new EmptyClass(), EmptyClass.class);
return new JavaObject(null, float.class);
}

public JSONObject SerializeToDotNet(ObjectInstance obj) throws JSONException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public JavaObject DeserializeToJavaObject(JSONObject obj) {
return new JavaObject(value, int.class);
} catch (JSONException e) { }
}
return new JavaObject(new EmptyClass(), EmptyClass.class);
return new JavaObject(null, int.class);
}

public JSONObject SerializeToDotNet(ObjectInstance obj) throws JSONException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public JavaObject DeserializeToJavaObject(JSONObject obj) {
return new JavaObject(value, long.class);
} catch (JSONException e) { }
}
return new JavaObject(new EmptyClass(), EmptyClass.class);
return new JavaObject(null, long.class);
}

public JSONObject SerializeToDotNet(ObjectInstance obj) throws JSONException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public JavaObject DeserializeToJavaObject(JSONObject obj) {
return new JavaObject(value, short.class);
} catch (JSONException e) { }
}
return new JavaObject(new EmptyClass(), EmptyClass.class);
return new JavaObject(null, short.class);
}

public JSONObject SerializeToDotNet(ObjectInstance obj) throws JSONException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public JavaObject DeserializeToJavaObject(JSONObject obj) {
return new JavaObject(value, String.class);
} catch (JSONException e) { }
}
return new JavaObject(new EmptyClass(), EmptyClass.class);
return new JavaObject(null, String.class);
}

public JSONObject SerializeToDotNet(ObjectInstance obj) throws JSONException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ private static Method getMethod(String methodName, Class<?> loadedClass, Class<?

private static Constructor<?> getConstructor(Class<?> loadedClass, Class<?>[] parameterTypes) throws NoSuchMethodException {
Constructor<?>[] constructors = loadedClass.getConstructors();

if (constructors == null) {
throw new NoSuchMethodException();
}
Expand All @@ -235,6 +236,7 @@ private static Constructor<?> getConstructor(Class<?> loadedClass, Class<?>[] pa
return constructor;
}
}

throw new NoSuchMethodException();
}

Expand Down Expand Up @@ -322,7 +324,7 @@ public boolean equals(Object obj) {
}
for (int i = 0; i < parameterTypes.length; ++i) {
if (parameterTypes[i] == null || that.parameterTypes[i] == null) {
return false;
continue;
}
Class<?> thisType = getWrappedType(parameterTypes[i]);
Class<?> thatType = getWrappedType(that.parameterTypes[i]);
Expand Down
Binary file modified Activities/Java/TestProgram/artifacts/TestProgram.jar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package uipath.java.test;

public class GenericMethods {
private String message;

public GenericMethods() {}

public GenericMethods(String sParam) {
this.message = sParam;
}

public String getMessage() {
return this.message;
}

public String[] GenericMethods(String[] strArrParam) {
return strArrParam;
}

public String GenericMethods(String s1, String s2) {
return s1 + s2;
}

public int[] IntArr(int[] intArrParam) {
return intArrParam;
}

public static String[] StringArrStatic(String[] args) {
return args;
}

public static int IntType(int args) {
return args;
}

public static boolean BoolType(boolean args) {
return args;
}

public static float FloatType(float args) {
return args;
}

public String[] StringArrNonStatic(String[] args) {
return args;
}

public <T> T GenericsExtObj(Object a) {
this.message = "Generic method with Object " + a;
return (T)this.message;
}

public <T extends String> Object GenericsExtString(T a) {
this.message = "Generic method " + a;
return this.message;
}

public <T> T GenericsR(T a) {
this.message = "Generic method with return " + a;
return (T)this.message;
}

public void finalize() throws Throwable {}

public String StringParamValidation(String X) {
return X;
}

public String ConcatenateXYZ() {
String X = "X";
String Y = "Y";
String Z = "Z";
return StringParamValidation(X) + StringParamValidation(Y) + StringParamValidation(Z);
}

public int RecursiveCallTest(int k) {
if (k < 2)
return 1;
return k * RecursiveCallTest(k - 1);
}

public static int StaticRecursiveCallTest(int k) {
if (k < 2)
return 1;
return k * StaticRecursiveCallTest(k - 1);
}
}
4 changes: 2 additions & 2 deletions Activities/Java/UiPath.Java.Activities/CreateJavaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ protected async override Task<Action<AsyncCodeActivityContext>> ExecuteAsync(Asy
throw new ArgumentNullException(nameof(TargetType));
}
List<object> parameters = GetParameters(context);

var types = GetParameterTypes(context, parameters);
JavaObject instance = null;
try
{
instance = await invoker.InvokeConstructor(className, parameters, parameters.Select(param => param?.GetType()).ToList(), cancellationToken);
instance = await invoker.InvokeConstructor(className, parameters, types, cancellationToken);
}
catch (Exception e)
{
Expand Down
4 changes: 2 additions & 2 deletions Activities/Java/UiPath.Java.Activities/InvokeJavaMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ protected async override Task<Action<AsyncCodeActivityContext>> ExecuteAsync(Asy
}

List<object> parameters = GetParameters(context);
var types = GetParameterTypes(context, parameters);
JavaObject instance = null;

try
{
instance = await invoker.InvokeMethod(methodName, className, javaObject, parameters,
parameters.Select(param => param?.GetType()).ToList(), cancellationToken);
instance = await invoker.InvokeMethod(methodName, className, javaObject, parameters, types, cancellationToken);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ protected List<object> GetParameters(AsyncCodeActivityContext context)
List<object> parameters = ParametersList.Get(context) ?? Parameters.Select(arg => arg.Get(context)).ToList();
return parameters;
}

protected List<Type> GetParameterTypes(AsyncCodeActivityContext context, List<object> parameters)
{
List<Type> parameterTypes = new List<Type>();
for(int index=0;index<Parameters.Count;index++)
{
if(null != parameters[index])
parameterTypes.Add(parameters[index].GetType());
else
parameterTypes.Add(Parameters[index]?.ArgumentType ?? typeof(object));
}
return parameterTypes;
}
}
}
14 changes: 13 additions & 1 deletion Activities/Java/UiPath.Java.Activities/JavaScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public class JavaScope : AsyncTaskNativeActivity
[LocalizedDescription(nameof(Resources.JavaPathDescription))]
public InArgument<string> JavaPath { get; set; }

[LocalizedCategory(nameof(Resources.Input))]
[LocalizedDisplayName(nameof(Resources.TimeoutMSDisplayName))]
[LocalizedDescription(nameof(Resources.TimeoutMSDescription))]
[DefaultValue(15000)]
public InArgument<int> TimeoutMS { get; set; }

[Browsable(false)]
public ActivityAction<object> Body { get; set; }

Expand Down Expand Up @@ -62,9 +68,15 @@ protected override async Task<Action<NativeActivityContext>> ExecuteAsync(Native
}
_invoker = new JavaInvoker(javaPath);

int initTimeout = TimeoutMS.Get(context);
if (initTimeout < 0)
{
throw new ArgumentException(UiPath.Java.Activities.Properties.Resources.TimeoutMSException, "TimeoutMS");
}

try
{
await _invoker.StartJavaService();
await _invoker.StartJavaService(initTimeout);
}
catch (Exception e)
{
Expand Down
Loading

0 comments on commit 886cf69

Please sign in to comment.