feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,145 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.jxc.ap;
import com.sun.tools.internal.jxc.ConfigReader;
import com.sun.tools.internal.jxc.api.JXC;
import com.sun.tools.internal.xjc.ErrorReceiver;
import com.sun.tools.internal.xjc.api.J2SJAXBModel;
import com.sun.tools.internal.xjc.api.Reference;
import org.xml.sax.SAXException;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.namespace.QName;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.StringTokenizer;
/**
* This class behaves as a JAXB Annotation Processor,
* It reads the user specified typeDeclarations
* and the config files
* It also reads config files
*
* Used in unit tests
*
* @author Bhakti Mehta (bhakti.mehta@sun.com)
*/
@SupportedAnnotationTypes("javax.xml.bind.annotation.*")
@SupportedOptions("jaxb.config")
public final class AnnotationParser extends AbstractProcessor {
private ErrorReceiver errorListener;
@Override
public void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
this.processingEnv = processingEnv;
errorListener = new ErrorReceiverImpl(
processingEnv.getMessager(),
processingEnv.getOptions().containsKey(Const.DEBUG_OPTION.getValue())
);
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (processingEnv.getOptions().containsKey(Const.CONFIG_FILE_OPTION.getValue())) {
String value = processingEnv.getOptions().get(Const.CONFIG_FILE_OPTION.getValue());
// For multiple config files we are following the format
// -Aconfig=foo.config:bar.config where : is the pathSeparatorChar
StringTokenizer st = new StringTokenizer(value, File.pathSeparator);
if (!st.hasMoreTokens()) {
errorListener.error(null, Messages.OPERAND_MISSING.format(Const.CONFIG_FILE_OPTION.getValue()));
return true;
}
while (st.hasMoreTokens()) {
File configFile = new File(st.nextToken());
if (!configFile.exists()) {
errorListener.error(null, Messages.NON_EXISTENT_FILE.format());
continue;
}
try {
Collection<TypeElement> rootElements = new ArrayList<TypeElement>();
filterClass(rootElements, roundEnv.getRootElements());
ConfigReader configReader = new ConfigReader(
processingEnv,
rootElements,
configFile,
errorListener
);
Collection<Reference> classesToBeIncluded = configReader.getClassesToBeIncluded();
J2SJAXBModel model = JXC.createJavaCompiler().bind(
classesToBeIncluded, Collections.<QName, Reference>emptyMap(), null, processingEnv);
SchemaOutputResolver schemaOutputResolver = configReader.getSchemaOutputResolver();
model.generateSchema(schemaOutputResolver, errorListener);
} catch (IOException e) {
errorListener.error(e.getMessage(), e);
} catch (SAXException e) {
// the error should have already been reported
}
}
}
return true;
}
private void filterClass(Collection<TypeElement> rootElements, Collection<? extends Element> elements) {
for (Element element : elements) {
if (element.getKind().equals(ElementKind.CLASS) || element.getKind().equals(ElementKind.INTERFACE) ||
element.getKind().equals(ElementKind.ENUM)) {
rootElements.add((TypeElement) element);
filterClass(rootElements, ElementFilter.typesIn(element.getEnclosedElements()));
}
}
}
@Override
public SourceVersion getSupportedSourceVersion() {
if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_6) > 0)
return SourceVersion.valueOf("RELEASE_7");
else
return SourceVersion.RELEASE_6;
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.jxc.ap;
import java.io.File;
/**
* Defines constants used in the Annotation Processing driver.
*
* @author Kohsuke Kawaguchi
*/
public enum Const {
/**
* Name of the annotation processing command-line option to take user-specified config files.
* <p/>
* <p/>
* It can take multiple file names separately by {@link File#pathSeparator}.
*/
CONFIG_FILE_OPTION("jaxb.config"),
DEBUG_OPTION("jaxb.debug");
private String value;
private Const(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.jxc.ap;
import com.sun.tools.internal.xjc.ErrorReceiver;
import org.xml.sax.SAXParseException;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.Diagnostic;
/**
* @author Kohsuke Kawaguchi
*/
final class ErrorReceiverImpl extends ErrorReceiver {
private final Messager messager;
private final boolean debug;
public ErrorReceiverImpl(Messager messager, boolean debug) {
this.messager = messager;
this.debug = debug;
}
public ErrorReceiverImpl(Messager messager) {
this(messager,false);
}
public ErrorReceiverImpl(ProcessingEnvironment env) {
this(env.getMessager());
}
public void error(SAXParseException exception) {
messager.printMessage(Diagnostic.Kind.ERROR, exception.getMessage());
messager.printMessage(Diagnostic.Kind.ERROR, getLocation(exception));
printDetail(exception);
}
public void fatalError(SAXParseException exception) {
messager.printMessage(Diagnostic.Kind.ERROR, exception.getMessage());
messager.printMessage(Diagnostic.Kind.ERROR, getLocation(exception));
printDetail(exception);
}
public void warning(SAXParseException exception) {
messager.printMessage(Diagnostic.Kind.WARNING, exception.getMessage());
messager.printMessage(Diagnostic.Kind.WARNING, getLocation(exception));
printDetail(exception);
}
public void info(SAXParseException exception) {
printDetail(exception);
}
private String getLocation(SAXParseException e) {
// TODO: format the location information for printing
return "";
}
private void printDetail(SAXParseException e) {
if(debug) {
e.printStackTrace(System.out);
}
}
}

View File

@@ -0,0 +1,175 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.jxc.ap;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.type.MirroredTypesException;
import javax.lang.model.type.TypeMirror;
import com.sun.xml.internal.bind.v2.model.annotation.AbstractInlineAnnotationReaderImpl;
import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader;
import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
import com.sun.xml.internal.bind.v2.model.annotation.LocatableAnnotation;
/**
* {@link AnnotationReader} implementation that reads annotation inline from Annoation Processing.
*
* @author Kohsuke Kawaguchi (kk@kohsuke.org)
*/
public final class InlineAnnotationReaderImpl extends AbstractInlineAnnotationReaderImpl<TypeMirror, TypeElement, VariableElement, ExecutableElement> {
/** The singleton instance. */
public static final InlineAnnotationReaderImpl theInstance = new InlineAnnotationReaderImpl();
private InlineAnnotationReaderImpl() {}
public <A extends Annotation> A getClassAnnotation(Class<A> a, TypeElement clazz, Locatable srcPos) {
return LocatableAnnotation.create(clazz.getAnnotation(a),srcPos);
}
public <A extends Annotation> A getFieldAnnotation(Class<A> a, VariableElement f, Locatable srcPos) {
return LocatableAnnotation.create(f.getAnnotation(a),srcPos);
}
public boolean hasFieldAnnotation(Class<? extends Annotation> annotationType, VariableElement f) {
return f.getAnnotation(annotationType)!=null;
}
public boolean hasClassAnnotation(TypeElement clazz, Class<? extends Annotation> annotationType) {
return clazz.getAnnotation(annotationType)!=null;
}
public Annotation[] getAllFieldAnnotations(VariableElement field, Locatable srcPos) {
return getAllAnnotations(field,srcPos);
}
public <A extends Annotation> A getMethodAnnotation(Class<A> a, ExecutableElement method, Locatable srcPos) {
return LocatableAnnotation.create(method.getAnnotation(a),srcPos);
}
public boolean hasMethodAnnotation(Class<? extends Annotation> a, ExecutableElement method) {
return method.getAnnotation(a)!=null;
}
public Annotation[] getAllMethodAnnotations(ExecutableElement method, Locatable srcPos) {
return getAllAnnotations(method,srcPos);
}
/**
* Gets all the annotations on the given declaration.
*/
private Annotation[] getAllAnnotations(Element decl, Locatable srcPos) {
List<Annotation> r = new ArrayList<Annotation>();
for( AnnotationMirror m : decl.getAnnotationMirrors() ) {
try {
String fullName = ((TypeElement) m.getAnnotationType().asElement()).getQualifiedName().toString();
Class<? extends Annotation> type =
SecureLoader.getClassClassLoader(getClass()).loadClass(fullName).asSubclass(Annotation.class);
Annotation annotation = decl.getAnnotation(type);
if(annotation!=null)
r.add( LocatableAnnotation.create(annotation,srcPos) );
} catch (ClassNotFoundException e) {
// just continue
}
}
return r.toArray(new Annotation[r.size()]);
}
public <A extends Annotation> A getMethodParameterAnnotation(Class<A> a, ExecutableElement m, int paramIndex, Locatable srcPos) {
VariableElement[] params = m.getParameters().toArray(new VariableElement[m.getParameters().size()]);
return LocatableAnnotation.create(
params[paramIndex].getAnnotation(a), srcPos );
}
public <A extends Annotation> A getPackageAnnotation(Class<A> a, TypeElement clazz, Locatable srcPos) {
return LocatableAnnotation.create(clazz.getEnclosingElement().getAnnotation(a), srcPos);
}
public TypeMirror getClassValue(Annotation a, String name) {
try {
a.annotationType().getMethod(name).invoke(a);
assert false;
throw new IllegalStateException("should throw a MirroredTypeException");
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
} catch (InvocationTargetException e) {
if( e.getCause() instanceof MirroredTypeException ) {
MirroredTypeException me = (MirroredTypeException)e.getCause();
return me.getTypeMirror();
}
// impossible
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new NoSuchMethodError(e.getMessage());
}
}
public TypeMirror[] getClassArrayValue(Annotation a, String name) {
try {
a.annotationType().getMethod(name).invoke(a);
assert false;
throw new IllegalStateException("should throw a MirroredTypesException");
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
} catch (InvocationTargetException e) {
if( e.getCause() instanceof MirroredTypesException ) {
MirroredTypesException me = (MirroredTypesException)e.getCause();
Collection<? extends TypeMirror> r = me.getTypeMirrors();
return r.toArray(new TypeMirror[r.size()]);
}
// *********************** TODO: jdk6 bug. Fixed in java7
// According to the javadocs it should throw the MirroredTypesException
if( e.getCause() instanceof MirroredTypeException ) {
MirroredTypeException me = (MirroredTypeException)e.getCause();
TypeMirror tr = me.getTypeMirror();
TypeMirror[] trArr = new TypeMirror[1];
trArr[0] = tr;
return trArr;
}
// *******************************************
// impossible
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new NoSuchMethodError(e.getMessage());
}
}
protected String fullName(ExecutableElement m) {
return ((TypeElement) m.getEnclosingElement()).getQualifiedName().toString()+'#'+m.getSimpleName();
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.jxc.ap;
import java.text.MessageFormat;
import java.util.ResourceBundle;
/**
* Message resources.
*
* @author Kohsuke Kawaguchi
*/
enum Messages {
// Accessor
NON_EXISTENT_FILE, // 1 arg
UNRECOGNIZED_PARAMETER, //1 arg
OPERAND_MISSING, // 1 arg
;
private static final ResourceBundle rb = ResourceBundle.getBundle(Messages.class.getPackage().getName() +".MessageBundle");
public String toString() {
return format();
}
public String format( Object... args ) {
return MessageFormat.format( rb.getString(name()), args );
}
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.jxc.ap;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.sun.tools.internal.xjc.BadCommandLineException;
/**
* This stores the invocation configuration for
* SchemaGenerator
*
* @author Bhakti Mehta
*/
public class Options {
public static final String DISABLE_XML_SECURITY = "-disableXmlSecurity";
// honor CLASSPATH environment variable, but it will be overrided by -cp
public String classpath = System.getenv("CLASSPATH");
public File targetDir = null;
public File episodeFile = null;
private boolean disableXmlSecurity = false;
// encoding is not required for JDK5, 6, but JDK 7 javac is much more strict - see issue 6859289
public String encoding = null;
public final List<String> arguments = new ArrayList<String>();
public void parseArguments(String[] args) throws BadCommandLineException {
for (int i = 0 ; i <args.length; i++) {
if (args[i].charAt(0)== '-') {
int j = parseArgument(args,i);
if(j==0)
throw new BadCommandLineException(
Messages.UNRECOGNIZED_PARAMETER.format(args[i]));
i += j;
} else {
arguments.add(args[i]);
}
}
}
private int parseArgument( String[] args, int i ) throws BadCommandLineException {
if (args[i].equals("-d")) {
if (i == args.length - 1)
throw new BadCommandLineException(
(Messages.OPERAND_MISSING.format(args[i])));
targetDir = new File(args[++i]);
if( !targetDir.exists() )
throw new BadCommandLineException(
Messages.NON_EXISTENT_FILE.format(targetDir));
return 1;
}
if (args[i].equals("-episode")) {
if (i == args.length - 1)
throw new BadCommandLineException(
(Messages.OPERAND_MISSING.format(args[i])));
episodeFile = new File(args[++i]);
return 1;
}
if (args[i].equals(DISABLE_XML_SECURITY)) {
if (i == args.length - 1)
throw new BadCommandLineException(
(Messages.OPERAND_MISSING.format(args[i])));
disableXmlSecurity = true;
return 1;
}
if (args[i].equals("-encoding")) {
if (i == args.length - 1)
throw new BadCommandLineException(
(Messages.OPERAND_MISSING.format(args[i])));
encoding = args[++i];
return 1;
}
if (args[i].equals("-cp") || args[i].equals("-classpath")) {
if (i == args.length - 1)
throw new BadCommandLineException(
(Messages.OPERAND_MISSING.format(args[i])));
classpath = args[++i];
return 1;
}
return 0;
}
/**
* @return the disableXmlSecurity
*/
public boolean isDisableXmlSecurity() {
return disableXmlSecurity;
}
}

View File

@@ -0,0 +1,152 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.jxc.ap;
import com.sun.tools.internal.jxc.api.JXC;
import com.sun.tools.internal.xjc.api.J2SJAXBModel;
import com.sun.tools.internal.xjc.api.Reference;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic;
import javax.tools.StandardLocation;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* {@link Processor} that implements the schema generator
* command line tool.
*
* @author Kohsuke Kawaguchi
*/
@SupportedAnnotationTypes("*")
public class SchemaGenerator extends AbstractProcessor {
/**
* User-specified schema locations, if any.
*/
private final Map<String,File> schemaLocations = new HashMap<String, File>();
private File episodeFile;
public SchemaGenerator() {
}
public SchemaGenerator( Map<String,File> m ) {
schemaLocations.putAll(m);
}
public void setEpisodeFile(File episodeFile) {
this.episodeFile = episodeFile;
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
final ErrorReceiverImpl errorListener = new ErrorReceiverImpl(processingEnv);
List<Reference> classes = new ArrayList<Reference>();
// simply ignore all the interface definitions,
// so that users won't have to manually exclude interfaces, which is silly.
filterClass(classes, roundEnv.getRootElements());
J2SJAXBModel model = JXC.createJavaCompiler().bind(classes, Collections.<QName, Reference>emptyMap(), null, processingEnv);
if (model == null)
return false; // error
try {
model.generateSchema(
new SchemaOutputResolver() {
public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
File file;
OutputStream out;
if (schemaLocations.containsKey(namespaceUri)) {
file = schemaLocations.get(namespaceUri);
if (file == null) return null; // don't generate
out = new FileOutputStream(file);
} else {
// use the default
file = new File(suggestedFileName);
out = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", suggestedFileName)
.openOutputStream();
file = file.getAbsoluteFile();
}
StreamResult ss = new StreamResult(out);
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Writing "+file);
ss.setSystemId(file.toURL().toExternalForm());
return ss;
}
}, errorListener);
if (episodeFile != null) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Writing "+episodeFile);
model.generateEpisodeFile(new StreamResult(episodeFile));
}
} catch (IOException e) {
errorListener.error(e.getMessage(), e);
}
return false;
}
private void filterClass(List<Reference> classes, Collection<? extends Element> elements) {
for (Element element : elements) {
if (element.getKind().equals(ElementKind.CLASS) || element.getKind().equals(ElementKind.ENUM)) {
classes.add(new Reference((TypeElement) element, processingEnv));
filterClass(classes, ElementFilter.typesIn(element.getEnclosedElements()));
}
}
}
@Override
public SourceVersion getSupportedSourceVersion() {
if (SourceVersion.latest().compareTo(SourceVersion.RELEASE_6) > 0)
return SourceVersion.valueOf("RELEASE_7");
else
return SourceVersion.RELEASE_6;
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.jxc.ap;
/**
* Class defined for safe calls of getClassLoader methods of any kind (context/system/class
* classloader. This MUST be package private and defined in every package which
* uses such invocations.
* @author snajper
*/
class SecureLoader {
static ClassLoader getContextClassLoader() {
if (System.getSecurityManager() == null) {
return Thread.currentThread().getContextClassLoader();
} else {
return java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return Thread.currentThread().getContextClassLoader();
}
});
}
}
static ClassLoader getClassClassLoader(final Class c) {
if (System.getSecurityManager() == null) {
return c.getClassLoader();
} else {
return java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return c.getClassLoader();
}
});
}
}
static ClassLoader getSystemClassLoader() {
if (System.getSecurityManager() == null) {
return ClassLoader.getSystemClassLoader();
} else {
return java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<ClassLoader>() {
public ClassLoader run() {
return ClassLoader.getSystemClassLoader();
}
});
}
}
}