JDK-6946826 : Variable control via annotations or other mechanism
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 6u10
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_10
  • CPU: sparc
  • Submitted: 2010-04-23
  • Updated: 2011-02-16
  • Resolved: 2010-04-23
Related Reports
Duplicate :  
Would it possible for the java to be enhanced in a way that data checks can be done by the language itself without any data checking logic being coded in the logical flow esp for  class variables at least.

eg: Say a class has two variables

int A, B

would it be possible via annotations or some mechanism to put in the logic upfront such that A should always be between 100 and 200.  B should be always between 50 and 100. If B is above 50  A's upper limit can be increased to 300 etc.

In a nutshell the java language will ensure that these variables never exceed their given ranges and if they do, throw an exception. If a variable which has a range is defined in the class and its used in some method an exception handler should be defined by the programmer for such a variable else the compiler should throw an error saying RangeCheckException not defined etc.

Most programming errors happen not due to logic flow which is easily debug able like null pointer etc but due to data inter-dependency. Where complex data relations cause programs to not abort but produce wrong output. A huge amount added checks in between logic needs to be created simply to ensure that data remains in bounds and data interdependence are always in sync. If this could be done by the compiler itself when a variable is modified based on the logic provided upfront. This would isolate data checking logic from programming logic.

  Programs would be easier to maintain and develop.

For a class using annotations or other mechanism be able to provide the java compiler with instructions  on how to restrict a variable from moving beyond a range or value and also specify interdependency between two or more variables on how they can influence each other.

When that variable is modified during the course of logic the language should ensure that it stays in the predefined conditions, else an exception should be thrown stating the reason.