java - custom compile time error -
i want show compile time error when call method.
like have class "myclass" in there 2 methods "methoda()" , "methodb()". make instance of "myclass" . using instance can call both methods need show compile time error if call "methodb()" before "methoda()"; strong text
class myclasss { public void methoda() { //do thing } public void methodb() { //do thing } } class myrunningclasss { public static void main(string... args) { myclass mc = new myclass(); // not give compile time error. mc.methoda(); mc.methodb(); // have give compile time error. mc.methodb(); mc.methoda(); } }
what suggesting isn't easy. need download openjdk , change it. it's large code base don't suggest that.
instead suggest add runtime assertion check , unit test code. if use maven or ant run tests part of build, these error detect @ build time, if tests, not compiler detects error.
what makes particularly difficult compiler if can number of things difficult determine @ compile time.
e.g.
public static somethinga(int n) { // if(n == x) mc.methoda(); } public static somethingb(int n) { // if(n == y) mc.methodb(); } // compile error or not for(int i=0;i<10;i++) { somethingb(i); somethinga(i); }
there many patterns such feature useful, hard solve. e.g. making sure lock.unlock() after lock.lock(), can place these in different methods, or put conditions on them.
Comments
Post a Comment