#2491 Do not use types not part of java.base in MapStruct processor

MapStruct should not use types that are outside of java.base.
This makes sure that no additional dependencies (such as jaxb-api) are needed on the annotation processor path
This commit is contained in:
Filip Hrisafov 2021-06-19 17:11:04 +02:00
parent c5c292f602
commit fb9c7a3ded
24 changed files with 98 additions and 154 deletions

View File

@ -136,7 +136,11 @@ public class ProcessorInvocationInterceptor implements InvocationInterceptor {
private void configureProcessor(Verifier verifier) {
String compilerId = processorTestContext.getProcessor().getCompilerId();
if ( compilerId != null ) {
verifier.addCliOption( "-Pgenerate-via-compiler-plugin" );
String profile = processorTestContext.getProcessor().getProfile();
if ( profile == null ) {
profile = "generate-via-compiler-plugin";
}
verifier.addCliOption( "-P" + profile );
verifier.addCliOption( "-Dcompiler-id=" + compilerId );
}
else {

View File

@ -44,6 +44,7 @@ public @interface ProcessorTest {
enum ProcessorType {
JAVAC( "javac" ),
JAVAC_WITH_PATHS( "javac", JRE.OTHER, "generate-via-compiler-plugin-with-annotation-processor-paths" ),
ECLIPSE_JDT( "jdt", JRE.JAVA_8 ),
@ -51,14 +52,20 @@ public @interface ProcessorTest {
private final String compilerId;
private final JRE max;
private final String profile;
ProcessorType(String compilerId) {
this( compilerId, JRE.OTHER );
}
ProcessorType(String compilerId, JRE max) {
this( compilerId, max, null );
}
ProcessorType(String compilerId, JRE max, String profile) {
this.compilerId = compilerId;
this.max = max;
this.profile = profile;
}
public String getCompilerId() {
@ -68,6 +75,10 @@ public @interface ProcessorTest {
public JRE maxJre() {
return max;
}
public String getProfile() {
return profile;
}
}
/**
@ -98,6 +109,7 @@ public @interface ProcessorTest {
*/
ProcessorType[] processorTypes() default {
ProcessorType.JAVAC,
ProcessorType.JAVAC_WITH_PATHS,
ProcessorType.ECLIPSE_JDT,
ProcessorType.PROCESSOR_PLUGIN
};

View File

@ -65,6 +65,38 @@
</dependency>
</dependencies>
</profile>
<profile>
<id>generate-via-compiler-plugin-with-annotation-processor-paths</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument combine.self="override"></compilerArgument>
<compilerId>\${compiler-id}</compilerId>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>${project.groupId}</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
<dependencies>
<dependency>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-jdt</artifactId>
<version>${org.eclipse.tycho.compiler-jdt.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>generate-via-processor-plugin</id>
<activation>

View File

@ -75,7 +75,7 @@ final class DateFormatValidatorFactory {
private static boolean isXmlGregorianCalendarSupposedToBeMapped(Type sourceType, Type targetType) {
return typesEqualsOneOf(
sourceType, targetType, XmlConstants.JAVAX_XML_DATATYPE_XMLGREGORIAN_CALENDAR );
sourceType, targetType, XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR );
}
private static boolean isJodaDateTimeSupposed(Type sourceType, Type targetType) {

View File

@ -9,16 +9,12 @@ import static org.mapstruct.ap.internal.util.Collections.asSet;
import java.util.Set;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.ConstructorFragment;
import org.mapstruct.ap.internal.model.common.FieldReference;
import org.mapstruct.ap.internal.model.common.FinalField;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.Strings;
import org.mapstruct.ap.internal.util.XmlConstants;
/**
* @author Sjaak Derksen
@ -30,12 +26,12 @@ public abstract class AbstractToXmlGregorianCalendar extends BuiltInMethod {
private final Type dataTypeFactoryType;
public AbstractToXmlGregorianCalendar(TypeFactory typeFactory) {
this.returnType = typeFactory.getType( XMLGregorianCalendar.class );
this.dataTypeFactoryType = typeFactory.getType( DatatypeFactory.class );
this.returnType = typeFactory.getType( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR );
this.dataTypeFactoryType = typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_FACTORY );
this.importTypes = asSet(
returnType,
dataTypeFactoryType,
typeFactory.getType( DatatypeConfigurationException.class )
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONFIGURATION_EXCEPTION )
);
}
@ -51,7 +47,7 @@ public abstract class AbstractToXmlGregorianCalendar extends BuiltInMethod {
@Override
public FieldReference getFieldReference() {
return new FinalField( dataTypeFactoryType, Strings.decapitalize( DatatypeFactory.class.getSimpleName() ) );
return new FinalField( dataTypeFactoryType, "datatypeFactory" );
}
@Override

View File

@ -59,12 +59,11 @@ public class BuiltInMappingMethods {
}
private static boolean isJaxbAvailable(TypeFactory typeFactory) {
return JaxbConstants.isJaxbElementPresent() && typeFactory.isTypeAvailable( JaxbConstants.JAXB_ELEMENT_FQN );
return typeFactory.isTypeAvailable( JaxbConstants.JAXB_ELEMENT_FQN );
}
private static boolean isXmlGregorianCalendarAvailable(TypeFactory typeFactory) {
return XmlConstants.isXmlGregorianCalendarPresent() &&
typeFactory.isTypeAvailable( XmlConstants.JAVAX_XML_DATATYPE_XMLGREGORIAN_CALENDAR );
return typeFactory.isTypeAvailable( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR );
}
private static boolean isJodaTimeAvailable(TypeFactory typeFactory) {

View File

@ -9,11 +9,10 @@ import static org.mapstruct.ap.internal.util.Collections.asSet;
import java.util.Set;
import javax.xml.bind.JAXBElement;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.JaxbConstants;
/**
* @author Sjaak Derksen
@ -25,7 +24,7 @@ public class JaxbElemToValue extends BuiltInMethod {
private final Set<Type> importTypes;
public JaxbElemToValue(TypeFactory typeFactory) {
Type type = typeFactory.getType( JAXBElement.class );
Type type = typeFactory.getType( JaxbConstants.JAXB_ELEMENT_FQN );
this.parameter = new Parameter( "element", type );
this.returnType = type.getTypeParameters().get( 0 );
this.importTypes = asSet( parameter.getType() );

View File

@ -6,12 +6,12 @@
package org.mapstruct.ap.internal.model.source.builtin;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.JodaTimeConstants;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -28,7 +28,7 @@ public class JodaLocalDateTimeToXmlGregorianCalendar extends AbstractToXmlGregor
this.parameter = new Parameter( "dt", typeFactory.getType( JodaTimeConstants.LOCAL_DATE_TIME_FQN ) );
this.importTypes = asSet(
parameter.getType(),
typeFactory.getType( DatatypeConstants.class )
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONSTANTS )
);
}

View File

@ -6,12 +6,12 @@
package org.mapstruct.ap.internal.model.source.builtin;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.JodaTimeConstants;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -28,7 +28,7 @@ public class JodaLocalDateToXmlGregorianCalendar extends AbstractToXmlGregorianC
this.parameter = new Parameter( "dt", typeFactory.getType( JodaTimeConstants.LOCAL_DATE_FQN ) );
this.importTypes = asSet(
parameter.getType(),
typeFactory.getType( DatatypeConstants.class )
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONSTANTS )
);
}

View File

@ -6,12 +6,12 @@
package org.mapstruct.ap.internal.model.source.builtin;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.JodaTimeConstants;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -28,7 +28,7 @@ public class JodaLocalTimeToXmlGregorianCalendar extends AbstractToXmlGregorianC
this.parameter = new Parameter( "dt", typeFactory.getType( JodaTimeConstants.LOCAL_TIME_FQN ) );
this.importTypes = asSet(
parameter.getType(),
typeFactory.getType( DatatypeConstants.class )
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONSTANTS )
);
}

View File

@ -9,11 +9,10 @@ import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -30,7 +29,7 @@ public class LocalDateTimeToXmlGregorianCalendar extends AbstractToXmlGregorianC
this.parameter = new Parameter( "localDateTime", typeFactory.getType( LocalDateTime.class ) );
this.importTypes = asSet(
parameter.getType(),
typeFactory.getType( DatatypeConstants.class ),
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONSTANTS ),
typeFactory.getType( ChronoField.class )
);
}

View File

@ -7,11 +7,11 @@ package org.mapstruct.ap.internal.model.source.builtin;
import java.time.LocalDate;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -28,7 +28,7 @@ public class LocalDateToXmlGregorianCalendar extends AbstractToXmlGregorianCalen
this.parameter = new Parameter( "localDate", typeFactory.getType( LocalDate.class ) );
this.importTypes = asSet(
parameter.getType(),
typeFactory.getType( DatatypeConstants.class )
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONSTANTS )
);
}

View File

@ -7,11 +7,11 @@ package org.mapstruct.ap.internal.model.source.builtin;
import java.util.Calendar;
import java.util.Set;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -25,7 +25,7 @@ public class XmlGregorianCalendarToCalendar extends BuiltInMethod {
private final Set<Type> importTypes;
public XmlGregorianCalendarToCalendar(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.parameter = new Parameter( "xcal", typeFactory.getType( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR ) );
this.returnType = typeFactory.getType( Calendar.class );
this.importTypes = asSet( returnType, parameter.getType() );
}

View File

@ -7,11 +7,11 @@ package org.mapstruct.ap.internal.model.source.builtin;
import java.util.Date;
import java.util.Set;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -25,7 +25,7 @@ public class XmlGregorianCalendarToDate extends BuiltInMethod {
private final Set<Type> importTypes;
public XmlGregorianCalendarToDate(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.parameter = new Parameter( "xcal", typeFactory.getType( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR ) );
this.returnType = typeFactory.getType( Date.class );
this.importTypes = asSet( returnType, parameter.getType() );
}

View File

@ -6,13 +6,12 @@
package org.mapstruct.ap.internal.model.source.builtin;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.JodaTimeConstants;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -26,10 +25,10 @@ public class XmlGregorianCalendarToJodaDateTime extends BuiltInMethod {
private final Set<Type> importTypes;
public XmlGregorianCalendarToJodaDateTime(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.parameter = new Parameter( "xcal", typeFactory.getType( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR ) );
this.returnType = typeFactory.getType( JodaTimeConstants.DATE_TIME_FQN );
this.importTypes = asSet(
typeFactory.getType( DatatypeConstants.class ),
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONSTANTS ),
typeFactory.getType( JodaTimeConstants.DATE_TIME_ZONE_FQN ),
returnType,
parameter.getType() );

View File

@ -6,13 +6,12 @@
package org.mapstruct.ap.internal.model.source.builtin;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.JodaTimeConstants;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -26,10 +25,10 @@ public class XmlGregorianCalendarToJodaLocalDate extends BuiltInMethod {
private final Set<Type> importTypes;
public XmlGregorianCalendarToJodaLocalDate(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.parameter = new Parameter( "xcal", typeFactory.getType( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR ) );
this.returnType = typeFactory.getType( JodaTimeConstants.LOCAL_DATE_FQN );
this.importTypes = asSet(
typeFactory.getType( DatatypeConstants.class ),
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONSTANTS ),
returnType,
parameter.getType() );
}

View File

@ -6,13 +6,12 @@
package org.mapstruct.ap.internal.model.source.builtin;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.JodaTimeConstants;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -26,10 +25,10 @@ public class XmlGregorianCalendarToJodaLocalDateTime extends BuiltInMethod {
private final Set<Type> importTypes;
public XmlGregorianCalendarToJodaLocalDateTime(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.parameter = new Parameter( "xcal", typeFactory.getType( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR ) );
this.returnType = typeFactory.getType( JodaTimeConstants.LOCAL_DATE_TIME_FQN );
this.importTypes = asSet(
typeFactory.getType( DatatypeConstants.class ),
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONSTANTS ),
returnType,
parameter.getType() );
}

View File

@ -6,13 +6,12 @@
package org.mapstruct.ap.internal.model.source.builtin;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.JodaTimeConstants;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -26,10 +25,10 @@ public class XmlGregorianCalendarToJodaLocalTime extends BuiltInMethod {
private final Set<Type> importTypes;
public XmlGregorianCalendarToJodaLocalTime(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.parameter = new Parameter( "xcal", typeFactory.getType( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR ) );
this.returnType = typeFactory.getType( JodaTimeConstants.LOCAL_TIME_FQN );
this.importTypes = asSet(
typeFactory.getType( DatatypeConstants.class ),
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONSTANTS ),
returnType,
parameter.getType() );
}

View File

@ -7,11 +7,11 @@ package org.mapstruct.ap.internal.model.source.builtin;
import java.time.LocalDate;
import java.util.Set;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -25,7 +25,7 @@ public class XmlGregorianCalendarToLocalDate extends BuiltInMethod {
private final Set<Type> importTypes;
public XmlGregorianCalendarToLocalDate(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.parameter = new Parameter( "xcal", typeFactory.getType( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR ) );
this.returnType = typeFactory.getType( LocalDate.class );
this.importTypes = asSet( returnType, parameter.getType() );
}

View File

@ -9,12 +9,10 @@ import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Set;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -28,12 +26,12 @@ public class XmlGregorianCalendarToLocalDateTime extends BuiltInMethod {
private final Set<Type> importTypes;
public XmlGregorianCalendarToLocalDateTime(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.parameter = new Parameter( "xcal", typeFactory.getType( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR ) );
this.returnType = typeFactory.getType( LocalDateTime.class );
this.importTypes = asSet(
returnType,
parameter.getType(),
typeFactory.getType( DatatypeConstants.class ),
typeFactory.getType( XmlConstants.JAVAX_XML_DATATYPE_CONSTANTS ),
typeFactory.getType( Duration.class )
);
}

View File

@ -8,12 +8,12 @@ package org.mapstruct.ap.internal.model.source.builtin;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import javax.xml.datatype.XMLGregorianCalendar;
import org.mapstruct.ap.internal.model.common.ConversionContext;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.common.TypeFactory;
import org.mapstruct.ap.internal.util.XmlConstants;
import static org.mapstruct.ap.internal.util.Collections.asSet;
@ -27,7 +27,7 @@ public class XmlGregorianCalendarToString extends BuiltInMethod {
private final Set<Type> importTypes;
public XmlGregorianCalendarToString(TypeFactory typeFactory) {
this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) );
this.parameter = new Parameter( "xcal", typeFactory.getType( XmlConstants.JAVAX_XML_XML_GREGORIAN_CALENDAR ) );
this.returnType = typeFactory.getType( String.class );
this.importTypes = asSet(
parameter.getType(),

View File

@ -1,76 +0,0 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.internal.util;
/**
* Utilities for working with classes. It is mainly needed because using the {@link ElementUtils}
* is not always correct. For example when compiling with JDK 9 and source version 8 classes from different modules
* are available by {@link ElementUtils#getTypeElement(CharSequence)} but they are actually not
* if those modules are not added during compilation.
*
* @author Filip Hrisafov
*/
class ClassUtils {
private ClassUtils() {
}
/**
* Determine whether the {@link Class} identified by the supplied name is present
* and can be loaded. Will return {@code false} if either the class or
* one of its dependencies is not present or cannot be loaded.
*
* @param className the name of the class to check
* @param classLoader the class loader to use
* (may be {@code null}, which indicates the default class loader)
*
* @return whether the specified class is present
*/
static boolean isPresent(String className, ClassLoader classLoader) {
try {
ClassLoader classLoaderToUse = classLoader;
if ( classLoaderToUse == null ) {
classLoaderToUse = getDefaultClassLoader();
}
classLoaderToUse.loadClass( className );
return true;
}
catch ( ClassNotFoundException ex ) {
// Class or one of its dependencies is not present...
return false;
}
}
/**
* Return the default ClassLoader to use: typically the thread context
* ClassLoader, if available; the ClassLoader that loaded the ClassUtils
* class will be used as fallback.
* <p>Call this method if you intend to use the thread context ClassLoader
* in a scenario where you absolutely need a non-null ClassLoader reference:
* for example, for class path resource loading (but not necessarily for
* {@code Class.forName}, which accepts a {@code null} ClassLoader
* reference as well).
*
* @return the default ClassLoader (never {@code null})
*
* @see Thread#getContextClassLoader()
*/
private static ClassLoader getDefaultClassLoader() {
ClassLoader cl = null;
try {
cl = Thread.currentThread().getContextClassLoader();
}
catch ( Throwable ex ) {
// Cannot access thread context ClassLoader - falling back to system class loader...
}
if ( cl == null ) {
// No thread context class loader -> use class loader of this class.
cl = ClassUtils.class.getClassLoader();
}
return cl;
}
}

View File

@ -11,18 +11,8 @@ package org.mapstruct.ap.internal.util;
public final class JaxbConstants {
public static final String JAXB_ELEMENT_FQN = "javax.xml.bind.JAXBElement";
private static final boolean IS_JAXB_ELEMENT_PRESENT = ClassUtils.isPresent(
JAXB_ELEMENT_FQN,
JaxbConstants.class.getClassLoader()
);
private JaxbConstants() {
}
/**
* @return {@code true} if {@link javax.xml.bind.JAXBElement} is present, {@code false} otherwise
*/
public static boolean isJaxbElementPresent() {
return IS_JAXB_ELEMENT_PRESENT;
}
}

View File

@ -12,19 +12,14 @@ package org.mapstruct.ap.internal.util;
*/
public final class XmlConstants {
public static final String JAVAX_XML_DATATYPE_XMLGREGORIAN_CALENDAR = "javax.xml.datatype.XMLGregorianCalendar";
private static final boolean IS_XML_GREGORIAN_CALENDAR_PRESENT = ClassUtils.isPresent(
JAVAX_XML_DATATYPE_XMLGREGORIAN_CALENDAR,
XmlConstants.class.getClassLoader()
);
// CHECKSTYLE:OFF
public static final String JAVAX_XML_XML_GREGORIAN_CALENDAR = "javax.xml.datatype.XMLGregorianCalendar";
public static final String JAVAX_XML_DATATYPE_CONFIGURATION_EXCEPTION = "javax.xml.datatype.DatatypeConfigurationException";
public static final String JAVAX_XML_DATATYPE_FACTORY = "javax.xml.datatype.DatatypeFactory";
public static final String JAVAX_XML_DATATYPE_CONSTANTS = "javax.xml.datatype.DatatypeConstants";
// CHECKSTYLE:ON
private XmlConstants() {
}
/**
* @return {@code true} if the {@link javax.xml.datatype.XMLGregorianCalendar} is present, {@code false} otherwise
*/
public static boolean isXmlGregorianCalendarPresent() {
return IS_XML_GREGORIAN_CALENDAR_PRESENT;
}
}