Skip to content

Commit

Permalink
Fix checkstyle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
HindujaB committed Oct 4, 2024
1 parent 3539693 commit 8804c11
Showing 1 changed file with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,20 @@ public static Object invokeObjectMethod(BObject bObject, String methodName, Obje
* @param paramValues to be passed to invokable unit
* @return return values
*/
public static Object invokeFunction(ClassLoader classLoader, String className, String methodName, Object... paramValues) {
public static Object invokeFunction(ClassLoader classLoader, String className, String methodName,
Object... paramValues) {
try {
Class<?> clazz = classLoader.loadClass(className);
Method method = getMethod(methodName, clazz);
try {
return method.invoke(null, paramValues);
} catch (IllegalAccessException | InvocationTargetException e) {
throw ErrorCreator.createError(StringUtils.fromString("'" + methodName + "' function invocation failed: " + e.getMessage()));
throw ErrorCreator.createError(StringUtils.fromString("'" + methodName +
"' function invocation failed: " + e.getMessage()));
}
} catch (Exception e) {
throw ErrorCreator.createError(StringUtils.fromString("'" + methodName + "' function invocation failed: " + e.getMessage()));
throw ErrorCreator.createError(StringUtils.fromString("'" + methodName +
"' function invocation failed: " + e.getMessage()));
}
}

Expand All @@ -144,7 +147,8 @@ public static Object invokeFunction(ClassLoader classLoader, String className, S
* @param fieldValues field values
* @return Ballerina object instance
*/
public static Object createObjectValue(String pkgOrg, String pkgName, String pkgVersion, String objectTypeName, Object... fieldValues) {
public static Object createObjectValue(String pkgOrg, String pkgName, String pkgVersion,
String objectTypeName, Object... fieldValues) {
Module packageId = new Module(pkgOrg, pkgName, pkgVersion);
return ValueCreator.createObjectValue(packageId, objectTypeName, fieldValues);
}
Expand Down Expand Up @@ -189,7 +193,9 @@ public static Object createErrorValue(Object message, Object cause, Object... de
}

ErrorType bErrorType = createErrorType(TypeConstants.ERROR, PredefinedTypes.TYPE_ERROR.getPackage());
BMap<BString, Object> errorDetailsMap = ValueCreator.createMapValue((MapType) PredefinedTypes.TYPE_ERROR_DETAIL, errorDetailEntries.toArray(errorDetailEntries.toArray(new BMapInitialValueEntry[0])));
BMap<BString, Object> errorDetailsMap = ValueCreator.createMapValue((MapType)
PredefinedTypes.TYPE_ERROR_DETAIL, errorDetailEntries.toArray(
errorDetailEntries.toArray(new BMapInitialValueEntry[0])));
return ErrorCreator.createError(bErrorType, (StringValue) message, (ErrorValue) cause, errorDetailsMap);
}

Expand All @@ -202,18 +208,23 @@ public static Object createErrorValue(Object message, Object cause, Object... de
*/
public static Object getAnnotationValue(Object typedescValue, String annotationName) {
if (!(typedescValue instanceof TypedescValue)) {
return ErrorCreator.createError(StringUtils.fromString("Incompatible types: expected 'typedesc`, " + "found '" + typedescValue.toString() + "'."));
return ErrorCreator.createError(StringUtils.fromString("Incompatible types: expected 'typedesc`, "
+ "found '" + typedescValue.toString() + "'."));
}
Type type = ((TypedescValue) typedescValue).getDescribingType();
if (type instanceof BAnnotatableType) {
return ((BAnnotatableType) type).getAnnotations().entrySet().stream().filter(annotationEntry -> annotationEntry.getKey().getValue().endsWith(annotationName)).findFirst().map(Map.Entry::getValue).orElse(null);
return ((BAnnotatableType) type).getAnnotations().entrySet().stream().filter(annotationEntry ->
annotationEntry.getKey().getValue().endsWith(annotationName)).findFirst()
.map(Map.Entry::getValue).orElse(null);
}

return ErrorCreator.createError(StringUtils.fromString("type: '" + TypeUtils.getType(type.getEmptyValue()) + "' does not support annotation access."));
return ErrorCreator.createError(StringUtils.fromString("type: '" + TypeUtils.getType(type.getEmptyValue())
+ "' does not support annotation access."));
}

private static Method getMethod(String functionName, Class<?> funcClass) throws NoSuchMethodException {
Method declaredMethod = Arrays.stream(funcClass.getDeclaredMethods()).filter(method -> functionName.equals(method.getName())).findAny().orElse(null);
Method declaredMethod = Arrays.stream(funcClass.getDeclaredMethods()).filter(method ->
functionName.equals(method.getName())).findAny().orElse(null);

if (declaredMethod != null) {
return declaredMethod;
Expand Down Expand Up @@ -307,7 +318,8 @@ private static BString[] processXMLNamePattern(String xmlNamePattern) {
xmlNamePattern = stepParts[stepParts.length - 1];
}

return Arrays.stream(xmlNamePattern.split(XML_NAME_PATTERN_SEPARATOR)).map(entry -> StringUtils.fromString(entry.trim())).toArray(BString[]::new);
return Arrays.stream(xmlNamePattern.split(XML_NAME_PATTERN_SEPARATOR)).map(entry ->
StringUtils.fromString(entry.trim())).toArray(BString[]::new);
}

/**
Expand All @@ -319,10 +331,12 @@ private static BString[] processXMLNamePattern(String xmlNamePattern) {
* @param userArgs argument values
* @return result of the function invocation
*/
public static Object classloadAndInvokeFunction(String executablePath, String mainClass, String functionName, Object... userArgs) {
public static Object classloadAndInvokeFunction(String executablePath, String mainClass, String functionName,
Object... userArgs) {
try {
URL pathUrl = Paths.get(executablePath).toUri().toURL();
URLClassLoader classLoader = AccessController.doPrivileged((PrivilegedAction<URLClassLoader>) () -> new URLClassLoader(new URL[]{pathUrl}, ClassLoader.getSystemClassLoader()));
URLClassLoader classLoader = AccessController.doPrivileged((PrivilegedAction<URLClassLoader>) () ->
new URLClassLoader(new URL[]{pathUrl}, ClassLoader.getSystemClassLoader()));

// Derives the namespace of the generated classes.
String[] mainClassNameParts = mainClass.split("\\.");
Expand All @@ -336,7 +350,8 @@ public static Object classloadAndInvokeFunction(String executablePath, String ma
}
}

private static Object invokeBalRuntimeMethod(String functionName, Module module, ClassLoader classLoader, Object[] paramValues) {
private static Object invokeBalRuntimeMethod(String functionName, Module module, ClassLoader classLoader,
Object[] paramValues) {
BalRuntime runtime = new ClassloaderRuntime(module, classLoader);
Object result;
try {
Expand All @@ -347,7 +362,8 @@ private static Object invokeBalRuntimeMethod(String functionName, Module module,
// Then call run method
result = runtime.call(module, functionName, paramValues);
} catch (Throwable throwable) {
throw ErrorCreator.createError(StringUtils.fromString("'" + functionName + "' function " + "invocation failed : " + throwable.getMessage()));
throw ErrorCreator.createError(StringUtils.fromString("'" + functionName + "' function " +
"invocation failed : " + throwable.getMessage()));
} finally {
try {
runtime.stop();
Expand All @@ -370,7 +386,8 @@ private static Object invokeBalRuntimeMethod(String functionName, Module module,
* @param args Arguments to provide.
* @return The result of the invocation.
*/
protected static Object invokeMethodDirectly(ClassLoader classLoader, String className, String methodName, Class<?>[] argTypes, Object[] args) throws Exception {
protected static Object invokeMethodDirectly(ClassLoader classLoader, String className, String methodName,
Class<?>[] argTypes, Object[] args) throws Exception {
Class<?> clazz = classLoader.loadClass(className);
Method method = clazz.getDeclaredMethod(methodName, argTypes);
return method.invoke(null, args);
Expand Down

0 comments on commit 8804c11

Please sign in to comment.