org.azeckoski.reflectutils.ReflectUtils Java Examples

The following examples show how to use org.azeckoski.reflectutils.ReflectUtils. 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: CSVApisUtil.java    From sofa-acts with Apache License 2.0 6 votes vote down vote up
public static Map<String, Field> findTargetClsFields(Class<?> cls) {
    if (isWrapClass(cls)) {
        return null;
    }
    ReflectUtils refUtil = ReflectUtils.getInstance();
    ClassFields<?> clsFiled = refUtil.analyzeClass(cls);
    List<Field> getPro = clsFiled.getClassData().getFields();
    Map<String, Field> reClsProp = new HashMap<String, Field>();
    for (Field proValue : getPro) {
        if (Modifier.isFinal(proValue.getModifiers())
            || Modifier.isStatic(proValue.getModifiers())) {
            continue;
        }
        String propName = proValue.getName();
        reClsProp.put(propName, proValue);
    }

    return reClsProp;
}
 
Example #2
Source File: RequestStorageImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public <T> T getStoredValueAsType(Class<T> type, String key) {
    if (type == null) {
        throw new IllegalArgumentException("type must be non-null");
    }
    Object value = getStoredValue(key);
    T togo = null;
    if (value != null) {
        if (value.getClass().isAssignableFrom(type)) {
            // type matches the requested one
            togo = (T) value;
        } else {
            togo = (T) ReflectUtils.getInstance().convert(value, type);
        }
    }
    return togo;
}
 
Example #3
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#makeFieldNameFromMethod(java.lang.String)}.
 */
public void testMakeFieldNameFromMethod() {
    String name = null;

    name = ReflectUtils.makeFieldNameFromMethod("getStuff");
    assertEquals("stuff", name);

    name = ReflectUtils.makeFieldNameFromMethod("getSomeStuff");
    assertEquals("someStuff", name);

    name = ReflectUtils.makeFieldNameFromMethod("setStuff");
    assertEquals("stuff", name);

    name = ReflectUtils.makeFieldNameFromMethod("isStuff");
    assertEquals("stuff", name);

    name = ReflectUtils.makeFieldNameFromMethod("stuff");
    assertEquals("stuff", name);
}
 
Example #4
Source File: EntityProviderMethodStoreImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Validates param types against a given list of valid types
 * @param paramTypes an array of parameter types
 * @param validTypes the types that are valid
 * @return the new valid array of param types
 * @throws IllegalArgumentException is the param types are invalid
 */
protected static Class<?>[] validateParamTypes(Class<?>[] paramTypes, Class<?>[] validTypes) {
    Class<?>[] validParams = new Class<?>[paramTypes.length];
    for (int i = 0; i < paramTypes.length; i++) {
        boolean found = false;
        Class<?> paramType = paramTypes[i];
        if (validTypes != null) {
            for (int j = 0; j < validTypes.length; j++) {
                if (validTypes[j].isAssignableFrom(paramType)) {
                    validParams[i] = validTypes[j];
                    found = true;
                }
            }
        }
        if (!found) {
            throw new IllegalArgumentException("Invalid method params: param type is not allowed: " + paramType.getName() 
                    + " : valid types include: " + ReflectUtils.arrayToString(validTypes));
        }
    }
    return validParams;
}
 
Example #5
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void testPopulateFromParams() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();
    List<String> results = null;
    Map<String, String[]> properties = new HashMap<String, String[]>();

    TestEntity target = new TestEntity();

    properties.put("id", new String[] {"1000"});
    properties.put("extra", new String[] {"OLD"});
    properties.put("sArray", new String[] {"AA","BB","CC"});
    results = reflectUtil.populateFromParams(target, properties);
    assertNotNull(results);
    assertEquals(3, results.size());
    assertNotNull(target);
    assertEquals(new Long(1000), target.getId());
    assertEquals("OLD", target.getExtra());
    assertEquals("33", target.getEntityId());
    assertEquals(null, target.getBool());
    assertEquals(3, target.getSArray().length);

}
 
Example #6
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#getFieldType(java.lang.Class, java.lang.String)}.
 */
