use of angularBeans.api.NGPostConstruct in project AngularBeans by bessemHmidi.
the class InvocationHandler method genericInvoke.
private void genericInvoke(Object service, String methodName, JsonObject params, Map<String, Object> returns, long reqID, String UID, HttpServletRequest request) throws SecurityException, ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
Object mainReturn = null;
Method m = null;
JsonElement argsElem = params.get("args");
if (reqID > 0) {
returns.put("reqId", reqID);
}
if (argsElem != null) {
JsonArray args = params.get("args").getAsJsonArray();
m = CommonUtils.getMethod(service.getClass(), methodName, args.size());
Pair<Boolean, Map<Integer, String>> ngCast = CommonUtils.getParamCastMap(m);
Type[] parameters = m.getGenericParameterTypes();
if (parameters.length == args.size()) {
List<Object> argsValues = new ArrayList<>();
for (int i = 0; i < parameters.length; i++) {
JsonElement element = args.get(i);
Type type = null;
if (ngCast != null) {
type = CommonUtils.getParamType(service, ngCast.getValue().get(i), ngCast.getKey());
}
if (element.isJsonPrimitive()) {
Class<?> clazz = null;
String typeString = ((parameters[i]).toString());
if (typeString.startsWith("class")) {
clazz = Class.forName(typeString.substring(6));
if (clazz.equals(Object.class)) {
clazz = CommonUtils.getPrimitiveClass(element);
}
} else {
clazz = builtInMap.get(typeString);
}
String val = element.getAsString();
if (type == null) {
argsValues.add(CommonUtils.convertFromString(val, clazz));
} else {
argsValues.add(util.deserialise(type, element));
}
} else if (element.isJsonArray()) {
JsonArray arr = element.getAsJsonArray();
if (type == null) {
argsValues.add(util.deserialise(parameters[i], arr));
} else {
argsValues.add(util.deserialise(type, arr));
}
} else {
if (type == null) {
argsValues.add(util.deserialise(parameters[i], element));
} else {
argsValues.add(util.deserialise(type, element));
}
}
}
if (!CommonUtils.isGetter(m)) {
update(service, params);
}
try {
mainReturn = m.invoke(service, argsValues.toArray());
} catch (Exception e) {
handleException(m, e);
e.printStackTrace();
}
}
} else {
m = CommonUtils.getMethod(service.getClass(), methodName, 0);
// handling methods that took HttpServletRequest as parameter
if (!CommonUtils.isGetter(m)) {
update(service, params);
}
mainReturn = m.invoke(service);
}
ModelQueryImpl qImpl = (ModelQueryImpl) modelQueryFactory.get(service.getClass());
Map<String, Object> scMap = new HashMap<>(qImpl.getData());
returns.putAll(scMap);
qImpl.getData().clear();
if (!modelQueryFactory.getRootScope().getRootScopeMap().isEmpty()) {
returns.put("rootScope", new HashMap<>(modelQueryFactory.getRootScope().getRootScopeMap()));
modelQueryFactory.getRootScope().getRootScopeMap().clear();
}
String[] updates = null;
if (m != null && m.isAnnotationPresent(NGReturn.class)) {
if (mainReturn == null)
mainReturn = "";
NGReturn ngReturn = m.getAnnotation(NGReturn.class);
updates = ngReturn.updates();
if (ngReturn.model().length() > 0) {
returns.put(ngReturn.model(), mainReturn);
Map<String, String> binding = new HashMap<>();
binding.put("boundTo", ngReturn.model());
mainReturn = binding;
}
}
if (m != null && m.isAnnotationPresent(NGPostConstruct.class)) {
NGPostConstruct ngPostConstruct = m.getAnnotation(NGPostConstruct.class);
updates = ngPostConstruct.updates();
}
if (updates != null) {
if ((updates.length == 1) && (updates[0].equals("*"))) {
List<String> upd = new ArrayList<>();
for (Method met : service.getClass().getDeclaredMethods()) {
if (CommonUtils.isGetter(met)) {
String fieldName = (met.getName()).substring(3);
String firstCar = fieldName.substring(0, 1);
upd.add((firstCar.toLowerCase() + fieldName.substring(1)));
}
}
updates = new String[upd.size()];
for (int i = 0; i < upd.size(); i++) {
updates[i] = upd.get(i);
}
}
}
if (updates != null) {
for (String up : updates) {
String getterName = GETTER_PREFIX + up.substring(0, 1).toUpperCase() + up.substring(1);
Method getter;
try {
getter = service.getClass().getMethod(getterName);
} catch (NoSuchMethodException e) {
getter = service.getClass().getMethod((getterName.replace(GETTER_PREFIX, BOOLEAN_GETTER_PREFIX)));
}
Object result = getter.invoke(service);
returns.put(up, result);
}
}
returns.put("mainReturn", mainReturn);
if (!logger.getLogPool().isEmpty()) {
returns.put("log", logger.getLogPool().toArray());
logger.getLogPool().clear();
}
}
Aggregations