org.apache.velocity.util.introspection.VelPropertyGet Java Examples

The following examples show how to use org.apache.velocity.util.introspection.VelPropertyGet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestableUberspect.java    From ApprovalTests.Java with Apache License 2.0 6 votes vote down vote up
public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i) throws Exception
{
  AbstractExecutor executor;
  if (obj == null)
  { throw new VelocityParsingError("tried " + getPropertyText("null", identifier), i); }
  Class<? extends Object> claz = obj.getClass();
  // trying getFoo()
  executor = new PropertyExecutor(log, introspectorWithLog, claz, identifier);
  if (!executor.isAlive())
  {
    // trying  get("foo")
    executor = new GetExecutor(log, introspectorWithLog, claz, identifier);
  }
  if (!executor.isAlive())
  {
    // trying  isFoo()
    executor = new BooleanPropertyExecutor(log, introspectorWithLog, claz, identifier);
  }
  if (!executor.isAlive())
  { throw new VelocityParsingError("Did not find " + getPropertyText(obj.getClass().getName(), identifier), i); }
  return new VelGetterImpl(executor);
}
 
Example #2
Source File: WebappUberspector.java    From velocity-tools with Apache License 2.0 6 votes vote down vote up
/**
 * Property getter
 * @param obj target object
 * @param identifier property key
 * @param i tool info
 * @return A Velocity Getter Method.
 */
public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i)
{
    if (obj == null)
    {
        return null;
    }
    VelPropertyGet ret = super.getPropertyGet(obj,identifier,i);

    if(ret == null)
    {
        Class claz = obj.getClass();
        if(obj instanceof HttpServletRequest
            || obj instanceof HttpSession
            || obj instanceof ServletContext)
        {
            AbstractExecutor executor = new GetAttributeExecutor(log, introspector, claz, identifier);
            ret = executor.isAlive() ? new VelGetterImpl(executor) : null;
        }
    }
    return ret;
}
 
Example #3
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
public void testNullPropertyGetter()
    throws Exception
{
    Uberspect u = ri.getUberspect();
    GetPutObject gpo = new GetPutObject();
    Map map = new HashMap();

    VelPropertyGet getter = u.getPropertyGet(gpo, null, null);

    // Don't screw up on null properties. That should map to get() on the GPO.
    assertNotNull(getter);
    assertEquals("Found wrong method", "get", getter.getMethodName());

    // And should be null on a Map which does not have a get()
    getter = u.getPropertyGet(map, null, null);
    assertNull(getter);

}
 
Example #4
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testNullObjects()
        throws Exception
{
    // How about some null objects... Gee, I'm mean. ;-)
    Uberspect u = ri.getUberspect();

    VelPropertyGet getter = u.getPropertyGet(null, "foo", null);
    assertNull(getter);

    VelPropertySet setter = u.getPropertySet(null, "foo", Object.class, null);
    assertNull(setter);
}
 
Example #5
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testEmptyPropertyGetter()
        throws Exception
{
    Uberspect u = ri.getUberspect();
    Map map = new HashMap();

    VelPropertyGet getter = u.getPropertyGet(map, "", null);

    // Don't screw up on empty properties. That should map to get("")
    assertNotNull(getter);
    assertEquals("Found wrong method", "get", getter.getMethodName());
}
 
Example #6
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testRegularGetters()
        throws Exception
{
    VelPropertyGet getter;

    Uberspect u = ri.getUberspect();
    UberspectorTestObject uto = new UberspectorTestObject();

    // getRegular()
    getter = u.getPropertyGet(uto, "Regular", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "getRegular", getter.getMethodName());

    // Lowercase regular
    getter = u.getPropertyGet(uto, "regular", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "getRegular", getter.getMethodName());

    // lowercase: getpremium()
    getter = u.getPropertyGet(uto, "premium", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "getpremium", getter.getMethodName());

    // test uppercase: getpremium()
    getter = u.getPropertyGet(uto, "Premium", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "getpremium", getter.getMethodName());
}
 
Example #7
Source File: UberspectorTestCase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public void testBooleanGetters()
        throws Exception
{
    VelPropertyGet getter;

    Uberspect u = ri.getUberspect();
    UberspectorTestObject uto = new UberspectorTestObject();

    // getRegular()
    getter = u.getPropertyGet(uto, "RegularBool", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "isRegularBool", getter.getMethodName());

    // Lowercase regular
    getter = u.getPropertyGet(uto, "regularBool", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "isRegularBool", getter.getMethodName());

    // lowercase: getpremiumBool()
    getter = u.getPropertyGet(uto, "premiumBool", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "ispremiumBool", getter.getMethodName());

    // test uppercase: ()
    getter = u.getPropertyGet(uto, "PremiumBool", null);
    assertNotNull(getter);
    assertEquals("Found wrong method", "ispremiumBool", getter.getMethodName());
}
 
Example #8
Source File: UberspectTestImpl.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i)
{
    VelPropertyGet propertyGet = super.getPropertyGet(obj, identifier, i);

    if (propertyGet == null)
    {
        if (obj == null)
            throw new UberspectTestException("Can't call getter '" + identifier + "' on null object",i);
        else
            throw new UberspectTestException("Did not find "+ obj.getClass().getName()+"."+identifier, i);
    }

    return propertyGet;
}
 
Example #9
Source File: UberspectDelegate.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public VelPropertyGet getPropertyGet(Object obj, String identifier, Info info) throws Exception {
	return uberspector.getPropertyGet( obj, identifier, info);
}
 
Example #10
Source File: ChainedUberspectorsTestCase.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
public VelPropertyGet getPropertyGet(Object obj, String identifier, Info info)
{
    identifier = identifier.replaceAll("foo","bar");
    return super.getPropertyGet(obj,identifier,info);
}