src/share/jaxws_classes/com/sun/tools/internal/jxc/ap/AnnotationParser.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/jxc/ap/AnnotationParser.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,145 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.internal.jxc.ap;
    1.30 +
    1.31 +import com.sun.tools.internal.jxc.ConfigReader;
    1.32 +import com.sun.tools.internal.jxc.api.JXC;
    1.33 +import com.sun.tools.internal.xjc.ErrorReceiver;
    1.34 +import com.sun.tools.internal.xjc.api.J2SJAXBModel;
    1.35 +import com.sun.tools.internal.xjc.api.Reference;
    1.36 +import org.xml.sax.SAXException;
    1.37 +
    1.38 +import javax.annotation.processing.AbstractProcessor;
    1.39 +import javax.annotation.processing.ProcessingEnvironment;
    1.40 +import javax.annotation.processing.RoundEnvironment;
    1.41 +import javax.annotation.processing.SupportedAnnotationTypes;
    1.42 +import javax.annotation.processing.SupportedOptions;
    1.43 +import javax.lang.model.SourceVersion;
    1.44 +import javax.lang.model.element.Element;
    1.45 +import javax.lang.model.element.ElementKind;
    1.46 +import javax.lang.model.element.TypeElement;
    1.47 +import javax.lang.model.util.ElementFilter;
    1.48 +import javax.xml.bind.SchemaOutputResolver;
    1.49 +import javax.xml.namespace.QName;
    1.50 +import java.io.File;
    1.51 +import java.io.IOException;
    1.52 +import java.util.ArrayList;
    1.53 +import java.util.Collection;
    1.54 +import java.util.Collections;
    1.55 +import java.util.Set;
    1.56 +import java.util.StringTokenizer;
    1.57 +
    1.58 +/**
    1.59 + * This class behaves as a JAXB Annotation Processor,
    1.60 + * It reads the user specified typeDeclarations
    1.61 + * and the config files
    1.62 + * It also reads config files
    1.63 + *
    1.64 + * Used in unit tests
    1.65 + *
    1.66 + * @author Bhakti Mehta (bhakti.mehta@sun.com)
    1.67 + */
    1.68 +@SupportedAnnotationTypes("javax.xml.bind.annotation.*")
    1.69 +@SupportedOptions("jaxb.config")
    1.70 +public final class AnnotationParser extends AbstractProcessor {
    1.71 +
    1.72 +    private ErrorReceiver errorListener;
    1.73 +
    1.74 +    @Override
    1.75 +    public void init(ProcessingEnvironment processingEnv) {
    1.76 +        super.init(processingEnv);
    1.77 +        this.processingEnv = processingEnv;
    1.78 +        errorListener = new ErrorReceiverImpl(
    1.79 +                processingEnv.getMessager(),
    1.80 +                processingEnv.getOptions().containsKey(Const.DEBUG_OPTION.getValue())
    1.81 +        );
    1.82 +    }
    1.83 +
    1.84 +    @Override
    1.85 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.86 +        if (processingEnv.getOptions().containsKey(Const.CONFIG_FILE_OPTION.getValue())) {
    1.87 +            String value = processingEnv.getOptions().get(Const.CONFIG_FILE_OPTION.getValue());
    1.88 +
    1.89 +            // For multiple config files we are following the format
    1.90 +            // -Aconfig=foo.config:bar.config where : is the pathSeparatorChar
    1.91 +            StringTokenizer st = new StringTokenizer(value, File.pathSeparator);
    1.92 +            if (!st.hasMoreTokens()) {
    1.93 +                errorListener.error(null, Messages.OPERAND_MISSING.format(Const.CONFIG_FILE_OPTION.getValue()));
    1.94 +                return true;
    1.95 +            }
    1.96 +
    1.97 +            while (st.hasMoreTokens()) {
    1.98 +                File configFile = new File(st.nextToken());
    1.99 +                if (!configFile.exists()) {
   1.100 +                    errorListener.error(null, Messages.NON_EXISTENT_FILE.format());
   1.101 +                    continue;
   1.102 +                }
   1.103 +
   1.104 +                try {
   1.105 +                    Collection<TypeElement> rootElements = new ArrayList<TypeElement>();
   1.106 +                    filterClass(rootElements, roundEnv.getRootElements());
   1.107 +                    ConfigReader configReader = new ConfigReader(
   1.108 +                            processingEnv,
   1.109 +                            rootElements,
   1.110 +                            configFile,
   1.111 +                            errorListener
   1.112 +                    );
   1.113 +
   1.114 +                    Collection<Reference> classesToBeIncluded = configReader.getClassesToBeIncluded();
   1.115 +                    J2SJAXBModel model = JXC.createJavaCompiler().bind(
   1.116 +                            classesToBeIncluded, Collections.<QName, Reference>emptyMap(), null, processingEnv);
   1.117 +
   1.118 +                    SchemaOutputResolver schemaOutputResolver = configReader.getSchemaOutputResolver();
   1.119 +
   1.120 +                    model.generateSchema(schemaOutputResolver, errorListener);
   1.121 +                } catch (IOException e) {
   1.122 +                    errorListener.error(e.getMessage(), e);
   1.123 +                } catch (SAXException e) {
   1.124 +                    // the error should have already been reported
   1.125 +                }
   1.126 +            }
   1.127 +        }
   1.128 +        return true;
   1.129 +    }
   1.130 +
   1.131 +    private void filterClass(Collection<TypeElement> rootElements, Collection<? extends Element> elements) {
   1.132 +        for (Element element : elements) {
   1.133 +            if (element.getKind().equals(ElementKind.CLASS) || element.getKind().equals(ElementKind.INTERFACE) ||
   1.134 +                    element.getKind().equals(ElementKind.ENUM)) {
   1.135 +                rootElements.add((TypeElement) element);
   1.136 +                filterClass(rootElements, ElementFilter.typesIn(element.getEnclosedElements()));
   1.137 +            }
   1.138 +        }
   1.139 +    }
   1.140 +
   1.141 +    @Override
   1.142 +    public SourceVersion getSupportedSourceVersion() {
   1.143 +        if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_6) > 0)
   1.144 +            return SourceVersion.valueOf("RELEASE_7");
   1.145 +        else
   1.146 +            return SourceVersion.RELEASE_6;
   1.147 +    }
   1.148 +}

mercurial