Skip to content

Commit

Permalink
#1761: Move JavaCompiler, Reference and J2SJAXBModel from XJC API to …
Browse files Browse the repository at this point in the history
…JXC API

Signed-off-by: Lukas Jungmann <[email protected]>
  • Loading branch information
lukasj committed Oct 7, 2023
1 parent f090f02 commit 724304f
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
<listitem><para>
<link xlink:href="https://github.com/eclipse-ee4j/jaxb-ri/issues/1757">#1757</link>: txw runtime and compiler should have different module names
</para></listitem>
<listitem><para>
<link xlink:href="https://github.com/eclipse-ee4j/jaxb-ri/issues/1761">#1761</link>: Move JavaCompiler, Reference and J2SJAXBModel from XJC API to JXC API
</para></listitem>
</itemizedlist>
</para></listitem>
</itemizedlist>
Expand Down
16 changes: 9 additions & 7 deletions jaxb-ri/jxc/src/main/java/com/sun/tools/jxc/ConfigReader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -10,7 +10,6 @@

package com.sun.tools.jxc;

import com.sun.tools.jxc.ap.Options;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
Expand All @@ -22,22 +21,25 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jakarta.xml.bind.SchemaOutputResolver;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import javax.xml.validation.ValidatorHandler;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;
import jakarta.xml.bind.SchemaOutputResolver;

import org.glassfish.jaxb.core.v2.util.XmlFactory;

import com.sun.tools.jxc.ap.Options;
import com.sun.tools.jxc.api.Reference;
import com.sun.tools.jxc.gen.config.Config;
import com.sun.tools.jxc.gen.config.Schema;
import com.sun.tools.xjc.SchemaCache;
import com.sun.tools.xjc.api.Reference;
import com.sun.tools.xjc.util.ForkContentHandler;

import org.glassfish.jaxb.core.v2.util.XmlFactory;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -13,8 +13,8 @@
import com.sun.tools.jxc.ConfigReader;
import com.sun.tools.jxc.api.JXC;
import com.sun.tools.xjc.ErrorReceiver;
import com.sun.tools.xjc.api.J2SJAXBModel;
import com.sun.tools.xjc.api.Reference;
import com.sun.tools.jxc.api.J2SJAXBModel;
import com.sun.tools.jxc.api.Reference;
import org.xml.sax.SAXException;

import javax.annotation.processing.AbstractProcessor;
Expand Down Expand Up @@ -100,7 +100,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

Collection<Reference> classesToBeIncluded = configReader.getClassesToBeIncluded();
J2SJAXBModel model = JXC.createJavaCompiler().bind(
classesToBeIncluded, Collections.emptyMap(), null, processingEnv);
classesToBeIncluded, Collections.emptyMap(), processingEnv, null);

SchemaOutputResolver schemaOutputResolver = configReader.getSchemaOutputResolver();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -10,9 +10,9 @@

package com.sun.tools.jxc.ap;

import com.sun.tools.jxc.api.J2SJAXBModel;
import com.sun.tools.jxc.api.JXC;
import com.sun.tools.xjc.api.J2SJAXBModel;
import com.sun.tools.xjc.api.Reference;
import com.sun.tools.jxc.api.Reference;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
Expand Down Expand Up @@ -70,6 +70,7 @@ public void setEpisodeFile(File episodeFile) {
}

@Override
@SuppressWarnings({"unchecked"})
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
final ErrorReceiverImpl errorListener = new ErrorReceiverImpl(processingEnv);

Expand All @@ -78,7 +79,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
// so that users won't have to manually exclude interfaces, which is silly.
filterClass(classesToBeBound, roundEnv.getRootElements());

J2SJAXBModel model = JXC.createJavaCompiler().bind(classesToBeBound, Collections.emptyMap(), null, processingEnv);
J2SJAXBModel model = JXC.createJavaCompiler().bind(classesToBeBound, Collections.emptyMap(), processingEnv, null);
if (model == null)
return false; // error

Expand Down
74 changes: 74 additions & 0 deletions jaxb-ri/jxc/src/main/java/com/sun/tools/jxc/api/J2SJAXBModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package com.sun.tools.jxc.api;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.JAXBModel;
import jakarta.xml.bind.SchemaOutputResolver;

import javax.annotation.processing.ProcessingEnvironment;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;

/**
* {@link JAXBModel} that exposes additional information available
* only for the {@code java -> schema} direction.
*
* @author Kohsuke Kawaguchi
*/
@SuppressWarnings({"removal"})
public interface J2SJAXBModel extends com.sun.tools.xjc.api.J2SJAXBModel {
/**
* Returns the name of the XML Type bound to the
* specified Java type.
*
* @param javaType
* must not be null. This must be one of the {@link Reference}s specified
* in the {@link JavaCompiler#bind(Collection, Map, ProcessingEnvironment, String)} method.
*
* @return
* null if it is not a part of the input to {@link JavaCompiler#bind(Collection, Map, ProcessingEnvironment, String)}.
*
* @throws IllegalArgumentException
* if the parameter is null
*/
QName getXmlTypeName(Reference javaType);

/**
* Generates the schema documents from the model.
*
* @param outputResolver
* this object controls the output to which schemas
* will be sent.
*
* @throws IOException
* if {@link SchemaOutputResolver} throws an {@link IOException}.
*/
void generateSchema(SchemaOutputResolver outputResolver, ErrorListener errorListener) throws IOException;

/**
* Generates the episode file from the model.
*
* <p>
* The "episode file" is really just a JAXB customization file (but with vendor extensions,
* at this point), that can be used later with a schema compilation to support separate
* compilation.
*
* @param output
* This receives the generated episode file.
* @since 2.1
*/
void generateEpisodeFile(Result output);
}
3 changes: 1 addition & 2 deletions jaxb-ri/jxc/src/main/java/com/sun/tools/jxc/api/JXC.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -10,7 +10,6 @@

