1 package edu.berkeley.sbp.meta;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.bind.*;
8 import java.lang.annotation.*;
9 import java.lang.reflect.*;
12 // FIXME: non-static methods
13 public class AnnotationGrammarBindings extends Grammar.Bindings {
15 private static boolean harsh = true;
17 private final Class _cl;
18 private HashMap<String,Class[]> _inner = new HashMap<String,Class[]>();
19 private HashMap<String,Method[]> _allMethods = new HashMap<String,Method[]>();
21 public AnnotationGrammarBindings(Class c) {
26 public void add(Class c, String prefix) {
28 ArrayList<Class> alc = new ArrayList<Class>();
29 if (_inner.get(prefix) != null)
30 for(Class cc : _inner.get(prefix))
33 ArrayList<Method> alm = new ArrayList<Method>();
34 if (_allMethods.get(prefix) != null)
35 for(Method m : _allMethods.get(prefix))
38 add(c, alc, alm, prefix);
39 this._inner.put(prefix, (Class[])alc.toArray(new Class[0]));
40 this._allMethods.put(prefix, (Method[])alm.toArray(new Method[0]));
43 public Object repeatTag() { return new Tree.ArrayBuildingTreeFunctor<Object>(); }
45 public Sequence tryResolveTag(Production p) {
47 String key = p.tag==null?p.nonTerminal:p.tag;
48 if (key==null) return null;
50 String prefix = key.indexOf('.')==-1 ? "" : key.substring(0, key.lastIndexOf('.'));
51 String suffix = key.indexOf('.')==-1 ? key : key.substring(key.lastIndexOf('.')+1);
53 p = new Production(suffix, p.elements, p.drops);
54 for(Method m : _allMethods.get(prefix))
55 if (p.isCompatible(m))
56 return p.makeSequence(m);
57 for(Class c : _inner.get(prefix))
58 for(Constructor con : c.getConstructors())
59 if (p.isCompatible(con))
60 return p.makeSequence(con);
61 for(Class c : _inner.get(prefix))
62 if (p.isCompatible(c))
63 return p.makeSequence(c);
72 private static void add(Class cl, ArrayList<Class> alc, ArrayList<Method> alm, String prefix) {
74 for(Method m : cl.getDeclaredMethods())
76 for(Class c : cl.getDeclaredClasses()) {
78 add(c, alc, alm, prefix);
80 if (cl.getSuperclass() != Object.class)
81 add(cl.getSuperclass(), alc, alm, prefix);