Пример #1
0
  private static JSONArray methodsForLanguage(Context context, String language) {
    Class[] scriptingClasses = {JavaScriptEngine.class, SchemeEngine.class};

    ArrayList<String> languages = new ArrayList<>();
    languages.add(BootstrapSiteExporter.LANGUAGE_ALL.toLowerCase());

    if (languages.contains(language) == false) languages.add(language.toLowerCase());

    JSONArray declaredMethods = new JSONArray();
    HashSet<String> included = new HashSet<>();

    for (Class classObj : scriptingClasses) {
      Method[] methods = classObj.getMethods();

      for (Method method : methods) {
        Annotation[] annotations = method.getDeclaredAnnotations();

        for (Annotation annotation : annotations) {
          if (annotation instanceof ScriptingEngineMethod) {
            ScriptingEngineMethod scriptAnnotation = (ScriptingEngineMethod) annotation;

            String category = context.getString(scriptAnnotation.category());
            String assetPath = scriptAnnotation.assetPath();
            String scriptLanguage = scriptAnnotation.language();
            String methodName = method.getName();

            if (languages.contains(scriptLanguage.toLowerCase())
                && included.contains(method.toGenericString()) == false) {
              StringBuilder args = new StringBuilder();

              for (String argument : ((ScriptingEngineMethod) annotation).arguments()) {
                if (args.length() > 0) args.append(", ");

                args.append(argument);
              }

              JSONObject methodDef = new JSONObject();

              try {
                methodDef.put(
                    BootstrapSiteExporter.METHOD_NAME, methodName + "(" + args.toString() + ")");
                methodDef.put(BootstrapSiteExporter.METHOD_FULL_NAME, method.toGenericString());
                methodDef.put(BootstrapSiteExporter.METHOD_LANGUAGE, scriptLanguage);
                methodDef.put(BootstrapSiteExporter.METHOD_CATEGORY, category);

                if (assetPath.trim().length() > 0)
                  methodDef.put(BootstrapSiteExporter.METHOD_ASSET_PATH, assetPath);
              } catch (JSONException e) {
                e.printStackTrace();
              }

              declaredMethods.put(methodDef);

              included.add(method.toGenericString());
            }
          }
        }
      }
    }

    return declaredMethods;
  }