Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.internal.jxc.ap; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.tools.internal.jxc.ConfigReader; |
aoqi@0 | 29 | import com.sun.tools.internal.jxc.api.JXC; |
aoqi@0 | 30 | import com.sun.tools.internal.xjc.ErrorReceiver; |
aoqi@0 | 31 | import com.sun.tools.internal.xjc.api.J2SJAXBModel; |
aoqi@0 | 32 | import com.sun.tools.internal.xjc.api.Reference; |
aoqi@0 | 33 | import org.xml.sax.SAXException; |
aoqi@0 | 34 | |
aoqi@0 | 35 | import javax.annotation.processing.AbstractProcessor; |
aoqi@0 | 36 | import javax.annotation.processing.ProcessingEnvironment; |
aoqi@0 | 37 | import javax.annotation.processing.RoundEnvironment; |
aoqi@0 | 38 | import javax.annotation.processing.SupportedAnnotationTypes; |
aoqi@0 | 39 | import javax.annotation.processing.SupportedOptions; |
aoqi@0 | 40 | import javax.lang.model.SourceVersion; |
aoqi@0 | 41 | import javax.lang.model.element.Element; |
aoqi@0 | 42 | import javax.lang.model.element.ElementKind; |
aoqi@0 | 43 | import javax.lang.model.element.TypeElement; |
aoqi@0 | 44 | import javax.lang.model.util.ElementFilter; |
aoqi@0 | 45 | import javax.xml.bind.SchemaOutputResolver; |
aoqi@0 | 46 | import javax.xml.namespace.QName; |
aoqi@0 | 47 | import java.io.File; |
aoqi@0 | 48 | import java.io.IOException; |
aoqi@0 | 49 | import java.util.ArrayList; |
aoqi@0 | 50 | import java.util.Collection; |
aoqi@0 | 51 | import java.util.Collections; |
aoqi@0 | 52 | import java.util.Set; |
aoqi@0 | 53 | import java.util.StringTokenizer; |
aoqi@0 | 54 | |
aoqi@0 | 55 | /** |
aoqi@0 | 56 | * This class behaves as a JAXB Annotation Processor, |
aoqi@0 | 57 | * It reads the user specified typeDeclarations |
aoqi@0 | 58 | * and the config files |
aoqi@0 | 59 | * It also reads config files |
aoqi@0 | 60 | * |
aoqi@0 | 61 | * Used in unit tests |
aoqi@0 | 62 | * |
aoqi@0 | 63 | * @author Bhakti Mehta (bhakti.mehta@sun.com) |
aoqi@0 | 64 | */ |
aoqi@0 | 65 | @SupportedAnnotationTypes("javax.xml.bind.annotation.*") |
aoqi@0 | 66 | @SupportedOptions("jaxb.config") |
aoqi@0 | 67 | public final class AnnotationParser extends AbstractProcessor { |
aoqi@0 | 68 | |
aoqi@0 | 69 | private ErrorReceiver errorListener; |
aoqi@0 | 70 | |
aoqi@0 | 71 | @Override |
aoqi@0 | 72 | public void init(ProcessingEnvironment processingEnv) { |
aoqi@0 | 73 | super.init(processingEnv); |
aoqi@0 | 74 | this.processingEnv = processingEnv; |
aoqi@0 | 75 | errorListener = new ErrorReceiverImpl( |
aoqi@0 | 76 | processingEnv.getMessager(), |
aoqi@0 | 77 | processingEnv.getOptions().containsKey(Const.DEBUG_OPTION.getValue()) |
aoqi@0 | 78 | ); |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | @Override |
aoqi@0 | 82 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
aoqi@0 | 83 | if (processingEnv.getOptions().containsKey(Const.CONFIG_FILE_OPTION.getValue())) { |
aoqi@0 | 84 | String value = processingEnv.getOptions().get(Const.CONFIG_FILE_OPTION.getValue()); |
aoqi@0 | 85 | |
aoqi@0 | 86 | // For multiple config files we are following the format |
aoqi@0 | 87 | // -Aconfig=foo.config:bar.config where : is the pathSeparatorChar |
aoqi@0 | 88 | StringTokenizer st = new StringTokenizer(value, File.pathSeparator); |
aoqi@0 | 89 | if (!st.hasMoreTokens()) { |
aoqi@0 | 90 | errorListener.error(null, Messages.OPERAND_MISSING.format(Const.CONFIG_FILE_OPTION.getValue())); |
aoqi@0 | 91 | return true; |
aoqi@0 | 92 | } |
aoqi@0 | 93 | |
aoqi@0 | 94 | while (st.hasMoreTokens()) { |
aoqi@0 | 95 | File configFile = new File(st.nextToken()); |
aoqi@0 | 96 | if (!configFile.exists()) { |
aoqi@0 | 97 | errorListener.error(null, Messages.NON_EXISTENT_FILE.format()); |
aoqi@0 | 98 | continue; |
aoqi@0 | 99 | } |
aoqi@0 | 100 | |
aoqi@0 | 101 | try { |
aoqi@0 | 102 | Collection<TypeElement> rootElements = new ArrayList<TypeElement>(); |
aoqi@0 | 103 | filterClass(rootElements, roundEnv.getRootElements()); |
aoqi@0 | 104 | ConfigReader configReader = new ConfigReader( |
aoqi@0 | 105 | processingEnv, |
aoqi@0 | 106 | rootElements, |
aoqi@0 | 107 | configFile, |
aoqi@0 | 108 | errorListener |
aoqi@0 | 109 | ); |
aoqi@0 | 110 | |
aoqi@0 | 111 | Collection<Reference> classesToBeIncluded = configReader.getClassesToBeIncluded(); |
aoqi@0 | 112 | J2SJAXBModel model = JXC.createJavaCompiler().bind( |
aoqi@0 | 113 | classesToBeIncluded, Collections.<QName, Reference>emptyMap(), null, processingEnv); |
aoqi@0 | 114 | |
aoqi@0 | 115 | SchemaOutputResolver schemaOutputResolver = configReader.getSchemaOutputResolver(); |
aoqi@0 | 116 | |
aoqi@0 | 117 | model.generateSchema(schemaOutputResolver, errorListener); |
aoqi@0 | 118 | } catch (IOException e) { |
aoqi@0 | 119 | errorListener.error(e.getMessage(), e); |
aoqi@0 | 120 | } catch (SAXException e) { |
aoqi@0 | 121 | // the error should have already been reported |
aoqi@0 | 122 | } |
aoqi@0 | 123 | } |
aoqi@0 | 124 | } |
aoqi@0 | 125 | return true; |
aoqi@0 | 126 | } |
aoqi@0 | 127 | |
aoqi@0 | 128 | private void filterClass(Collection<TypeElement> rootElements, Collection<? extends Element> elements) { |
aoqi@0 | 129 | for (Element element : elements) { |
aoqi@0 | 130 | if (element.getKind().equals(ElementKind.CLASS) || element.getKind().equals(ElementKind.INTERFACE) || |
aoqi@0 | 131 | element.getKind().equals(ElementKind.ENUM)) { |
aoqi@0 | 132 | rootElements.add((TypeElement) element); |
aoqi@0 | 133 | filterClass(rootElements, ElementFilter.typesIn(element.getEnclosedElements())); |
aoqi@0 | 134 | } |
aoqi@0 | 135 | } |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | @Override |
aoqi@0 | 139 | public SourceVersion getSupportedSourceVersion() { |
aoqi@0 | 140 | if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_6) > 0) |
aoqi@0 | 141 | return SourceVersion.valueOf("RELEASE_7"); |
aoqi@0 | 142 | else |
aoqi@0 | 143 | return SourceVersion.RELEASE_6; |
aoqi@0 | 144 | } |
aoqi@0 | 145 | } |