Class StaticCalendarDetector

  • All Implemented Interfaces:
    Detector, Priorities, org.apache.bcel.classfile.Visitor

    public class StaticCalendarDetector
    extends OpcodeStackDetector
    Detector for static fields of type Calendar or DateFormat and their subclasses. Because Calendar is unsafe for multithreaded use, static fields look suspicious. To work correctly, all access would need to be synchronized by the client which cannot be guaranteed.
    • Field Detail

      • DEBUG

        private static final boolean DEBUG
        External Debug flag set?
      • PROP_SKIP_SYNCHRONIZED_CHECK

        private static final java.lang.String PROP_SKIP_SYNCHRONIZED_CHECK
        External flag to determine whether to skip the test for synchronized blocks (default: if a call on a static Calendar or DateFormat is detected inside a synchronizationb block, it will not be reported). Setting this to true will report method calls on static fields if they are in a synchronized block. As the check currently does not take into account the lock's mutex it may be useful to switch allow
        See Also:
        Constant Field Values
      • reporter

        private final BugReporter reporter
        The reporter to report to
      • currentClass

        private java.lang.String currentClass
        Name of the class being inspected
      • calendarType

        private final ClassDescriptor calendarType
        ObjectType for Calendar
      • dateFormatType

        private final ClassDescriptor dateFormatType
        ObjectType for DateFormat
      • currentMethod

        private org.apache.bcel.classfile.Method currentMethod
        Stores the current method
      • currentCFG

        private CFG currentCFG
        Stores current Control Flow Graph
      • currentLockDataFlow

        private LockDataflow currentLockDataFlow
        Stores current LDF
      • sawDateClass

        private boolean sawDateClass
    • Constructor Detail

      • StaticCalendarDetector

        public StaticCalendarDetector​(BugReporter aReporter)
        Creates a new instance of this Detector.
        Parameters:
        aReporter - BugReporter instance to report found problems to.
    • Method Detail

      • visit

        public void visit​(org.apache.bcel.classfile.JavaClass someObj)
        Remembers the class name and resets temporary fields.
        Overrides:
        visit in class BetterVisitor
      • visit

        public void visit​(org.apache.bcel.classfile.ConstantPool pool)
        Overrides:
        visit in class BetterVisitor
      • visit

        public void visit​(org.apache.bcel.classfile.Field aField)
        Checks if the visited field is of type Calendar or DateFormat or a subclass of either one. If so and the field is static and non-private it is suspicious and will be reported.
        Overrides:
        visit in class BetterVisitor
      • visitMethod

        public void visitMethod​(org.apache.bcel.classfile.Method obj)
        Specified by:
        visitMethod in interface org.apache.bcel.classfile.Visitor
        Overrides:
        visitMethod in class BetterVisitor
      • visit

        public void visit​(org.apache.bcel.classfile.Code obj)
        Overrides:
        visit in class DismantleBytecode
      • sawOpcode

        public void sawOpcode​(int seen)
        Checks for method invocations ( INVOKEVIRTUAL) call on a static Calendar or DateFormat fields. The OpcodeStack is used to determine if an invocation is done on such a static field.
        Specified by:
        sawOpcode in class OpcodeStackDetector
        Parameters:
        seen - An opcode to be analyzed
        See Also:
        DismantleBytecode.sawOpcode(int)
      • isLocked

        private boolean isLocked()
      • report

        public void report()
        Description copied from interface: Detector
        This method is called after all classes to be visited. It should be used by any detectors which accumulate information over all visited classes to generate results.
        Specified by:
        report in interface Detector
        Overrides:
        report in class BytecodeScanningDetector