001    package bsh;
002    
003    import java.util.Hashtable;
004    
005    /**
006            
007            @author Pat Niemeyer (pat@pat.net)
008    */
009    /*
010            Note: which of these things should be checked at parse time vs. run time?
011    */
012    public class Modifiers implements java.io.Serializable
013    {
014            public static final int CLASS=0, METHOD=1, FIELD=2;
015            Hashtable modifiers;
016    
017            /**
018                    @param context is METHOD or FIELD
019            */
020            public void addModifier( int context, String name ) 
021            {
022                    if ( modifiers == null )
023                            modifiers = new Hashtable();
024    
025                    Object existing = modifiers.put( name, Void.TYPE/*arbitrary flag*/ );
026                    if ( existing != null )
027                            throw new IllegalStateException("Duplicate modifier: "+ name );
028    
029                    int count = 0;
030                    if ( hasModifier("private") ) ++count;
031                    if ( hasModifier("protected") ) ++count;
032                    if ( hasModifier("public") ) ++count;
033                    if ( count > 1 )
034                            throw new IllegalStateException(
035                                    "public/private/protected cannot be used in combination." );
036    
037                    switch( context ) 
038                    {
039                    case CLASS:
040                            validateForClass();
041                            break;
042                    case METHOD:
043                            validateForMethod();
044                            break;
045                    case FIELD:
046                            validateForField();
047                            break;
048                    }
049            }
050    
051            public boolean hasModifier( String name ) 
052            {
053                    if ( modifiers == null )
054                            modifiers = new Hashtable();
055                    return modifiers.get(name) != null;
056            }
057    
058            // could refactor these a bit
059            private void validateForMethod() 
060            { 
061                    insureNo("volatile", "Method");
062                    insureNo("transient", "Method");
063            }
064            private void validateForField() 
065            { 
066                    insureNo("synchronized", "Variable");
067                    insureNo("native", "Variable");
068                    insureNo("abstract", "Variable");
069            }
070            private void validateForClass() 
071            { 
072                    validateForMethod(); // volatile, transient
073                    insureNo("native", "Class");
074                    insureNo("synchronized", "Class");
075            }
076    
077            private void insureNo( String modifier, String context )
078            {
079                    if ( hasModifier( modifier ) )
080                            throw new IllegalStateException(
081                                    context + " cannot be declared '"+modifier+"'");
082            }
083    
084            public String toString()
085            {
086                    return "Modifiers: "+modifiers;
087            }
088    
089    }