diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/LogUtils.java b/common/source/java/ch/systemsx/cisd/common/utilities/LogUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..5d8e48b3d5f9ee9d959988234ccd4a0303c11ecb --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/utilities/LogUtils.java @@ -0,0 +1,33 @@ +/* + * Copyright 2010 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.utilities; + +import org.apache.log4j.Logger; + +/** + * @author Piotr Buczek + */ +public class LogUtils +{ + + /** log error in production, fail in development mode */ + public static void logErrorWithFailingAssertion(Logger logger, String message) + { + logger.error(message); + assert false : message; + } +} diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ReflectingStringEscaper.java b/common/source/java/ch/systemsx/cisd/common/utilities/ReflectingStringEscaper.java index fc9cb01c12b1bcbcab68bbc3ac90a7e829a6d10c..ecd1226349bd8f10ca37e1597cbf454af73cd8a5 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/ReflectingStringEscaper.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ReflectingStringEscaper.java @@ -59,9 +59,11 @@ public class ReflectingStringEscaper ReflectingStringEscaperUnrestricted<T> escaper = new ReflectingStringEscaperUnrestricted<T>(true, bean); return escaper.escape(); - } catch (Exception exception) + } catch (Throwable t) { - operationLog.error(exception.getMessage()); + t.printStackTrace(); + LogUtils.logErrorWithFailingAssertion(operationLog, + "Cannot traverse primitive collections or primitives " + t); return null; } finally { diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/ReflectionStringTraverser.java b/common/source/java/ch/systemsx/cisd/common/utilities/ReflectionStringTraverser.java index 98b4c97f9f7e11c99967764b36132e675dc28fa0..aae3bbd16e5f4d3b20c7b27c7ad523317afaf3ac 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/ReflectionStringTraverser.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/ReflectionStringTraverser.java @@ -22,6 +22,7 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -81,7 +82,8 @@ class ReflectionStringTraverser } if (isMutable(object) == false) { - log.error("Cannot traverse primitive collections or primitives " + object); + LogUtils.logErrorWithFailingAssertion(log, + "Cannot traverse primitive collections or primitives " + object); return; } else if (clazz.isArray()) { @@ -128,13 +130,13 @@ class ReflectionStringTraverser try { traverseField(object, field); - } catch (Exception ex) + } catch (Throwable t) { - log.error("Failed accessing field <" + field.getName() + "> of " - + object.getClass() + ":\n\t" + object); - ex.printStackTrace(); - - throw new IllegalStateException("Should not happen: " + ex.getMessage()); + t.printStackTrace(); + LogUtils.logErrorWithFailingAssertion( + log, + "Failed accessing field <" + field.getName() + "> of " + + object.getClass() + ":\n\t" + object); } } } @@ -246,7 +248,8 @@ class ReflectionStringTraverser } else { // NOTE: we do not handle e.g. list of list of Strings - log.error("Cannot traverse primitive collections or primitives " + collection); + LogUtils.logErrorWithFailingAssertion(log, + "Cannot traverse primitive collections or primitives " + collection); } } } @@ -346,7 +349,7 @@ class ReflectionStringTraverser return new ArrayList<T>(); } else if (Set.class.isAssignableFrom(clazz)) { - return new HashSet<T>(); + return new LinkedHashSet<T>(); // preserve order } else { throw new IllegalStateException("Do not know how to create a collection of type "