public void testGetFieldType() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();
    Class<?> type;

    type = reflectUtil.getFieldType(TestBean.class, "myString");
    assertNotNull(type);
    assertEquals(String.class, type);

    type = reflectUtil.getFieldType(TestBean.class, "myInt");
    assertNotNull(type);
    assertEquals(int.class, type);

    type = reflectUtil.getFieldType(TestPea.class, "id");
    assertNotNull(type);
    assertEquals(String.class, type);

    type = reflectUtil.getFieldType(TestNesting.class, "sList");
    assertNotNull(type);
    assertEquals(List.class, type);

    type = reflectUtil.getFieldType(TestNesting.class, "sMap");
    assertNotNull(type);
    assertEquals(Map.class, type);
}
 
Example #7
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#setFieldStringValue(java.lang.Object, java.lang.String, java.lang.String)}.
 */
public void testSetFieldStringValue() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();
    Object thing = null;

    thing = new TestBean();
    reflectUtil.setFieldValue(thing, "myString", "TEST");
    assertEquals("TEST", ((TestBean)thing).getMyString());

    thing = new TestBean();
    reflectUtil.setFieldValue(thing, "myInt", "10");
    assertEquals(10, ((TestBean)thing).getMyInt());

    thing = new TestEntity();
    reflectUtil.setFieldValue(thing, "id", "6");
    assertEquals(new Long(6), ((TestEntity)thing).getId());

    thing = new TestEntity();
    reflectUtil.setFieldValue(thing, "sArray", "A, B, C");
    assertEquals(3, ((TestEntity)thing).getSArray().length);
    assertEquals("A", ((TestEntity)thing).getSArray()[0]);
    assertEquals("B", ((TestEntity)thing).getSArray()[1]);
    assertEquals("C", ((TestEntity)thing).getSArray()[2]);
}
 
Example #8
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#clone(java.lang.Object, int, java.lang.String[])}.
 */
public void testClone() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();

    TestBean tb = new TestBean();
    tb.setMyInt(100);
    tb.setMyString("1000");
    TestBean tbClone = reflectUtil.clone(tb, 0, null);
    assertNotNull(tbClone);
    assertEquals(tb.getMyInt(), tbClone.getMyInt());
    assertEquals(tb.getMyString(), tbClone.getMyString());

    // test skipping values
    tbClone = reflectUtil.clone(tb, 0, new String[] {"myInt"});
    assertNotNull(tbClone);
    assertTrue(tb.getMyInt() != tbClone.getMyInt());
    assertEquals(tb.getMyString(), tbClone.getMyString());

    tbClone = reflectUtil.clone(tb, 5, null);
    assertNotNull(tbClone);
    assertEquals(tb.getMyInt(), tbClone.getMyInt());
    assertEquals(tb.getMyString(), tbClone.getMyString());

}
 
Example #9
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#getFieldType(java.lang.Class, java.lang.String)}.
 */
public void testGetFieldType() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();
    Class<?> type;

    type = reflectUtil.getFieldType(TestBean.class, "myString");
    assertNotNull(type);
    assertEquals(String.class, type);

    type = reflectUtil.getFieldType(TestBean.class, "myInt");
    assertNotNull(type);
    assertEquals(int.class, type);

    type = reflectUtil.getFieldType(TestPea.class, "id");
    assertNotNull(type);
    assertEquals(String.class, type);

    type = reflectUtil.getFieldType(TestNesting.class, "sList");
    assertNotNull(type);
    assertEquals(List.class, type);

    type = reflectUtil.getFieldType(TestNesting.class, "sMap");
    assertNotNull(type);
    assertEquals(Map.class, type);
}
 
Example #10
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#clone(java.lang.Object, int, java.lang.String[])}.
 */
public void testClone() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();

    TestBean tb = new TestBean();
    tb.setMyInt(100);
    tb.setMyString("1000");
    TestBean tbClone = reflectUtil.clone(tb, 0, null);
    assertNotNull(tbClone);
    assertEquals(tb.getMyInt(), tbClone.getMyInt());
    assertEquals(tb.getMyString(), tbClone.getMyString());

    // test skipping values
    tbClone = reflectUtil.clone(tb, 0, new String[] {"myInt"});
    assertNotNull(tbClone);
    assertTrue(tb.getMyInt() != tbClone.getMyInt());
    assertEquals(tb.getMyString(), tbClone.getMyString());

    tbClone = reflectUtil.clone(tb, 5, null);
    assertNotNull(tbClone);
    assertEquals(tb.getMyInt(), tbClone.getMyInt());
    assertEquals(tb.getMyString(), tbClone.getMyString());

}
 
