01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.netui.compiler.apt;
20:
21: import org.apache.beehive.netui.compiler.typesystem.impl.env.CoreAnnotationProcessorEnvImpl;
22: import org.apache.beehive.netui.compiler.typesystem.impl.DelegatingImpl;
23: import org.apache.beehive.netui.compiler.typesystem.impl.WrapperFactory;
24: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeDeclaration;
25: import org.apache.beehive.netui.compiler.typesystem.env.CoreAnnotationProcessor;
26: import org.apache.beehive.netui.compiler.typesystem.env.CoreAnnotationProcessorEnv;
27:
28: import java.util.Set;
29: import java.util.Iterator;
30:
31: import com.sun.mirror.apt.AnnotationProcessorFactory;
32: import com.sun.mirror.apt.AnnotationProcessor;
33: import com.sun.mirror.apt.AnnotationProcessorEnvironment;
34:
35: /**
36: * Slim Sun apt annotation processor factory that delegates to our "core" (apt-independent) layer.
37: */
38: public abstract class BaseAnnotationProcessorFactory implements
39: AnnotationProcessorFactory {
40: /**
41: * Get a Sun annotation processor for the given annotation type declarations. This just wraps the type declarations
42: * in our wrapper types, and returns an annotation processor that delegates to our "core" (apt-independent)
43: * processor to do the real work.
44: */
45: public final AnnotationProcessor getProcessorFor(
46: Set annotationTypeDeclarations,
47: AnnotationProcessorEnvironment env) {
48:
49: CoreAnnotationProcessorEnv coreEnv = CoreAnnotationProcessorEnvImpl
50: .get(env);
51: AnnotationTypeDeclaration[] atds = new AnnotationTypeDeclaration[annotationTypeDeclarations
52: .size()];
53: int j = 0;
54: for (Iterator i = annotationTypeDeclarations.iterator(); i
55: .hasNext();) {
56: // Wrap each Sun/Mirror annotation type declaration with our core AnnotationTypeDeclaration.
57: com.sun.mirror.declaration.AnnotationTypeDeclaration decl = (com.sun.mirror.declaration.AnnotationTypeDeclaration) i
58: .next();
59: atds[j++] = WrapperFactory.get()
60: .getAnnotationTypeDeclaration(decl);
61: }
62:
63: CoreAnnotationProcessor ap = getCoreProcessorFor(atds, coreEnv);
64: return ap != null ? new DelegatingAnnotationProcessor(ap)
65: : null;
66: }
67:
68: private static class DelegatingAnnotationProcessor extends
69: DelegatingImpl implements AnnotationProcessor {
70: public DelegatingAnnotationProcessor(
71: CoreAnnotationProcessor delegate) {
72: super (delegate);
73: }
74:
75: public void process() {
76: ((CoreAnnotationProcessor) getDelegate()).process();
77: }
78: }
79:
80: /**
81: * Get the core annotation processor which is appropriate for the given annotations. Note that this is "our"
82: * annotation processor, not a Sun apt annotation processor. See
83: * {@link BaseAnnotationProcessorFactory#getProcessorFor} for the place where a Sun annotation processor is returned.
84: */
85: protected abstract CoreAnnotationProcessor getCoreProcessorFor(
86: AnnotationTypeDeclaration[] annotationTypeDecls,
87: CoreAnnotationProcessorEnv env);
88: }
|