PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/bsh/Modifiers.java

#
Java | 88 lines | 67 code | 10 blank | 11 comment | 12 complexity | b693d460a5e616d70abcfa73b5d142a4 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. package bsh;
  2. import java.util.Hashtable;
  3. /**
  4. @author Pat Niemeyer (pat@pat.net)
  5. */
  6. /*
  7. Note: which of these things should be checked at parse time vs. run time?
  8. */
  9. public class Modifiers implements java.io.Serializable
  10. {
  11. public static final int CLASS=0, METHOD=1, FIELD=2;
  12. Hashtable modifiers;
  13. /**
  14. @param context is METHOD or FIELD
  15. */
  16. public void addModifier( int context, String name )
  17. {
  18. if ( modifiers == null )
  19. modifiers = new Hashtable();
  20. Object got = modifiers.put( name, Void.TYPE/*arbitrary flag*/ );
  21. if ( got != null )
  22. throw new IllegalStateException("Duplicate modifier: "+ name );
  23. int count = 0;
  24. if ( hasModifier("private") ) ++count;
  25. if ( hasModifier("protected") ) ++count;
  26. if ( hasModifier("public") ) ++count;
  27. if ( count > 1 )
  28. throw new IllegalStateException(
  29. "public/private/protected cannot be used in combination." );
  30. switch( context )
  31. {
  32. case CLASS:
  33. validateForClass();
  34. break;
  35. case METHOD:
  36. validateForMethod();
  37. break;
  38. case FIELD:
  39. validateForField();
  40. break;
  41. }
  42. }
  43. public boolean hasModifier( String name )
  44. {
  45. if ( modifiers == null )
  46. modifiers = new Hashtable();
  47. return modifiers.get(name) != null;
  48. }
  49. // could refactor these a bit
  50. private void validateForMethod()
  51. {
  52. insureNo("volatile", "Method");
  53. insureNo("transient", "Method");
  54. }
  55. private void validateForField()
  56. {
  57. insureNo("synchronized", "Variable");
  58. insureNo("native", "Variable");
  59. insureNo("abstract", "Variable");
  60. }
  61. private void validateForClass()
  62. {
  63. validateForMethod(); // volatile, transient
  64. insureNo("native", "Class");
  65. insureNo("synchronized", "Class");
  66. }
  67. private void insureNo( String modifier, String context )
  68. {
  69. if ( hasModifier( modifier ) )
  70. throw new IllegalStateException(
  71. context + " cannot be declared '"+modifier+"'");
  72. }
  73. public String toString()
  74. {
  75. return "Modifiers: "+modifiers;
  76. }
  77. }