public Collection<ELFunctionDefinition> getAllFunctions() { List<ELFunctionDefinition> fns = new ArrayList<>(functionLibrary.entries()); if (parent != null) { fns.addAll(parent.getAllFunctions()); } final Set<String> disabledFunctions = disabled == null ? new HashSet<>() : disabled.getOrDefault(Library.FUNCTION, new HashSet<>()); return fns.stream().filter(f -> !disabledFunctions.contains(f.getName())).collect(Collectors.toList()); }
private void addFnDocs(JinjavaDoc doc) { for (ELFunctionDefinition fn : jinjava.getGlobalContext().getAllFunctions()) { if (StringUtils.isBlank(fn.getNamespace())) { Method realMethod = fn.getMethod(); if (realMethod.getDeclaringClass().getName().contains(InjectedContextFunctionProxy.class.getSimpleName())) { try { realMethod = (Method) realMethod.getDeclaringClass().getField("delegate").get(null); } catch (Exception e) { throw new RuntimeException(e); } } com.hubspot.jinjava.doc.annotations.JinjavaDoc docAnnotation = realMethod.getAnnotation(com.hubspot.jinjava.doc.annotations.JinjavaDoc.class); if (docAnnotation == null) { LOG.warn("Function {} doesn't have a @{} annotation", fn.getName(), com.hubspot.jinjava.doc.annotations.JinjavaDoc.class.getName()); doc.addFunction(new JinjavaDocFunction(fn.getLocalName(), "", "", false, new JinjavaDocParam[] {}, new JinjavaDocSnippet[] {}, Collections.emptyMap())); } else if (!docAnnotation.hidden()) { doc.addFunction(new JinjavaDocFunction(fn.getLocalName(), docAnnotation.value(), docAnnotation.aliasOf(), docAnnotation.deprecated(), extractParams(docAnnotation.params()), extractSnippets(docAnnotation.snippets()), extractMeta(docAnnotation.meta()))); } } } }
@Test public void testDefineProxy() throws Exception { Method m = MyClass.class.getDeclaredMethod("concatState", String.class); MyClass instance = new MyClass("bar"); ELFunctionDefinition proxy = InjectedContextFunctionProxy.defineProxy("ns", "fooproxy", m, instance); assertThat(proxy.getName()).isEqualTo("ns:fooproxy"); assertThat(proxy.getMethod().getDeclaringClass().getSimpleName()).isEqualTo( InjectedContextFunctionProxy.class.getSimpleName() + "$$ns$$fooproxy"); assertThat(proxy.getMethod().invoke(null, "foo")).isEqualTo("foobar"); }
@Test public void testDefineProxy() throws Exception { Method m = MyClass.class.getDeclaredMethod("concatState", String.class); MyClass instance = new MyClass("bar"); ELFunctionDefinition proxy = InjectedContextFunctionProxy.defineProxy("ns", "fooproxy", m, instance); assertThat(proxy.getName()).isEqualTo("ns:fooproxy"); assertThat(proxy.getMethod().getDeclaringClass().getSimpleName()).isEqualTo( InjectedContextFunctionProxy.class.getSimpleName() + "$$ns$$fooproxy"); assertThat(proxy.getMethod().invoke(null, "foo")).isEqualTo("foobar"); }