Skip to content

Commit

Permalink
Port to Java 1.4 Throwable APIs (!)
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Oct 10, 2024
1 parent 6627fad commit a4d861f
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 476 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<action type="fix" issue="BEANUTILS-541" dev="ggregory" due-to="Sergey Chernov">FluentPropertyBeanIntrospector caches corrupted writeMethod (1.x backport) #69.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Replace internal use of Locale.ENGLISH with Locale.ROOT.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Replace Maven CLIRR plugin with JApiCmp.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory">Port to Java 1.4 Throwable APIs (!).</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 47 to 73.</action>
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump Java requirement from Java 6 to 8.</action>
Expand Down
50 changes: 16 additions & 34 deletions src/main/java/org/apache/commons/beanutils/BeanMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,15 @@ public BeanMap(final Object bean) {
*/
@Override
public void clear() {
if(bean == null) {
if (bean == null) {
return;
}

Class<? extends Object> beanClass = null;
try {
beanClass = bean.getClass();
bean = beanClass.getConstructor().newInstance();
}
catch (final Exception e) {
final UnsupportedOperationException uoe =
new UnsupportedOperationException("Could not create new instance of class: " + beanClass);
BeanUtils.initCause(uoe, e);
throw uoe;
} catch (final Exception e) {
throw new UnsupportedOperationException("Could not create new instance of class: " + beanClass, e);
}
}

Expand Down Expand Up @@ -281,48 +276,39 @@ public void clear() {
*/
@Override
public Object clone() throws CloneNotSupportedException {
final BeanMap newMap = (BeanMap)super.clone();

if(bean == null) {
// no bean, just an empty bean map at the moment. return a newly
final BeanMap newMap = (BeanMap) super.clone();
if (bean == null) {
// no bean, just an empty bean map at the moment. return a newly
// cloned and empty bean map.
return newMap;
}

Object newBean = null;
final Class<? extends Object> beanClass = bean.getClass(); // Cannot throw Exception
try {
newBean = beanClass.getConstructor().newInstance();
} catch (final Exception e) {
// unable to instantiate
final CloneNotSupportedException cnse = new CloneNotSupportedException
("Unable to instantiate the underlying bean \"" +
beanClass.getName() + "\": " + e);
BeanUtils.initCause(cnse, e);
final CloneNotSupportedException cnse = new CloneNotSupportedException(
"Unable to instantiate the underlying bean \"" + beanClass.getName() + "\": " + e);
cnse.initCause(e);
throw cnse;
}

try {
newMap.setBean(newBean);
} catch (final Exception exception) {
final CloneNotSupportedException cnse = new CloneNotSupportedException
("Unable to set bean in the cloned bean map: " +
exception);
BeanUtils.initCause(cnse, exception);
final CloneNotSupportedException cnse = new CloneNotSupportedException("Unable to set bean in the cloned bean map: " + exception);
cnse.initCause(exception);
throw cnse;
}

try {
for (final Object key : readMethods.keySet()) {
if(getWriteMethod(key) != null) {
if (getWriteMethod(key) != null) {
newMap.put(key, get(key));
}
}
} catch (final Exception exception) {
final CloneNotSupportedException cnse = new CloneNotSupportedException
("Unable to copy bean values to cloned bean map: " +
exception);
BeanUtils.initCause(cnse, exception);
final CloneNotSupportedException cnse = new CloneNotSupportedException("Unable to copy bean values to cloned bean map: " + exception);
cnse.initCause(exception);
throw cnse;
}

Expand Down Expand Up @@ -446,9 +432,7 @@ protected Object[] createWriteMethodArguments( final Method method, Object value
return new Object[] { value };
}
catch ( final InvocationTargetException | InstantiationException e ) {
final IllegalArgumentException iae = new IllegalArgumentException(e.getMessage());
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(e.getMessage(), e);
}
}

Expand Down Expand Up @@ -750,9 +734,7 @@ public Object put(final Object name, final Object value) throws IllegalArgumentE
firePropertyChange( name, oldValue, newValue );
}
catch ( final InvocationTargetException | IllegalAccessException e ) {
final IllegalArgumentException iae = new IllegalArgumentException(e.getMessage());
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(e.getMessage(), e);
}
return oldValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,26 +165,19 @@ public void execute(final Object object) {
final String errorMsg = "Unable to execute Closure. Null value encountered in property path...";

if (!ignoreNull) {
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg, e);
throw iae;
}
log.warn("WARNING: " + errorMsg + e);
} catch (final IllegalAccessException e) {
final String errorMsg = "Unable to access the property provided.";
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
} catch (final InvocationTargetException e) {
final String errorMsg = "Exception occurred in property's getter";
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
} catch (final NoSuchMethodException e) {
final String errorMsg = "Property not found";
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,26 +200,18 @@ public boolean evaluate(final Object object) {
final String errorMsg = "Problem during evaluation. Null value encountered in property path...";

if (!ignoreNull) {
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
}
log.warn("WARNING: " + errorMsg + e);
} catch (final IllegalAccessException e) {
final String errorMsg = "Unable to access the property provided.";
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
} catch (final InvocationTargetException e) {
final String errorMsg = "Exception occurred in property's getter";
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
} catch (final NoSuchMethodException e) {
final String errorMsg = "Property not found.";
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
}

return evaluation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,27 +178,19 @@ public Object transform(final Object object) {
final String errorMsg = "Problem during transformation. Null value encountered in property path...";

if (!ignoreNull) {
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
}
log.warn("WARNING: " + errorMsg + e);
} catch (final IllegalAccessException e) {
final String errorMsg = "Unable to access the property provided.";
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
} catch (final InvocationTargetException e) {
final String errorMsg = "Exception occurred in property's getter";
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
} catch (final NoSuchMethodException e) {
final String errorMsg = "No property found for name [" +
propertyName + "]";
final IllegalArgumentException iae = new IllegalArgumentException(errorMsg);
BeanUtils.initCause(iae, e);
throw iae;
throw new IllegalArgumentException(errorMsg, e);
}

return propertyValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ public class ConversionException extends RuntimeException {
private static final long serialVersionUID = 1L;

/**
* The root cause of this <code>ConversionException</code>, compatible with
* JDK 1.4's extensions to <code>java.lang.Throwable</code>.
*/
protected Throwable cause = null;

/**
* Construct a new exception with the specified message.
* Constructs a new exception with the specified message.
*
* @param message The message describing this exception
*/
Expand All @@ -43,34 +37,22 @@ public ConversionException(final String message) {
}

/**
* Construct a new exception with the specified message and root cause.
* Constructs a new exception with the specified message and root cause.
*
* @param message The message describing this exception
* @param cause The root cause of this exception
*/
public ConversionException(final String message, final Throwable cause) {
super(message);
this.cause = cause;
super(message, cause);
}

/**
* Construct a new exception with the specified root cause.
* Constructs a new exception with the specified root cause.
*
* @param cause The root cause of this exception
*/
public ConversionException(final Throwable cause) {
super(cause.getMessage());
this.cause = cause;

}

/**
* Return the root cause of this conversion exception.
* @return the root cause of this conversion exception
*/
@Override
public Throwable getCause() {
return this.cause;
super(cause);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,13 @@ public ConvertingWrapDynaBean(final Object instance) {
*/
@Override
public void set(final String name, final Object value) {

try {
BeanUtils.copyProperty(instance, name, value);
} catch (final InvocationTargetException ite) {
final Throwable cause = ite.getTargetException();
throw new IllegalArgumentException
("Error setting property '" + name +
"' nested exception - " + cause);
throw new IllegalArgumentException("Error setting property '" + name + "' nested exception - " + cause);
} catch (final Throwable t) {
final IllegalArgumentException iae = new IllegalArgumentException
("Error setting property '" + name +
"', exception - " + t);
BeanUtils.initCause(iae, t);
throw iae;
throw new IllegalArgumentException("Error setting property '" + name + "', exception - " + t, t);
}

}
}
Loading

0 comments on commit a4d861f

Please sign in to comment.