Example #11
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#setFieldStringValue(java.lang.Object, java.lang.String, java.lang.String)}.
 */
public void testSetFieldStringValue() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();
    Object thing = null;

    thing = new TestBean();
    reflectUtil.setFieldValue(thing, "myString", "TEST");
    assertEquals("TEST", ((TestBean)thing).getMyString());

    thing = new TestBean();
    reflectUtil.setFieldValue(thing, "myInt", "10");
    assertEquals(10, ((TestBean)thing).getMyInt());

    thing = new TestEntity();
    reflectUtil.setFieldValue(thing, "id", "6");
    assertEquals(new Long(6), ((TestEntity)thing).getId());

    thing = new TestEntity();
    reflectUtil.setFieldValue(thing, "sArray", "A, B, C");
    assertEquals(3, ((TestEntity)thing).getSArray().length);
    assertEquals("A", ((TestEntity)thing).getSArray()[0]);
    assertEquals("B", ((TestEntity)thing).getSArray()[1]);
    assertEquals("C", ((TestEntity)thing).getSArray()[2]);
}
 
Example #12
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public void testPopulateFromParams() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();
    List<String> results = null;
    Map<String, String[]> properties = new HashMap<String, String[]>();

    TestEntity target = new TestEntity();

    properties.put("id", new String[] {"1000"});
    properties.put("extra", new String[] {"OLD"});
    properties.put("sArray", new String[] {"AA","BB","CC"});
    results = reflectUtil.populateFromParams(target, properties);
    assertNotNull(results);
    assertEquals(3, results.size());
    assertNotNull(target);
    assertEquals(new Long(1000), target.getId());
    assertEquals("OLD", target.getExtra());
    assertEquals("33", target.getEntityId());
    assertEquals(null, target.getBool());
    assertEquals(3, target.getSArray().length);

}
 
Example #13
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#makeFieldNameFromMethod(java.lang.String)}.
 */
public void testMakeFieldNameFromMethod() {
    String name = null;

    name = ReflectUtils.makeFieldNameFromMethod("getStuff");
    assertEquals("stuff", name);

    name = ReflectUtils.makeFieldNameFromMethod("getSomeStuff");
    assertEquals("someStuff", name);

    name = ReflectUtils.makeFieldNameFromMethod("setStuff");
    assertEquals("stuff", name);

    name = ReflectUtils.makeFieldNameFromMethod("isStuff");
    assertEquals("stuff", name);

    name = ReflectUtils.makeFieldNameFromMethod("stuff");
    assertEquals("stuff", name);
}
 
Example #14
Source File: EntityProviderMethodStoreImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Validates param types against a given list of valid types
 * @param paramTypes an array of parameter types
 * @param validTypes the types that are valid
 * @return the new valid array of param types
 * @throws IllegalArgumentException is the param types are invalid
 */
protected static Class<?>[] validateParamTypes(Class<?>[] paramTypes, Class<?>[] validTypes) {
    Class<?>[] validParams = new Class<?>[paramTypes.length];
    for (int i = 0; i < paramTypes.length; i++) {
        boolean found = false;
        Class<?> paramType = paramTypes[i];
        if (validTypes != null) {
            for (int j = 0; j < validTypes.length; j++) {
                if (validTypes[j].isAssignableFrom(paramType)) {
                    validParams[i] = validTypes[j];
                    found = true;
                }
            }
        }
        if (!found) {
            throw new IllegalArgumentException("Invalid method params: param type is not allowed: " + paramType.getName() 
                    + " : valid types include: " + ReflectUtils.arrayToString(validTypes));
        }
    }
    return validParams;
}
 
Example #15
Source File: RequestStorageImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
public <T> T getStoredValueAsType(Class<T> type, String key) {
    if (type == null) {
        throw new IllegalArgumentException("type must be non-null");
    }
    Object value = getStoredValue(key);
    T togo = null;
    if (value != null) {
        if (value.getClass().isAssignableFrom(type)) {
            // type matches the requested one
            togo = (T) value;
        } else {
            togo = (T) ReflectUtils.getInstance().convert(value, type);
        }
    }
    return togo;
}
 
