0

Let's suppose I have following structure:

1) Managed Bean:

@ViewScoped
@ManagedBean
public class TestBean {
    private Test test;

    //getters/setters
}

2) Test class:

public class Test {
    private String attribute;

    //gets/sets
}

3) XHTML

<p:inputText id="test" value="#{testBean.test.atribute}" />

Now, I know there is a way to find and get component instance:

UIComponent c = view.findComponent(s);

From UIComponent, how do I get the type bound to component?

What I need is to get full qualified class name from what is set as "value" attribute in component. Something like: package.Test.attribute.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Emilio Numazaki
  • 836
  • 1
  • 5
  • 25
  • Since ui know it's an InputText from primefaces can't you simply do InputText c = ((InputText) view.findComponent(s)).getValue().getClass()? – Mário Fernandes Aug 18 '17 at 13:58
  • Hello @MárioFernandes, thank you for your answer. I did what you suggested but it is throwing java.lang.ClassCastException: javax.faces.component.UINamingContainer cannot be cast to org.primefaces.component.inputtext.InputText – Emilio Numazaki Aug 18 '17 at 14:14
  • I'm assuming you're finding the incorrect component then. Since p:inputText is not a UINamingContainer. Are you passing the full id (maybe there needs to be the form id preprended) – Mário Fernandes Aug 18 '17 at 14:17
  • Yes, I'm using PrimeFace's ComponentUtils.findComponentClientId utility method. My guess it is returning composite instead of inputText but it doesn't make sense since composite have no ID. – Emilio Numazaki Aug 18 '17 at 14:36
  • I see that the findComponentClientId uses a method firstWithId, do you have anything with the same id in the dom? (not the same since it's not allowed but starting the same at least) – Mário Fernandes Aug 21 '17 at 06:45

1 Answers1

1

UIComponent offers getValueExpression("attributeName")

sample :

  UIViewRoot viewRoot = Faces.getViewRoot();
            UIComponent component= viewRoot.findComponent("x");
            ValueExpression value = component.getValueExpression("value");
            Class<?> expectedType = value.getType(Faces.getELContext());

NB:Faces here is from Omnifaces, which is a "Collection of utility methods for the JSF API that are mainly shortcuts for obtaining stuff from the thread local FacesContext. "

excepts from getType() javadoc

public abstract Class getType(ELContext context) Evaluates the expression relative to the provided context, and returns the most general type that is acceptable for an object to be passed as the value parameter in a future call to the setValue(javax.el.ELContext. java.lang.Object) method. This is not always the same as getValue().getClass(). For example, in the case of an expression that references an array element, the getType method will return the element type of the array, which might be a superclass of the type of the actual element that is currently in the specified array element.

For MethodExpression read this.

basamoahjnr
  • 145
  • 11
  • This implies that the user must have omnifaces :/ – Mário Fernandes Aug 21 '17 at 06:43
  • No, the first part of the answer is plain jsf – Kukeltje Aug 21 '17 at 07:03
  • Indeed it does :) – Mário Fernandes Aug 21 '17 at 07:38
  • First of all, thank you all for your support. There is no problem using Ominifaces, because I'm already using it for other things. I did what @Kukeltje said but it returns only last attribute's type,what it's expected behavior but what I need is something deeper, I mean, I need the entity type instead of only attribute. For instance, if my 'value' declaration is 'testBean.test.attribute', I'm trying to get full qualified name of class that holds 'test.attribute', in this case: package.Test.attribute. – Emilio Numazaki Aug 21 '17 at 14:27
  • The reason I'm looking for a way to do it, is because I'm developing an advanced access control mecanism. Where, based on bean+attr bound to a component, it checks in database if is there any constraints on it for example: It is read only for, or hidden, or accessible by specific roles, etc. I know there is easier ways to do it but I whish something more automated, where developer doesn't need to worry about any additional declaration to fullfill this requirement. – Emilio Numazaki Aug 21 '17 at 14:30