PageRenderTime 95ms CodeModel.GetById 80ms app.highlight 11ms RepoModel.GetById 1ms 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
 1package bsh;
 2
 3import java.util.Hashtable;
 4
 5/**
 6	
 7	@author Pat Niemeyer (pat@pat.net)
 8*/
 9/*
10	Note: which of these things should be checked at parse time vs. run time?
11*/
12public class Modifiers implements java.io.Serializable
13{
14	public static final int CLASS=0, METHOD=1, FIELD=2;
15	Hashtable modifiers;
16
17	/**
18		@param context is METHOD or FIELD
19	*/
20	public void addModifier( int context, String name ) 
21	{
22		if ( modifiers == null )
23			modifiers = new Hashtable();
24		Object got = modifiers.put( name, Void.TYPE/*arbitrary flag*/ );
25		if ( got != null )
26			throw new IllegalStateException("Duplicate modifier: "+ name );
27
28		int count = 0;
29		if ( hasModifier("private") ) ++count;
30		if ( hasModifier("protected") ) ++count;
31		if ( hasModifier("public") ) ++count;
32		if ( count > 1 )
33			throw new IllegalStateException(
34				"public/private/protected cannot be used in combination." );
35
36		switch( context ) 
37		{
38		case CLASS:
39			validateForClass();
40			break;
41		case METHOD:
42			validateForMethod();
43			break;
44		case FIELD:
45			validateForField();
46			break;
47		}
48	}
49
50	public boolean hasModifier( String name ) 
51	{
52		if ( modifiers == null )
53			modifiers = new Hashtable();
54		return modifiers.get(name) != null;
55	}
56
57	// could refactor these a bit
58	private void validateForMethod() 
59	{ 
60		insureNo("volatile", "Method");
61		insureNo("transient", "Method");
62	}
63	private void validateForField() 
64	{ 
65		insureNo("synchronized", "Variable");
66		insureNo("native", "Variable");
67		insureNo("abstract", "Variable");
68	}
69	private void validateForClass() 
70	{ 
71		validateForMethod(); // volatile, transient
72		insureNo("native", "Class");
73		insureNo("synchronized", "Class");
74	}
75
76	private void insureNo( String modifier, String context )
77	{
78		if ( hasModifier( modifier ) )
79			throw new IllegalStateException(
80				context + " cannot be declared '"+modifier+"'");
81	}
82
83	public String toString()
84	{
85		return "Modifiers: "+modifiers;
86	}
87
88}