[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
| Re: [jdt-dev] Use of Java asserts in ECJ | 
  
  
    On 19.04.24 13:04, Sankaran, Srikanth
      via jdt-dev wrote:
    
    
      
      
      
      
        I wholeheartedly support adding lots of
          sanity checks via assertions, but not via Java assert
          statement, but using the pattern
         
        if
            (42 != 42)
            {
           
            throw
            new
              AssertionError("Something
              is fishy!");
        }
         
       
    
    Various situations have their specific patterns for raising red
      alert.
    First, locally launching junit tests should always (i.e.,
      automatically) pass -ea. If that is not the case it could be a
      regression vis-a-vis 
      https://bugs.eclipse.org/bugs/show_bug.cgi?id=479553 (or someone
      carelessly removed the vmarg from the launch config ;) ).
    
    Secondly, in the compiler we have a group of options around
      AbortCompilation, ProblemHandler.abortDueToInternalError() which
      try to report the problem as a compile error. This  has the
      benefit of associating the problem to a specific source location
      (at least the compilation unit that was currently being
      processed). Directly throwing AssertionError misses this
      opportunity.
    
    Finally, it's always a question how fatal is the problem? If,
      e.g., the problem occurs during indexing, should we end up with no
      index at all, or can we just skip some (small) part and keep
      going? In the IDE some problems may just be logged, and then
      ignored (and remember: the compiler might be called in various
      guises in the IDE).
    
    Since the compiler cannot use any logging framework we can only
      throw, report an error or keep silent. I could still imagine
      situations (e.g, when reading library class with unexpected
      details) where the normal behavior should be "ignore", but if
      something fishy is observed then the assert keyword could be a
      useful tool. In that case we could ask a user to set -ea to figure
      out if any such issue was observed but ignored in normal
      operation.
    So my simple answer would be: it depends.
    But unconditionally throwing any RuntimeException or Error is a
      drastic thing to be done only as a last resort, if you ask me.
    best,
      Stephan
    
    PS: Does anyone know, if it is possible to set -ea
      programmatically? In that case the ECJ Main could do just that for
      asserts that in other use cases could be ignorable :)