Example #16
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#capitalize(java.lang.String)}.
 */
public void testCapitalize() {
    assertTrue( ReflectUtils.capitalize("lower").equals("Lower") );
    assertTrue( ReflectUtils.capitalize("UPPER").equals("UPPER") );
    assertTrue( ReflectUtils.capitalize("myStuff").equals("MyStuff") );
    assertTrue( ReflectUtils.capitalize("MyStuff").equals("MyStuff") );
    assertTrue( ReflectUtils.capitalize("").equals("") );
    assertTrue( ReflectUtils.capitalize("m").equals("M") );
}
 
Example #17
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#contains(T[], java.lang.Object)}.
 */
public void testContains() {
    assertFalse( ReflectUtils.contains(new String[] {}, "stuff") );
    assertFalse( ReflectUtils.contains(new String[] {"apple"}, "stuff") );
    assertTrue( ReflectUtils.contains(new String[] {"stuff"}, "stuff") );
    assertTrue( ReflectUtils.contains(new String[] {"stuff","other","apple"}, "stuff") );
}
 
Example #18
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#getClassFromCollection(java.util.Collection)}.
 */
@SuppressWarnings("unchecked")
public void testGetClassFromCollection() {
    Class<?> result = null;

    // null returns object class
    result = ReflectUtils.getClassFromCollection(null);
    assertNotNull(result);
    assertEquals(Object.class, result);

    // empty collection is always object
    result = ReflectUtils.getClassFromCollection( new ArrayList<String>() );
    assertNotNull(result);
    assertEquals(Object.class, result);

    // NOTE: Cannot get real type from empty collections

    // try with collections that have things in them
    List<Object> l = new ArrayList<Object>();
    l.add(new String("testing"));
    result = ReflectUtils.getClassFromCollection(l);
    assertNotNull(result);
    assertEquals(String.class, result);

    HashSet<Object> s = new HashSet<Object>();
    s.add(new Double(22.0));
    result = ReflectUtils.getClassFromCollection(s);
    assertNotNull(result);
    assertEquals(Double.class, result);

    List v = new Vector<Object>();
    v.add( new Integer(30) );
    result = ReflectUtils.getClassFromCollection(v);
    assertNotNull(result);
    assertEquals(Integer.class, result);
}
 
Example #19
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#getObjectValues(java.lang.Object)}.
 */
public void testGetObjectValues() {
    Map<String, Object> m = null;
    ReflectUtils reflectUtil = ReflectUtils.getInstance();

    m = reflectUtil.getObjectValues( new TestNone() );
    assertNotNull(m);
    assertEquals(0, m.size());

    m = reflectUtil.getObjectValues( new TestPea() );
    assertNotNull(m);
    assertEquals(2, m.size());
    assertTrue(m.containsKey("id"));
    assertEquals("id", m.get("id"));
    assertTrue(m.containsKey("entityId"));
    assertEquals("EID", m.get("entityId"));

    m = reflectUtil.getObjectValues( new TestBean() );
    assertNotNull(m);
    assertEquals(2, m.size());
    assertTrue(m.containsKey("myInt"));
    assertTrue(m.containsKey("myString"));
    assertEquals(0, m.get("myInt"));
    assertEquals("woot", m.get("myString"));

    m = reflectUtil.getObjectValues( new TestEntity() );
    assertNotNull(m);
    assertEquals(6, m.size());
    assertTrue(m.containsKey("id"));
    assertTrue(m.containsKey("entityId"));
    assertTrue(m.containsKey("extra"));
    assertTrue(m.containsKey("sArray"));
    assertTrue(m.containsKey("bool"));
    assertTrue(m.containsKey("prefix"));
    assertEquals(new Long(3), m.get("id"));
    assertEquals("33", m.get("entityId"));
    assertEquals(null, m.get("extra"));
    assertEquals("crud", m.get("prefix"));
    assertTrue(m.get("sArray").getClass().isArray());
}
 
Example #20
Source File: EntityProviderManagerImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Get the capabilities implemented by this provider
 * 
 * @param provider
 * @return
 */
