Uploaded image for project: 'The AndroMDA project'
  1. ANDROMDA-1194

ClassCastException caused by generated class PatternMatchingExceptionHandler

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:

      AndroMDA RC1

      Description

      The generated org.andromda.presentation.bpm4struts.PatternMatchingExceptionHandler class throws ClassCastException when handling exceptions that have a root cause which isn't a java.lang.Exception.

      For exemple, when a class on which the web application is dependent can't be found, I get the following exception in the stack trace:
      java.lang.ClassCastException: java.lang.NoClassDefFoundError

      This hides the real exception message which is actually:
      java.lang.NoClassDefFoundError: javax/transaction/TransactionManager

      The ClassCastException is caused by the following statement:
      exception = (Exception) this.findRootCause(exception);

      Reason:
      The root cause of an Exception can be any Throwable => we can't assume it is always an Exception.

      I could get the error message by changing the generated file manually (see code fragment hereafter), but it would be easier to change the method signature to throw Throwable instead of Exception:

      Generated code:

      public String handleException(Exception exception) throws Exception
      {
      exception = (Exception)this.findRootCause(exception);
      String matched = null;
      if (exception != null && exception.getMessage() != null)
      {
      Matcher matcher = compiledPattern.matcher(
      exception.getMessage().replaceAll("\\s+", " "));
      try
      {
      if (matcher.matches())

      { matched = matcher.group(3); }
      if (matched == null)
      { throw exception; }
      }
      catch (IllegalStateException ex)
      { // no match was found rethrow the exception throw exception; }
      return matched;
      }
      throw exception;
      }

      Manually edited code:
      public String handleException(Exception exception) throws Exception
      {
      final Throwable t = this.findRootCause(exception);

      if (t != null && t.getMessage() != null)
      {
      String matched = null;
      Matcher matcher = compiledPattern.matcher(
      t.getMessage().replaceAll("\\s+", " "));
      try
      {
      if (matcher.matches())
      { matched = matcher.group(3); }

      }
      catch (IllegalStateException ex)

      { // no match was found rethrow the exception }

      if (matched != null)

      { return matched; }

      }
      if (t instanceof Exception)

      { throw (Exception) t; }

      else if (t instanceof Error)

      { throw (Error) t; }

      else

      { throw new Exception(t); }

      }

        Attachments

          Activity

            People

            • Assignee:
              overheat overheat
              Reporter:
              johnny.macchione@sylis.be Johnny Macchione
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: