List of usage examples for java.lang IllegalAccessException toString
public String toString()
From source file:io.smartspaces.activity.annotation.StandardConfigurationPropertyAnnotationProcessor.java
/** * Injects config parameters into a given field of a given object, if the * field is marked with// www. jav a 2s . c o m * {@link io.smartspaces.activity.annotation.ConfigurationProperty}. * * @param obj * object that contains the field into which config parameters will * be injected * @param field * field into which a config parameter will be injected * @param errors * list container to accumulate errors * @param successes * list container to accumulate successes */ private void processField(Object obj, Field field, List<String> errors, List<String> successes) { ConfigurationProperty annotation = field.getAnnotation(ConfigurationProperty.class); if (annotation == null) { return; } int initialErrorSize = errors.size(); String fieldName = field.getName(); if (Modifier.isFinal(field.getModifiers())) { errors.add(String.format("Field '%s' is marked final and may have unpredictable effects", fieldName)); } String property = annotation.name(); if (property == null || property.isEmpty()) { property = annotation.value(); } property = property.trim(); if (property.isEmpty()) { errors.add(String.format("Field '%s' has property name that is all white space or empty", fieldName)); } boolean required = annotation.required(); String delimiter = annotation.delimiter(); boolean accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); } try { Object value = null; Class<?> type = field.getType(); if (required) { Object defaultValue = field.get(obj); boolean valueIsNotDefault = !Objects.equal(defaultValue, Defaults.defaultValue(type)); if (valueIsNotDefault) { errors.add(String.format( "Field '%s' into which a required property '%s' " + "is to be injected already has a value: '%s', set 'required = false', " + "or set the value of the property in the configuration " + "(do not initialize the field directly).", fieldName, property, defaultValue)); } if (!configuration.containsProperty(property) && !property.isEmpty()) { errors.add(String.format("Field '%s' does not contain required property '%s'", fieldName, property)); } } // If value is required but not present, an error has already been // registered. if (type == int.class || type == Integer.class) { value = configuration.getPropertyInteger(property, null); } else if (type == long.class || type == Long.class) { value = configuration.getPropertyLong(property, null); } else if (type == double.class || type == Double.class) { value = configuration.getPropertyDouble(property, null); } else if (type == boolean.class || type == Boolean.class) { value = configuration.getPropertyBoolean(property, null); } else if (type == String.class) { value = configuration.getPropertyString(property); } else if (type.isAssignableFrom(List.class)) { value = configuration.getPropertyStringList(property, delimiter); } else if (type.isAssignableFrom(Set.class)) { value = configuration.getPropertyStringSet(property, delimiter); } else { errors.add(String.format(String.format("Field '%s' has unsupported type '%s'", fieldName, tryGetValueForErrorMessage(property)))); } if (errors.size() != initialErrorSize) { return; } String header = "@" + ConfigurationProperty.class.getSimpleName(); if (value != null) { successes.add(String.format("%s field '%s' injected property '%s' with value '%s'", header, fieldName, property, value)); field.set(obj, value); } else { successes.add(String.format("%s field '%s' has no value from property '%s', skipping", header, fieldName, property)); } } catch (IllegalAccessException e) { errors.add(String.format("Field '%s' can not be accessed: %s", fieldName, e.toString())); } catch (Exception e) { errors.add(String.format("Field '%s' with property '%s' encountered error: %s", fieldName, tryGetValueForErrorMessage(property), e.toString())); } finally { if (!accessible) { field.setAccessible(false); } } }
From source file:com.athena.meerkat.controller.web.tomcat.services.TomcatInstanceService.java
public void saveTomcatConfig(TomcatInstance tomcat, DomainTomcatConfiguration conf) { DomainTomcatConfiguration domainConfig = domainService.getTomcatConfig(tomcat.getDomainId()); if (domainConfig == null) { return;/*ww w . j a va 2s . co m*/ } List<TomcatInstConfig> tomcatConfs = new ArrayList<>(); if (conf != null) { // get all fields in domain tomcat config Field[] fields = DomainTomcatConfiguration.class.getDeclaredFields(); for (Field field : fields) { String name = field.getName(); // check whether the config property is exist in read-only // conf // list if (Arrays.asList(MeerkatConstants.TOMCAT_INSTANCE_CONFIGS_CUSTOM).contains(name)) { String value = ""; try { field.setAccessible(true); value = field.get(conf).toString(); //check whether the value is modified if (!field.get(domainConfig).toString().equals(value)) { TomcatInstConfig tomcatConf = tomcatInstConfigRepo .findByConfigNameAndTomcatInstance(name, tomcat); if (tomcatConf != null) { tomcatConf.setConfigValue(value); } else { tomcatConf = new TomcatInstConfig(tomcat, name, value); } tomcatConfs.add(tomcatConf); } field.setAccessible(false); } catch (IllegalAccessException e) { LOGGER.error(e.toString(), e); throw new RuntimeException(e); } } } tomcatInstConfigRepo.save(tomcatConfs); } }
From source file:com.redhat.rhn.common.util.CSVWriter.java
/** * Util function to get the value for the current row/column in the List. */// w w w .ja v a 2 s . co m private Object getObjectValue(Object row, String columnKey) { if (row instanceof Map) { Map rowmap = (Map) row; return rowmap.get(columnKey); } else if (row instanceof BaseDto) { String ovalue = null; try { ovalue = BeanUtils.getProperty(row, columnKey); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Can't access method in DTO: get" + columnKey + "(), IllegalAccessException:" + e.toString()); } catch (InvocationTargetException e) { throw new IllegalArgumentException("Can't access method in DTO: get" + columnKey + "(), InvocationTargetException:" + e.toString()); } catch (NoSuchMethodException e) { throw new IllegalArgumentException("Can't call method in DTO class: " + row.getClass().getName() + "." + "get" + columnKey + "(), NoSuchMethodException: " + e.toString()); } catch (NestedNullException e) { return null; } return ovalue; } return null; }
From source file:RevEngAPI.java
/** Generate a .java file for the outline of the given class. */ public void doClass(Class c) throws IOException { className = c.getName();/*from ww w . jav a 2 s . co m*/ // pre-compute offset for stripping package name classNameOffset = className.lastIndexOf('.') + 1; // Inner class if (className.indexOf('$') != -1) return; // get name, as String, with . changed to / String slashName = className.replace('.', '/'); String fileName = slashName + ".java"; System.out.println(className + " --> " + fileName); String dirName = slashName.substring(0, slashName.lastIndexOf("/")); new File(dirName).mkdirs(); // create the file. PrintWriter out = new PrintWriter(new FileWriter(fileName)); out.println("// Generated by RevEngAPI for class " + className); // If in a package, say so. Package pkg; if ((pkg = c.getPackage()) != null) { out.println("package " + pkg.getName() + ';'); out.println(); } // print class header int cMods = c.getModifiers(); printMods(cMods, out); out.print("class "); out.print(trim(c.getName())); out.print(' '); // XXX get superclass out.println('{'); // print constructors Constructor[] ctors = c.getDeclaredConstructors(); for (int i = 0; i < ctors.length; i++) { if (i == 0) { out.println(); out.println("\t// Constructors"); } Constructor cons = ctors[i]; int mods = cons.getModifiers(); if (Modifier.isPrivate(mods)) continue; out.print('\t'); printMods(mods, out); out.print(trim(cons.getName()) + "("); Class[] classes = cons.getParameterTypes(); for (int j = 0; j < classes.length; j++) { if (j > 0) out.print(", "); out.print(trim(classes[j].getName()) + ' ' + mkName(PREFIX_ARG, j)); } out.println(") {"); out.print("\t}"); } // print method names Method[] mems = c.getDeclaredMethods(); for (int i = 0; i < mems.length; i++) { if (i == 0) { out.println(); out.println("\t// Methods"); } Method m = mems[i]; if (m.getName().startsWith("access$")) continue; int mods = m.getModifiers(); if (Modifier.isPrivate(mods)) continue; out.print('\t'); printMods(mods, out); out.print(m.getReturnType()); out.print(' '); out.print(trim(m.getName()) + "("); Class[] classes = m.getParameterTypes(); for (int j = 0; j < classes.length; j++) { if (j > 0) out.print(", "); out.print(trim(classes[j].getName()) + ' ' + mkName(PREFIX_ARG, j)); } out.println(") {"); out.println("\treturn " + defaultValue(m.getReturnType()) + ';'); out.println("\t}"); } // print fields Field[] flds = c.getDeclaredFields(); for (int i = 0; i < flds.length; i++) { if (i == 0) { out.println(); out.println("\t// Fields"); } Field f = flds[i]; int mods = f.getModifiers(); if (Modifier.isPrivate(mods)) continue; out.print('\t'); printMods(mods, out); out.print(trim(f.getType().getName())); out.print(' '); out.print(f.getName()); if (Modifier.isFinal(mods)) { try { out.print(" = " + f.get(null)); } catch (IllegalAccessException ex) { out.print("; // " + ex.toString()); } } out.println(';'); } out.println("}"); //out.flush(); out.close(); }
From source file:gov.nih.nci.caarray.plugins.agilent.AgilentRawTextDataHandlerTest.java
private void checkColumnLength(HybridizationData hybridizationData, AgilentTextQuantitationType quantitationType, int expectedColumnLength) { try {/*w ww.j a v a2 s . c o m*/ final AbstractDataColumn column = hybridizationData.getColumn(quantitationType); assertNotNull(column); final Object array = PropertyUtils.getProperty(column, "values"); assertEquals(expectedColumnLength, Array.getLength(array)); } catch (final IllegalAccessException ex) { ex.printStackTrace(); fail(ex.toString()); } catch (final InvocationTargetException ex) { ex.printStackTrace(); fail(ex.toString()); } catch (final NoSuchMethodException ex) { ex.printStackTrace(); fail(ex.toString()); } }
From source file:us.mn.state.health.lims.taglib.OptionsCollectionTag.java
protected Object getProperty(Object bean, String property) throws JspException { // Get the value for this option Object beanValue = ""; try {// w ww . j a v a 2 s .co m beanValue = PropertyUtils.getProperty(bean, property); if (beanValue == null) { beanValue = ""; } } catch (IllegalAccessException e) { //bugzilla 2154 LogEvent.logError("OptionsCollectionTag", "getProperty()", e.toString()); JspException jspe = new JspException(messages.getMessage("getter.access", value, bean)); TagUtils.getInstance().saveException(pageContext, jspe); throw jspe; } catch (InvocationTargetException e) { //bugzilla 2154 LogEvent.logError("OptionsCollectionTag", "getProperty()", e.toString()); Throwable t = e.getTargetException(); JspException jspe = new JspException(messages.getMessage("getter.result", value, t.toString())); TagUtils.getInstance().saveException(pageContext, jspe); throw jspe; } catch (NoSuchMethodException e) { //bugzilla 2154 LogEvent.logError("OptionsCollectionTag", "getProperty()", e.toString()); JspException jspe = new JspException(messages.getMessage("getter.method", value, bean)); TagUtils.getInstance().saveException(pageContext, jspe); throw jspe; } if (!StringUtil.isNullorNill(maxLength)) { try { int max = Integer.parseInt(maxLength); String beanVal = beanValue.toString(); if (beanVal.length() > max) { beanValue = beanVal.substring(0, max); } } catch (Exception e) { //bugzilla 2154 LogEvent.logError("OptionsCollectionTag", "getProperty()", e.toString()); // don't process maxLength if there is a problem } } return beanValue; }
From source file:org.tynamo.descriptor.PropertyDescriptorTest.java
@Test public void testCloneWidthExtensions2() throws Exception { String testExtension = "testExtension"; DescriptorExtension descriptorExtension = new DescriptorExtension() { private final Logger logger = LoggerFactory.getLogger(DescriptorExtension.class); private String name; private Class propertyType; private Class beanType; private TynamoPropertyDescriptor propertyDescriptor = null; private boolean hidden = true; private boolean searchable = true; @Override/*w w w .jav a 2 s . c o m*/ public Object clone() { return new Object(); } public void copyFrom(Descriptor descriptor) { try { BeanUtils.copyProperties(this, (ObjectReferenceDescriptor) descriptor); } catch (IllegalAccessException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (InvocationTargetException e) { logger.error(e.getMessage()); e.printStackTrace(); } catch (Exception e) { logger.error(e.toString()); e.printStackTrace(); } } /** * Interface Implementation */ public String findAddExpression() { return getExpression(""); } public String findRemoveExpression() { return getExpression(""); } public String getExpression(String method) { return getName(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Class getBeanType() { return beanType; } public void setBeanType(Class beanType) { this.beanType = beanType; } public Class getPropertyType() { return propertyType; } public void setPropertyType(Class propertyType) { this.propertyType = propertyType; } public boolean isSearchable() { return searchable; } public void setSearchable(boolean searchable) { this.searchable = searchable; } public boolean isHidden() { return hidden; } public void setHidden(boolean hidden) { this.hidden = hidden; } public TynamoPropertyDescriptor getPropertyDescriptor() { return propertyDescriptor; } public void setPropertyDescriptor(TynamoPropertyDescriptor propertyDescriptor) { this.propertyDescriptor = propertyDescriptor; } }; TynamoPropertyDescriptorImpl descriptor1 = new TynamoPropertyDescriptorImpl(Foo.class, "foo", String.class); descriptor1.addExtension(testExtension, descriptorExtension); TynamoPropertyDescriptorImpl descriptor2 = (TynamoPropertyDescriptorImpl) descriptor1.clone(); assertTrue(descriptor2.supportsExtension(testExtension)); assertEquals(descriptorExtension, descriptor2.getExtension(testExtension)); }
From source file:net.lightbody.bmp.proxy.jetty.util.jmx.ModelMBeanImpl.java
public Object getAttribute(String name) throws AttributeNotFoundException, MBeanException, ReflectionException { if (log.isDebugEnabled()) log.debug("getAttribute " + name); Method getter = (Method) _getter.get(name); if (getter == null) throw new AttributeNotFoundException(name); try {/* w w w.j ava 2s. co m*/ Object o = _object; if (getter.getDeclaringClass().isInstance(this)) o = this; return getter.invoke(o, (java.lang.Object[]) null); } catch (IllegalAccessException e) { log.warn(LogSupport.EXCEPTION, e); throw new AttributeNotFoundException(e.toString()); } catch (InvocationTargetException e) { log.warn(LogSupport.EXCEPTION, e); throw new ReflectionException((Exception) e.getTargetException()); } }
From source file:net.lightbody.bmp.proxy.jetty.util.jmx.ModelMBeanImpl.java
public void setAttribute(Attribute attr) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException { if (attr == null) return;//from www .j av a2 s . c om if (log.isDebugEnabled()) log.debug("setAttribute " + attr.getName() + "=" + attr.getValue()); Method setter = (Method) _setter.get(attr.getName()); if (setter == null) throw new AttributeNotFoundException(attr.getName()); try { Object o = _object; if (setter.getDeclaringClass().isInstance(this)) o = this; setter.invoke(o, new Object[] { attr.getValue() }); } catch (IllegalAccessException e) { log.warn(LogSupport.EXCEPTION, e); throw new AttributeNotFoundException(e.toString()); } catch (InvocationTargetException e) { log.warn(LogSupport.EXCEPTION, e); throw new ReflectionException((Exception) e.getTargetException()); } }