New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a <o:importClass type="..." /> #381

Closed
kalgon opened this Issue May 16, 2017 · 5 comments

Comments

Projects
None yet
3 participants
@kalgon

kalgon commented May 16, 2017

The current version of EL does not support the instanceof operator.

A possible workaround would be to import the Class in the request scope so that we could call isInstance(), isAssignableFrom() on it:

<o:importClass type="eg.Customer" var="customerClass" />
<ui:fragment rendered="#{customerClass.isInstance(currentObject)}">...</ui:fragment>

What do you think?

@BryanV91

This comment has been minimized.

Show comment
Hide comment
@BryanV91

BryanV91 May 17, 2017

Workaround:

Create a request scoped bean, that contains this method:

@RequestScoped
@Named
public class UtilityBean implements Serializable {

public boolean compareClass(String className, Object obj) {
		try {
			return Class.forName(className).isInstance(obj);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return false;
	}

}

In your JSF page:

<ui:fragment rendered="#{utilityBean.compareClass('eg.Customer',currentObject)}">...</ui:fragment>

You will avoid having this:

<o:importClass type="eg.Customer" var="customerClass" />
<o:importClass type="eg.Car" var="carClass" />
<o:importClass type="eg.Boat" var="boatClass" />
<o:importClass type="eg.OtherClass" var="otherClass" />
...
<o:importClass type="eg.nthClass" var="nthClass" />
<ui:fragment rendered="#{customerClass.isInstance(currentObject)}">...</ui:fragment>

BryanV91 commented May 17, 2017

Workaround:

Create a request scoped bean, that contains this method:

@RequestScoped
@Named
public class UtilityBean implements Serializable {

public boolean compareClass(String className, Object obj) {
		try {
			return Class.forName(className).isInstance(obj);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return false;
	}

}

In your JSF page:

<ui:fragment rendered="#{utilityBean.compareClass('eg.Customer',currentObject)}">...</ui:fragment>

You will avoid having this:

<o:importClass type="eg.Customer" var="customerClass" />
<o:importClass type="eg.Car" var="carClass" />
<o:importClass type="eg.Boat" var="boatClass" />
<o:importClass type="eg.OtherClass" var="otherClass" />
...
<o:importClass type="eg.nthClass" var="nthClass" />
<ui:fragment rendered="#{customerClass.isInstance(currentObject)}">...</ui:fragment>
@kalgon

This comment has been minimized.

Show comment
Hide comment
@kalgon

kalgon May 17, 2017

Even better alternative:

<o:importFunctions type="java.lang.Class" var="k" />
<ui:fragment rendered="#{k:forName('eg.Customer').isInstance(currentObject)}">...</ui:fragment>

kalgon commented May 17, 2017

Even better alternative:

<o:importFunctions type="java.lang.Class" var="k" />
<ui:fragment rendered="#{k:forName('eg.Customer').isInstance(currentObject)}">...</ui:fragment>
@BryanV91

This comment has been minimized.

Show comment
Hide comment
@BryanV91

BryanV91 May 17, 2017

Really cool alternative.

BryanV91 commented May 17, 2017

Really cool alternative.

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC May 20, 2017

Member

Cool alternative indeed :)

An <o:importClass> is however a bit on the edge. Here are another alternatives: http://stackoverflow.com/q/10314214

Member

BalusC commented May 20, 2017

Cool alternative indeed :)

An <o:importClass> is however a bit on the edge. Here are another alternatives: http://stackoverflow.com/q/10314214

BalusC added a commit that referenced this issue May 21, 2017

@BalusC

This comment has been minimized.

Show comment
Hide comment
@BalusC

BalusC May 21, 2017

Member

I added an of:isInstance() function. It'll be in today's latest 3.0-SNAPSHOT.

Member

BalusC commented May 21, 2017

I added an of:isInstance() function. It'll be in today's latest 3.0-SNAPSHOT.

@BalusC BalusC closed this May 21, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment