use of java.beans.IntrospectionException in project goci by EBISPOT.
the class EmbeddableDocument method embed.
public void embed(Document document) {
try {
Set<String> excludeNames = new HashSet<>();
BeanInfo objectInfo = Introspector.getBeanInfo(Document.class);
for (PropertyDescriptor descriptor : objectInfo.getPropertyDescriptors()) {
excludeNames.add(descriptor.getName());
}
try {
BeanInfo docInfo = Introspector.getBeanInfo(document.getClass());
for (PropertyDescriptor descriptor : docInfo.getPropertyDescriptors()) {
if (!excludeNames.contains(descriptor.getName())) {
boolean isExcluded = false;
Method readMethod = descriptor.getReadMethod();
readMethod.setAccessible(true);
if (readMethod.isAnnotationPresent(NonEmbeddableField.class)) {
isExcluded = true;
} else {
try {
Field field = document.getClass().getDeclaredField(descriptor.getName());
field.setAccessible(true);
if (field.isAnnotationPresent(NonEmbeddableField.class)) {
isExcluded = true;
}
} catch (NoSuchFieldException e) {
// no field with this name, skip
}
}
if (!isExcluded) {
try {
// determine method to update this document with doc being embedded
Method propertyAdderMethod = findPropertyAdder(descriptor);
// invoke read method on passed document
Object fieldToEmbed = descriptor.getReadMethod().invoke(document);
propertyAdderMethod.invoke(this, fieldToEmbed);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new DocumentEmbeddingException("Failed to read property '" + descriptor.getName() + "'", e);
}
}
}
}
} catch (IntrospectionException e) {
throw new DocumentEmbeddingException("Failed to analyse document in preparation for embedding", e);
}
} catch (IntrospectionException e) {
throw new DocumentEmbeddingException("Failed to read Object.class when determining which properties to exclude", e);
}
}
use of java.beans.IntrospectionException in project goci by EBISPOT.
the class EmbeddableDocumentTest method testIntrospection.
@Test
public void testIntrospection() {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(studyDoc.getClass());
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
System.out.println("Display name: " + pd.getDisplayName());
System.out.println("Name: " + pd.getName());
System.out.println("Read method: " + pd.getReadMethod());
System.out.println("\t" + pd);
}
} catch (IntrospectionException e) {
e.printStackTrace();
fail();
}
}
use of java.beans.IntrospectionException in project jdk8u_jdk by JetBrains.
the class Test5102804 method getReference.
private static Reference getReference() {
try {
ClassLoader loader = new Loader();
Class type = Class.forName(BEAN_NAME, true, loader);
if (!type.getClassLoader().equals(loader)) {
throw new Error("Wrong class loader");
}
BeanInfo info = Introspector.getBeanInfo(type);
if (0 != info.getDefaultPropertyIndex()) {
throw new Error("Wrong bean info found");
}
return new WeakReference<Class>(type);
} catch (IntrospectionException exception) {
throw new Error("Introspection Error", exception);
} catch (ClassNotFoundException exception) {
throw new Error("Class Not Found", exception);
}
}
use of java.beans.IntrospectionException in project jdk8u_jdk by JetBrains.
the class Test4520754 method getBeanInfo.
private static BeanInfo getBeanInfo(Boolean mark, Class type) {
System.out.println("test=" + mark + " for " + type);
BeanInfo info;
try {
info = Introspector.getBeanInfo(type);
} catch (IntrospectionException exception) {
throw new Error("unexpected exception", exception);
}
if (info == null) {
throw new Error("could not find BeanInfo for " + type);
}
if (mark != info.getBeanDescriptor().getValue("test")) {
throw new Error("could not find marked BeanInfo for " + type);
}
return info;
}
use of java.beans.IntrospectionException in project opennms by OpenNMS.
the class OnmsRestService method applyQueryFilters.
protected static void applyQueryFilters(final MultivaluedMap<String, String> p, final CriteriaBuilder builder, final Integer defaultLimit) {
final MultivaluedMap<String, String> params = new MultivaluedMapImpl();
params.putAll(p);
builder.distinct();
builder.limit(defaultLimit);
if (params.containsKey("limit")) {
builder.limit(Integer.valueOf(params.getFirst("limit")));
params.remove("limit");
}
if (params.containsKey("offset")) {
builder.offset(Integer.valueOf(params.getFirst("offset")));
params.remove("offset");
}
if (params.containsKey("orderBy")) {
builder.clearOrder();
builder.orderBy(params.getFirst("orderBy"));
params.remove("orderBy");
if (params.containsKey("order")) {
if ("desc".equalsIgnoreCase(params.getFirst("order"))) {
builder.desc();
} else {
builder.asc();
}
params.remove("order");
}
}
final String query = removeParameter(params, "query");
if (query != null)
builder.sql(query);
final String matchType;
final String match = removeParameter(params, "match");
if (match == null) {
matchType = "all";
} else {
matchType = match;
}
builder.match(matchType);
final Class<?> criteriaClass = builder.toCriteria().getCriteriaClass();
final BeanWrapper wrapper = getBeanWrapperForClass(criteriaClass);
final String comparatorParam = removeParameter(params, "comparator", "eq").toLowerCase();
final Criteria currentCriteria = builder.toCriteria();
for (final String key : params.keySet()) {
for (final String paramValue : params.get(key)) {
// the actual implementation com.sun.jersey.core.util.MultivaluedMapImpl returns a String, so this is fine in some way ...
if ("null".equalsIgnoreCase(paramValue)) {
builder.isNull(key);
} else if ("notnull".equalsIgnoreCase(paramValue)) {
builder.isNotNull(key);
} else {
Object value;
Class<?> type = Object.class;
try {
type = currentCriteria.getType(key);
} catch (final IntrospectionException e) {
LOG.debug("Unable to determine type for key {}", key);
}
if (type == null) {
type = Object.class;
}
LOG.warn("comparator = {}, key = {}, propertyType = {}", comparatorParam, key, type);
if (comparatorParam.equals("contains") || comparatorParam.equals("iplike") || comparatorParam.equals("ilike") || comparatorParam.equals("like")) {
value = paramValue;
} else {
LOG.debug("convertIfNecessary({}, {})", key, paramValue);
try {
value = wrapper.convertIfNecessary(paramValue, type);
} catch (final Throwable t) {
LOG.debug("failed to introspect (key = {}, value = {})", key, paramValue, t);
value = paramValue;
}
}
try {
final Method m = builder.getClass().getMethod(comparatorParam, String.class, Object.class);
m.invoke(builder, new Object[] { key, value });
} catch (final Throwable t) {
LOG.warn("Unable to find method for comparator: {}, key: {}, value: {}", comparatorParam, key, value, t);
}
}
}
}
}
Aggregations