@SuppressWarnings("unchecked")
protected static List<Class<? extends EntityProvider>> extractCapabilities(EntityProvider provider) {
    List<Class<?>> superclasses = ReflectUtils.getSuperclasses(provider.getClass());
    Set<Class<? extends EntityProvider>> capabilities = new HashSet<Class<? extends EntityProvider>>();

    for (Class<?> superclazz : superclasses) {
        if (superclazz.isInterface() && EntityProvider.class.isAssignableFrom(superclazz)) {
            capabilities.add((Class<? extends EntityProvider>) superclazz);
        }
    }
    return new ArrayList<Class<? extends EntityProvider>>(capabilities);
}
 
Example #21
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#getFieldTypes(java.lang.Class)}.
 */
public void testGetFieldTypes() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();
    Map<String, Class<?>> types;

    types = reflectUtil.getFieldTypes(TestBean.class);
    assertNotNull(types);
    assertEquals(2, types.size());
    assertEquals(String.class, types.get("myString"));
    assertEquals(int.class, types.get("myInt"));
}
 
Example #22
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#getFieldNameWithAnnotation(java.lang.Class, java.lang.Class)}.
 */
public void testGetFieldNameWithAnnotation() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();
    String fieldName = null;

    fieldName = reflectUtil.getFieldNameWithAnnotation(TestBean.class, TestAnnote.class);
    assertEquals("myInt", fieldName);

    fieldName = reflectUtil.getFieldNameWithAnnotation(TestEntity.class, TestAnnote.class);
    assertEquals("entityId", fieldName);
}
 
Example #23
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#getSuperclasses(java.lang.Class)}.
 */
public void testGetSuperclasses() {
    List<Class<?>> superClasses = null;

    superClasses = ReflectUtils.getSuperclasses(TestNone.class);
    assertNotNull(superClasses);
    assertEquals(1, superClasses.size());
    assertEquals(TestNone.class, superClasses.get(0));

    superClasses = ReflectUtils.getSuperclasses(TestImplOne.class);
    assertNotNull(superClasses);
    assertEquals(3, superClasses.size());
    assertTrue( superClasses.contains(TestImplOne.class) );
    assertTrue( superClasses.contains(TestInterfaceOne.class) );
    assertTrue( superClasses.contains(Serializable.class) );

    superClasses = ReflectUtils.getSuperclasses(TestImplFour.class);
    assertNotNull(superClasses);
    assertTrue(superClasses.size() >= 7);
    assertTrue( superClasses.contains(TestImplFour.class) );
    assertTrue( superClasses.contains(TestInterfaceOne.class) );
    assertTrue( superClasses.contains(TestInterfaceFour.class) );
    assertTrue( superClasses.contains(Serializable.class) );
    assertTrue( superClasses.contains(Runnable.class) );
    assertTrue( superClasses.contains(Cloneable.class) );
    assertTrue( superClasses.contains(Readable.class) );
}
 
Example #24
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#setFieldValue(java.lang.Object, java.lang.String, java.lang.Object)}.
 */
public void testSetFieldValue() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();
    Object thing = null;

    thing = new TestBean();
    reflectUtil.setFieldValue(thing, "myString", "TEST");
    assertEquals("TEST", ((TestBean)thing).getMyString());

    thing = new TestBean();
    reflectUtil.setFieldValue(thing, "myInt", 5);
    assertEquals(5, ((TestBean)thing).getMyInt());

    thing = new TestEntity();
    reflectUtil.setFieldValue(thing, "sArray", new String[] {"A", "B", "C"});
    assertEquals(3, ((TestEntity)thing).getSArray().length);
    assertEquals("A", ((TestEntity)thing).getSArray()[0]);
    assertEquals("B", ((TestEntity)thing).getSArray()[1]);
    assertEquals("C", ((TestEntity)thing).getSArray()[2]);

    thing = new TestBean();
    try {
        reflectUtil.setFieldValue(thing, "id", "uhohes");
        fail("Should have thrown exception");
    } catch (FieldnameNotFoundException e) {
        assertNotNull(e.getMessage());
    }
}
 
Example #25
Source File: DeveloperHelperServiceMock.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T getConfigurationSetting(String settingName, T defaultValue) {
    T value = null;
    Object o = settings.get(settingName);
    if (defaultValue == null) {
        value = (T) o;
    } else {
        ReflectUtils.getInstance().convert( settings.get(settingName), defaultValue.getClass());
    }
    return value;
}
 
