public Method lookupJavaMethod( IEvaluator<Result<IValue>> eval, FunctionDeclaration func, Environment env, boolean hasReflectiveAccess) { if (!func.isAbstract()) { throw new NonAbstractJavaFunction(func); } String className = getClassName(func); String name = Names.name(func.getSignature().getName()); if (className.length() == 0) { // TODO: Can this ever be thrown since the Class instance has // already been identified via the javaClass tag. throw new MissingTag(JAVA_CLASS_TAG, func); } for (ClassLoader loader : loaders) { try { Class<?> clazz = loader.loadClass(className); Parameters parameters = func.getSignature().getParameters(); Class<?>[] javaTypes = getJavaTypes(parameters, env, hasReflectiveAccess); try { Method m; if (javaTypes.length > 0) { // non-void m = clazz.getMethod(name, javaTypes); } else { m = clazz.getMethod(name); } return m; } catch (SecurityException e) { throw RuntimeExceptionFactory.permissionDenied( vf.string(e.getMessage()), eval.getCurrentAST(), eval.getStackTrace()); } catch (NoSuchMethodException e) { throw new UndeclaredJavaMethod(e.getMessage(), func); } } catch (ClassNotFoundException e) { continue; } } throw new UndeclaredJavaMethod(className + "." + name, func); }
private String getClassName(FunctionDeclaration declaration) { Tags tags = declaration.getTags(); if (tags.hasTags()) { for (Tag tag : tags.getTags()) { if (Names.name(tag.getName()).equals(JAVA_CLASS_TAG)) { if (tag.hasContents()) { String contents = ((TagString.Lexical) tag.getContents()).getString(); if (contents.length() > 2 && contents.startsWith("{")) { contents = contents.substring(1, contents.length() - 1); } return contents; } } } } return ""; }