package com.sun.tools.jxc.api;

import com.sun.tools.xjc.api.JavaCompiler;
import com.sun.tools.jxc.api.impl.j2s.JavaCompilerImpl;

/**
Expand Down
69 changes: 69 additions & 0 deletions jaxb-ri/jxc/src/main/java/com/sun/tools/jxc/api/JavaCompiler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package com.sun.tools.jxc.api;

import java.util.Collection;
import java.util.Map;

import javax.xml.namespace.QName;

import javax.annotation.processing.ProcessingEnvironment;


/**
* Java-to-Schema compiler.
*
* @author
* Kohsuke Kawaguchi ([email protected])
*/
@SuppressWarnings({"removal"})
public interface JavaCompiler extends com.sun.tools.xjc.api.JavaCompiler {

/**
* Compiles the given annotated Java source code.
*
* <p>
* This operation takes a set of "root types", then compute the list of
* all the types that need to be bound by forming a transitive reflexive
* closure of types that are referenced by the root types.
*
* <p>
* Errors will be sent to {@link javax.annotation.processing.ProcessingEnvironment#getMessager()}.
*
* @param rootTypes
* The list of types that needs to be bound to XML.
* "root references" from JAX-RPC to JAXB is always in the form of (type,annotations) pair.
*
* @param additionalElementDecls
* Add element declarations for the specified element names to
* the XML types mapped from the corresponding {@link Reference}s.
* Those {@link Reference}s must be included in the {@code rootTypes} parameter.
* In this map, a {@link Reference} can be null, in which case the element name is
* declared to have an empty complex type.
* ({@code <xs:element name='foo'><xs:complexType/></xs:element>})
* This parameter can be null, in which case the method behaves as if the empty map is given.
*
* @param defaultNamespaceRemap
* If not-null, all the uses of the empty default namespace ("") will
* be replaced by this namespace URI.
*
* @param source
* The caller supplied view to the annotated source code that JAXB is going to process.
*
* @return
* Non-null if no error was reported. Otherwise null.
*/
J2SJAXBModel bind(
Collection<Reference> rootTypes,
Map<QName, Reference> additionalElementDecls,
ProcessingEnvironment source,
String defaultNamespaceRemap);
}
93 changes: 93 additions & 0 deletions jaxb-ri/jxc/src/main/java/com/sun/tools/jxc/api/Reference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package com.sun.tools.jxc.api;

import javax.annotation.processing.ProcessingEnvironment;
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.TypeMirror;

/**
* Reference to a JAXB type (from JAX-RPC.)
*
* <p>
* A reference is a Java type (represented as a {@link javax.lang.model.type.TypeMirror})
* and a set of annotations (represented as a {@link javax.lang.model.element.Element}).
* Together they describe a root reference to a JAXB type binding.
*
* <p>
* Those two values can be supplied independently, or you can use
* other convenience constructors to supply two values at once.
*
*
* @author Kohsuke Kawaguchi
*/
public final class Reference {
/**
* The JAXB type being referenced. Must not be null.
*/
public final TypeMirror type;
/**
* The declaration from which annotations for the {@link #type} is read.
* Must not be null.
*/
public final Element annotations;

/**
* Creates a reference from the return type of the method
* and annotations on the method.
*/
public Reference(ExecutableElement method) {
this(method.getReturnType(),method);
}

/**
* Creates a reference from the parameter type
* and annotations on the parameter.
*/
public Reference(VariableElement param) {
this(param.asType(), param);
}

/**
* Creates a reference from a class declaration and its annotations.
*/
public Reference(TypeElement type, ProcessingEnvironment env) {
this(env.getTypeUtils().getDeclaredType(type),type);
}

/**
* Creates a reference by providing two values independently.
*/
public Reference(TypeMirror type, Element annotations) {
if(type==null || annotations==null)
throw new IllegalArgumentException();
this.type = type;
this.annotations = annotations;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Reference)) return false;

final Reference that = (Reference) o;

return annotations.equals(that.annotations) && type.equals(that.type);
}

@Override
public int hashCode() {
return 29 * type.hashCode() + annotations.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.J2SJAXBModel;
import com.sun.tools.xjc.api.Reference;
import com.sun.tools.jxc.api.J2SJAXBModel;
import com.sun.tools.jxc.api.Reference;
import org.glassfish.jaxb.core.v2.model.annotation.AnnotationReader;
import org.glassfish.jaxb.core.v2.model.core.ArrayInfo;
import org.glassfish.jaxb.core.v2.model.core.ClassInfo;
Expand Down Expand Up @@ -152,6 +152,12 @@ private NonElement<TypeMirror, TypeElement> getXmlType(Reference r) {
return types.getTypeInfo(ref);
}

@Override
@SuppressWarnings({"removal"})
public QName getXmlTypeName(com.sun.tools.xjc.api.Reference javaType) {
return getXmlTypeName(new Reference(javaType.type, javaType.annotations));
}

@Override
public void generateSchema(SchemaOutputResolver outputResolver, ErrorListener errorListener) throws IOException {
getSchemaGenerator().write(outputResolver,errorListener);
Expand Down
Loading

0 comments on commit 724304f

Please sign in to comment.