Example #26
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#unCapitalize(java.lang.String)}.
 */
public void testUnCapitalize() {
    assertTrue( ReflectUtils.unCapitalize("lower").equals("lower") );
    assertTrue( ReflectUtils.unCapitalize("UPPER").equals("uPPER") );
    assertTrue( ReflectUtils.unCapitalize("MyStuff").equals("myStuff") );
    assertTrue( ReflectUtils.unCapitalize("myStuff").equals("myStuff") );
    assertTrue( ReflectUtils.unCapitalize("").equals("") );
    assertTrue( ReflectUtils.unCapitalize("M").equals("m") );
}
 
Example #27
Source File: EntityDataUtils.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Gets the fieldname of the identifier field for an entity class type
 * @param type any entity class type
 * @return the name of the identifier field for this entity OR null if it cannot be determined
 */
public static String getEntityIdField(Class<?> type) {
    String entityIdField = ReflectUtils.getInstance().getFieldNameWithAnnotation(type, EntityId.class);
    if (entityIdField == null) {
        try {
            ReflectUtils.getInstance().getFieldType(type, "id");
            entityIdField = "id";
        } catch (FieldnameNotFoundException e) {
            entityIdField = null;
        }
    }
    return entityIdField;
}
 
Example #28
Source File: EntityDataUtils.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Gets the id field value from an entity if possible
 * @param entity any entity object
 * @return the id value OR null if it cannot be found
 */
public static String getEntityId(Object entity) {
    String entityId = null;
    try {
        entityId = ReflectUtils.getInstance().getFieldValueAsString(entity, "entityId", EntityId.class);
    } catch (FieldnameNotFoundException e) {
        try {
            // try just id only as well
            entityId = ReflectUtils.getInstance().getFieldValueAsString(entity, "id", null);
        } catch (FieldnameNotFoundException e1) {
            entityId = null;
        }
    }
    return entityId;
}
 
Example #29
Source File: ReflectUtilTest.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Test method for {@link org.sakaiproject.entitybroker.util.reflect.ReflectUtil#copy(java.lang.Object, java.lang.Object, int, java.lang.String[], boolean)}.
 */
public void testCopy() {
    ReflectUtils reflectUtil = ReflectUtils.getInstance();

    TestBean orig = new TestBean();
    orig.setMyInt(100);
    orig.setMyString("1000");
    TestBean dest = new TestBean();
    assertNotSame(orig.getMyInt(), dest.getMyInt());
    assertNotSame(orig.getMyString(), dest.getMyString());
    reflectUtil.copy(orig, dest, 0, null, false);
    assertNotNull(dest);
    assertEquals(orig.getMyInt(), dest.getMyInt());
    assertEquals(orig.getMyString(), dest.getMyString());

    dest = new TestBean();
    reflectUtil.copy(orig, dest, 0, new String[] {"myInt"}, false);
    assertNotNull(dest);
    assertNotSame(orig.getMyInt(), dest.getMyInt());
    assertEquals(orig.getMyString(), dest.getMyString());

    dest = new TestBean();
    reflectUtil.copy(orig, dest, 5, null, true);
    assertNotNull(dest);
    assertEquals(orig.getMyInt(), dest.getMyInt());
    assertEquals(orig.getMyString(), dest.getMyString());
}
 
Example #30
Source File: EntityProviderManagerImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Get the capabilities implemented by this provider
 * 
 * @param provider
 * @return
 */
@SuppressWarnings("unchecked")
protected static List<Class<? extends EntityProvider>> extractCapabilities(EntityProvider provider) {
    List<Class<?>> superclasses = ReflectUtils.getSuperclasses(provider.getClass());
    Set<Class<? extends EntityProvider>> capabilities = new HashSet<Class<? extends EntityProvider>>();

    for (Class<?> superclazz : superclasses) {
        if (superclazz.isInterface() && EntityProvider.class.isAssignableFrom(superclazz)) {
            capabilities.add((Class<? extends EntityProvider>) superclazz);
        }
    }
    return new ArrayList<Class<? extends EntityProvider>>(capabilities);
}