List of usage examples for org.apache.commons.lang3 ClassUtils isAssignable
public static boolean isAssignable(final Class<?> cls, final Class<?> toClass)
Checks if one Class can be assigned to a variable of another Class .
Unlike the Class#isAssignableFrom(java.lang.Class) method, this method takes into account widenings of primitive classes and null s.
Primitive widenings allow an int to be assigned to a long, float or double.
From source file:org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQueryCreator.java
protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID> criteria, Part part, Iterator<Object> iterator) { if (part.shouldIgnoreCase().equals(IgnoreCaseType.ALWAYS)) throw new UnsupportedOperationException("Case insensitivity not supported"); Class<?> leafNodePropertyType = part.getProperty().getLeafProperty().getType(); PropertyPath leafNodePropertyPath = part.getProperty().getLeafProperty(); String leafNodePropertyName = leafNodePropertyPath.toDotPath(); if (leafNodePropertyName.indexOf(".") != -1) { int index = leafNodePropertyName.lastIndexOf("."); leafNodePropertyName = leafNodePropertyName.substring(index); }//w w w . ja v a 2 s .c om switch (part.getType()) { case IN: Object in = iterator.next(); Assert.notNull(in, "Creating conditions on null parameters not supported: please specify a value for '" + leafNodePropertyName + "'"); boolean isIterable = ClassUtils.isAssignable(in.getClass(), Iterable.class); boolean isArray = ObjectUtils.isArray(in); Assert.isTrue(isIterable || isArray, "In criteria can only operate with Iterable or Array parameters"); Iterable<?> iterable = isIterable ? ((Iterable<?>) in) : Arrays.asList(ObjectUtils.toObjectArray(in)); return criteria.withPropertyIn(leafNodePropertyName, iterable, leafNodePropertyType); case CONTAINING: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.CONTAINS, iterator.next(), leafNodePropertyType); case STARTING_WITH: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.BEGINS_WITH, iterator.next(), leafNodePropertyType); case BETWEEN: Object first = iterator.next(); Object second = iterator.next(); return criteria.withPropertyBetween(leafNodePropertyName, first, second, leafNodePropertyType); case AFTER: case GREATER_THAN: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GT, iterator.next(), leafNodePropertyType); case BEFORE: case LESS_THAN: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LT, iterator.next(), leafNodePropertyType); case GREATER_THAN_EQUAL: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.GE, iterator.next(), leafNodePropertyType); case LESS_THAN_EQUAL: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.LE, iterator.next(), leafNodePropertyType); case IS_NULL: return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NULL); case IS_NOT_NULL: return criteria.withNoValuedCriteria(leafNodePropertyName, ComparisonOperator.NOT_NULL); case TRUE: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.TRUE, leafNodePropertyType); case FALSE: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.EQ, Boolean.FALSE, leafNodePropertyType); case SIMPLE_PROPERTY: return criteria.withPropertyEquals(leafNodePropertyName, iterator.next(), leafNodePropertyType); case NEGATING_SIMPLE_PROPERTY: return criteria.withSingleValueCriteria(leafNodePropertyName, ComparisonOperator.NE, iterator.next(), leafNodePropertyType); default: throw new IllegalArgumentException("Unsupported keyword " + part.getType()); } }
From source file:org.socialsignin.spring.data.dynamodb.repository.query.DynamoDBQueryCreator.java
protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID> criteria, Part part, Iterator<Object> iterator) { if (part.shouldIgnoreCase().equals(IgnoreCaseType.ALWAYS)) throw new UnsupportedOperationException("Case insensitivity not supported"); Class<?> propertyType = part.getProperty().getType(); switch (part.getType()) { case IN://from ww w . j a v a2 s .co m Object in = iterator.next(); Assert.notNull(in, "Creating conditions on null parameters not supported: please specify a value for '" + part.getProperty().getSegment() + "'"); boolean isIterable = ClassUtils.isAssignable(in.getClass(), Iterable.class); boolean isArray = ObjectUtils.isArray(in); Assert.isTrue(isIterable || isArray, "In criteria can only operate with Iterable or Array parameters"); Iterable<?> iterable = isIterable ? ((Iterable<?>) in) : Arrays.asList(ObjectUtils.toObjectArray(in)); return criteria.withPropertyIn(part.getProperty().getSegment(), iterable, propertyType); case CONTAINING: return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.CONTAINS, iterator.next(), propertyType); case STARTING_WITH: return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.BEGINS_WITH, iterator.next(), propertyType); case BETWEEN: Object first = iterator.next(); Object second = iterator.next(); return criteria.withPropertyBetween(part.getProperty().getSegment(), first, second, propertyType); case AFTER: case GREATER_THAN: return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.GT, iterator.next(), propertyType); case BEFORE: case LESS_THAN: return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.LT, iterator.next(), propertyType); case GREATER_THAN_EQUAL: return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.GE, iterator.next(), propertyType); case LESS_THAN_EQUAL: return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.LE, iterator.next(), propertyType); case IS_NULL: return criteria.withNoValuedCriteria(part.getProperty().getSegment(), ComparisonOperator.NULL); case IS_NOT_NULL: return criteria.withNoValuedCriteria(part.getProperty().getSegment(), ComparisonOperator.NOT_NULL); case TRUE: return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.EQ, Boolean.TRUE, propertyType); case FALSE: return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.EQ, Boolean.FALSE, propertyType); case SIMPLE_PROPERTY: return criteria.withPropertyEquals(part.getProperty().getSegment(), iterator.next(), propertyType); case NEGATING_SIMPLE_PROPERTY: return criteria.withSingleValueCriteria(part.getProperty().getSegment(), ComparisonOperator.NE, iterator.next(), propertyType); default: throw new IllegalArgumentException("Unsupported keyword " + part.getType()); } }
From source file:org.xmlsh.util.JavaUtils.java
public static boolean isCollectionOf(Object obj, Class<?> c) { if (obj instanceof Collection) { Collection col = (Collection<?>) obj; if (col.isEmpty()) return true; // sure WTF return ClassUtils.isAssignable(col.iterator().next().getClass(), c); }/*from w w w . j a va2 s . c o m*/ return false; }
From source file:software.coolstuff.springframework.owncloud.service.impl.local.OwncloudLocalResourceImpl.java
@Override public boolean equals(Object obj) { if (obj == null || !ClassUtils.isAssignable(obj.getClass(), OwncloudResource.class)) { return false; }// w w w . j a v a 2 s .co m OwncloudResource otherObj = (OwncloudResource) obj; return new EqualsBuilder().append(eTag, otherObj.getETag()).append(href, otherObj.getHref()).isEquals(); }
From source file:software.coolstuff.springframework.owncloud.service.impl.OwncloudUtils.java
/** * Checks, if the given Authentication Object is authenticated * by the Owncloud{Rest|Local}AuthenticationProvider * @param authentication Authentication Object * @return is authenticated by the Owncloud{Rest|Local}AuthenticationProvider *//*from w w w.j a va 2 s . c om*/ public static boolean isValidAuthentication(Authentication authentication) { if (authentication == null) { return false; } // if UserDetails are set then it must be of Class OwncloudUserDetails if (authentication.getDetails() != null && !ClassUtils.isAssignable(authentication.getPrincipal().getClass(), OwncloudUserDetails.class)) { return false; } if (authentication.getCredentials() != null) { // if Credentials are available then these must be of Class CharSequence and not empty return CharSequence.class.isAssignableFrom(authentication.getCredentials().getClass()) && StringUtils.isNotBlank((CharSequence) authentication.getCredentials()); } if (authentication.getPrincipal() != null) { // Password of the UserDetails must not be empty UserDetails userDetails = (UserDetails) authentication.getPrincipal(); return StringUtils.isNotBlank(userDetails.getPassword()); } return false; }
From source file:software.coolstuff.springframework.owncloud.service.impl.OwncloudUtils.java
/** * Convert a OwncloudResource to a OwncloudFileResource. * @param owncloudResource OwncloudResource * @return converted to OwncloudFileResource * @throws OwncloudNoFileResourceException if the OwncloudResource is not convertable to a OwncloudFileResource *///from ww w . ja va 2 s . c o m public static OwncloudFileResource toOwncloudFileResource(OwncloudResource owncloudResource) throws OwncloudNoFileResourceException { if (owncloudResource == null) { return null; } if (isDirectory(owncloudResource) || !ClassUtils.isAssignable(owncloudResource.getClass(), OwncloudFileResource.class)) { throw new OwncloudNoFileResourceException(owncloudResource.getHref()); } return (OwncloudFileResource) owncloudResource; }
From source file:yoyo.framework.standard.shared.commons.lang.ClassUtilsTest.java
@Test public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException { List<Class<?>> classes = new ArrayList<Class<?>>() { {/*w ww. ja v a2 s .com*/ add(Integer.class); add(Long.class); } }; assertThat(ClassUtils.convertClassesToClassNames(classes).toArray(), is(new Object[] { "java.lang.Integer", "java.lang.Long" })); List<String> classNames = new ArrayList<String>() { { add("java.lang.Integer"); add("java.lang.Long"); } }; assertThat(ClassUtils.convertClassNamesToClasses(classNames).toArray(), is(classes.toArray())); assertThat(ClassUtils.getAllInterfaces(String.class).toArray(), is(new Object[] { java.io.Serializable.class, java.lang.Comparable.class, java.lang.CharSequence.class })); assertThat(ClassUtils.getAllSuperclasses(HashMap.class).toArray(), is(new Object[] { java.util.AbstractMap.class, java.lang.Object.class })); assertThat(ClassUtils.getClass("java.lang.String").toString(), is("class java.lang.String")); assertThat(ClassUtils.getPackageCanonicalName(String.class), is("java.lang")); assertThat(ClassUtils.getPackageName(String.class), is("java.lang")); assertThat(ClassUtils.getPublicMethod(String.class, "length", new Class[] {}), is(not(nullValue()))); assertThat(ClassUtils.getShortCanonicalName(String.class), is("String")); assertThat(ClassUtils.getShortClassName(String.class), is("String")); assertThat(ClassUtils.getSimpleName(String.class), is("String")); assertThat(ClassUtils.isAssignable(String.class, Object.class), is(true)); assertThat(ClassUtils.isInnerClass(Foo.class), is(true)); assertThat(ClassUtils.isInnerClass(String.class), is(false)); assertThat(ClassUtils.isPrimitiveOrWrapper(Integer.class), is(true)); assertThat(ClassUtils.isPrimitiveOrWrapper(String.class), is(false)); assertThat(ClassUtils.isPrimitiveWrapper(Integer.class), is(true)); assertThat(ClassUtils.isPrimitiveWrapper(String.class), is(false)); assertThat(ClassUtils.primitivesToWrappers(new Class[] { Integer.class, Long.class }), is(not(nullValue()))); assertThat(ClassUtils.primitiveToWrapper(Integer.class), is(not(nullValue()))); assertThat(ClassUtils.toClass(1L, 2L), is(not(nullValue()))); assertThat(ClassUtils.wrappersToPrimitives(new Class[] { Integer.class, Long.class }), is(not(nullValue()))); assertThat(ClassUtils.wrapperToPrimitive(Integer.class), is(not(nullValue()))); }