Mercurial Hosting > nabble
comparison src/nabble/naml/compiler/JavaNamespace.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.naml.compiler; | |
2 | |
3 import java.lang.reflect.Constructor; | |
4 import java.util.ArrayList; | |
5 import java.util.List; | |
6 import fschmidt.util.java.Computable; | |
7 import fschmidt.util.java.Memoizer; | |
8 | |
9 | |
10 final class JavaNamespace implements GenericNamespace { | |
11 final Class cls; | |
12 private final boolean isGlobal; | |
13 private final boolean isTransparent; | |
14 final String name; | |
15 final Class extensionTarget; | |
16 final Constructor extensionConstructor; | |
17 private final List<String> names = new ArrayList<String>(); | |
18 | |
19 private JavaNamespace(Class<?> cls,Namespace namespace) { | |
20 this.cls = cls; | |
21 this.name = namespace.name(); | |
22 this.isGlobal = namespace.global(); | |
23 this.isTransparent = namespace.transparent(); | |
24 this.extensionTarget = null; | |
25 this.extensionConstructor = null; | |
26 addNames(cls); | |
27 } | |
28 | |
29 private JavaNamespace(Class<?> cls,NamespaceExtension namespaceExt) { | |
30 this.cls = cls; | |
31 this.name = namespaceExt.name(); | |
32 this.extensionTarget = namespaceExt.target(); | |
33 this.isGlobal = getNamespace(extensionTarget).isGlobal; | |
34 this.isTransparent = true; | |
35 try { | |
36 this.extensionConstructor = cls.getConstructor(extensionTarget); | |
37 } catch(NoSuchMethodException e) { | |
38 throw new RuntimeException(e); | |
39 } | |
40 addNames(cls); | |
41 } | |
42 | |
43 @Override public boolean isGlobal() { | |
44 return isGlobal; | |
45 } | |
46 | |
47 @Override public boolean isTransparent() { | |
48 return isTransparent; | |
49 } | |
50 | |
51 @Override public List<String> names() { | |
52 return names; | |
53 } | |
54 | |
55 @Override public String getId() { | |
56 return cls.getName(); | |
57 } | |
58 | |
59 private void addNames(Class<?> c) { | |
60 if( c==null ) | |
61 return; | |
62 Namespace namespace = c.getAnnotation(Namespace.class); | |
63 if( namespace != null ) { | |
64 names.add( namespace.name() ); | |
65 } | |
66 NamespaceExtension namespaceExt = c.getAnnotation(NamespaceExtension.class); | |
67 if( namespaceExt != null ) | |
68 names.add(namespaceExt.name()); | |
69 addNames(c.getSuperclass()); | |
70 /* | |
71 for( Class ifc : c.getInterfaces() ) { | |
72 addNames(ifc); | |
73 } | |
74 */ | |
75 } | |
76 | |
77 public String toString() { | |
78 return name; | |
79 } | |
80 | |
81 | |
82 private static Memoizer<Class<?>,JavaNamespace> cache = new Memoizer<Class<?>,JavaNamespace>(new Computable<Class<?>,JavaNamespace>() { | |
83 public JavaNamespace get(Class<?> cls) { | |
84 Namespace namespace = cls.getAnnotation(Namespace.class); | |
85 if( namespace != null ) | |
86 return new JavaNamespace(cls,namespace); | |
87 NamespaceExtension namespaceExt = cls.getAnnotation(NamespaceExtension.class); | |
88 if( namespaceExt != null ) | |
89 return new JavaNamespace(cls,namespaceExt); | |
90 throw new TemplateRuntimeException(""+cls+" isn't annotated as a Namespace or NamespaceExtension"); | |
91 } | |
92 }); | |
93 | |
94 static JavaNamespace getNamespace(Class cls) { | |
95 return cache.get(cls); | |
96 } | |
97 | |
98 static JavaNamespace getNamespaceExt(Class cls) { | |
99 JavaNamespace ns = getNamespace(cls); | |
100 if( ns.extensionTarget == null ) | |
101 throw new RuntimeException(""+cls+" isn't NamespaceExtension"); | |
102 return ns; | |
103 } | |
104 | |
105 } |