Skip to content

Commit

Permalink
Remove reflection so that GraalVM native-image does not need a hint.
Browse files Browse the repository at this point in the history
  • Loading branch information
flx5 committed Jun 9, 2024
1 parent b7d1ff7 commit 168bf0d
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.glassfish.jaxb.runtime.v2.model.runtime.RuntimeAttributePropertyInfo;
import org.glassfish.jaxb.runtime.v2.model.runtime.RuntimeNonElement;
import org.glassfish.jaxb.runtime.v2.model.runtime.RuntimePropertyInfo;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
import org.glassfish.jaxb.runtime.v2.runtime.property.AttributeProperty;
import org.glassfish.jaxb.runtime.v2.runtime.property.Property;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.glassfish.jaxb.runtime.v2.model.runtime.RuntimeNonElement;
import org.glassfish.jaxb.runtime.v2.model.runtime.RuntimePropertyInfo;
import org.glassfish.jaxb.runtime.v2.model.runtime.RuntimeValuePropertyInfo;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
import org.glassfish.jaxb.runtime.v2.runtime.property.Property;
import org.glassfish.jaxb.runtime.v2.runtime.property.ValueProperty;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
package org.glassfish.jaxb.runtime.v2.model.runtime;

import org.glassfish.jaxb.core.v2.model.core.AttributePropertyInfo;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
import org.glassfish.jaxb.runtime.v2.runtime.property.AttributeProperty;
import org.glassfish.jaxb.runtime.v2.runtime.property.Property;

import java.lang.reflect.Type;

Expand All @@ -21,4 +24,10 @@ public interface RuntimeAttributePropertyInfo extends AttributePropertyInfo<Type
// refinement
@Override
RuntimeNonElement getTarget();


@Override
default Property<?> create(JAXBContextImpl grammar) {
return new AttributeProperty<>(grammar, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
package org.glassfish.jaxb.runtime.v2.model.runtime;

import org.glassfish.jaxb.core.v2.model.core.ElementPropertyInfo;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
import org.glassfish.jaxb.runtime.v2.runtime.property.ArrayElementLeafProperty;
import org.glassfish.jaxb.runtime.v2.runtime.property.ArrayElementNodeProperty;
import org.glassfish.jaxb.runtime.v2.runtime.property.ListElementProperty;
import org.glassfish.jaxb.runtime.v2.runtime.property.Property;
import org.glassfish.jaxb.runtime.v2.runtime.property.PropertyFactory;
import org.glassfish.jaxb.runtime.v2.runtime.property.SingleElementLeafProperty;
import org.glassfish.jaxb.runtime.v2.runtime.property.SingleElementNodeProperty;

import java.lang.reflect.Type;
import java.util.Collection;
Expand All @@ -25,4 +33,19 @@ public interface RuntimeElementPropertyInfo extends ElementPropertyInfo<Type,Cla

@Override
List<? extends RuntimeTypeRef> getTypes();

@Override
default Property<?> create(JAXBContextImpl grammar) {
if(this.isValueList())
return new ListElementProperty<>(grammar, this);

boolean isLeaf = PropertyFactory.isLeaf(this);
boolean isCollection = this.isCollection();

if (isLeaf) {
return isCollection ? new ArrayElementLeafProperty<>(grammar, this) : new SingleElementLeafProperty<>(grammar, this);
}

return isCollection ? new ArrayElementNodeProperty<>(grammar, this) : new SingleElementNodeProperty<>(grammar, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
package org.glassfish.jaxb.runtime.v2.model.runtime;

import org.glassfish.jaxb.core.v2.model.core.MapPropertyInfo;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
import org.glassfish.jaxb.runtime.v2.runtime.property.AttributeProperty;
import org.glassfish.jaxb.runtime.v2.runtime.property.Property;
import org.glassfish.jaxb.runtime.v2.runtime.property.SingleMapNodeProperty;

import java.lang.reflect.Type;

Expand All @@ -22,4 +26,10 @@ public interface RuntimeMapPropertyInfo extends RuntimePropertyInfo, MapProperty
RuntimeNonElement getKeyType();
@Override
RuntimeNonElement getValueType();


@Override
default Property<?> create(JAXBContextImpl grammar) {
return new SingleMapNodeProperty<>(grammar, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.glassfish.jaxb.core.v2.model.core.PropertyInfo;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
import org.glassfish.jaxb.runtime.v2.runtime.property.Property;
import org.glassfish.jaxb.runtime.v2.runtime.reflect.Accessor;

import java.lang.reflect.Type;
Expand Down Expand Up @@ -78,4 +79,6 @@ public interface RuntimePropertyInfo extends PropertyInfo<Type,Class> {
* @return always non-null.
*/
Type getIndividualType();

Property<?> create(JAXBContextImpl grammar);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
package org.glassfish.jaxb.runtime.v2.model.runtime;

import org.glassfish.jaxb.core.v2.model.core.ReferencePropertyInfo;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
import org.glassfish.jaxb.runtime.v2.runtime.property.ArrayReferenceNodeProperty;
import org.glassfish.jaxb.runtime.v2.runtime.property.Property;
import org.glassfish.jaxb.runtime.v2.runtime.property.SingleReferenceNodeProperty;

import java.lang.reflect.Type;
import java.util.Set;
Expand All @@ -21,4 +25,10 @@
public interface RuntimeReferencePropertyInfo extends ReferencePropertyInfo<Type,Class>, RuntimePropertyInfo {
@Override
Set<? extends RuntimeElement> getElements();

@Override
default Property<?> create(JAXBContextImpl grammar) {
boolean isCollection = this.isCollection();
return isCollection ? new ArrayReferenceNodeProperty<>(grammar, this) : new SingleReferenceNodeProperty<>(grammar, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
package org.glassfish.jaxb.runtime.v2.model.runtime;

import org.glassfish.jaxb.core.v2.model.core.ValuePropertyInfo;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;
import org.glassfish.jaxb.runtime.v2.runtime.property.Property;
import org.glassfish.jaxb.runtime.v2.runtime.property.ValueProperty;

import java.lang.reflect.Type;

Expand All @@ -20,4 +23,9 @@
public interface RuntimeValuePropertyInfo extends ValuePropertyInfo<Type,Class>,RuntimePropertyInfo,RuntimeNonElementRef {
@Override
RuntimeNonElement getTarget();

@Override
default Property<?> create(JAXBContextImpl grammar) {
return new ValueProperty<>(grammar, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @author Kohsuke Kawaguchi (kk@kohsuke.org)
*/
final class ArrayElementLeafProperty<BeanT,ListT,ItemT> extends ArrayElementProperty<BeanT,ListT,ItemT> {
public final class ArrayElementLeafProperty<BeanT,ListT,ItemT> extends ArrayElementProperty<BeanT,ListT,ItemT> {

private final Transducer<ItemT> xducer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author Kohsuke Kawaguchi
*/
final class ArrayElementNodeProperty<BeanT,ListT,ItemT> extends ArrayElementProperty<BeanT,ListT,ItemT> {
public final class ArrayElementNodeProperty<BeanT,ListT,ItemT> extends ArrayElementProperty<BeanT,ListT,ItemT> {

public ArrayElementNodeProperty(JAXBContextImpl p, RuntimeElementPropertyInfo prop) {
super(p, prop);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* @author Kohsuke Kawaguchi
*/
class ArrayReferenceNodeProperty<BeanT,ListT,ItemT> extends ArrayERProperty<BeanT,ListT,ItemT> {
public class ArrayReferenceNodeProperty<BeanT,ListT,ItemT> extends ArrayERProperty<BeanT,ListT,ItemT> {

/**
* Expected element names and what class to unmarshal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*
* @author Kohsuke Kawaguchi
*/
final class ListElementProperty<BeanT,ListT,ItemT> extends ArrayProperty<BeanT,ListT,ItemT> {
public final class ListElementProperty<BeanT,ListT,ItemT> extends ArrayProperty<BeanT,ListT,ItemT> {

private final Name tagName;
private final String defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@

import org.glassfish.jaxb.core.v2.model.core.ClassInfo;
import org.glassfish.jaxb.core.v2.model.core.ID;
import org.glassfish.jaxb.core.v2.model.core.PropertyKind;
import org.glassfish.jaxb.runtime.v2.model.runtime.*;
import org.glassfish.jaxb.runtime.v2.runtime.JAXBContextImpl;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;

/**
Expand All @@ -28,90 +25,19 @@
public abstract class PropertyFactory {
private PropertyFactory() {}


/**
* Constructors of the {@link Property} implementation.
*/
private static final Constructor<? extends Property>[] propImpls;

static {
Class<? extends Property>[] implClasses = new Class[] {
SingleElementLeafProperty.class,
null, // single reference leaf --- but there's no such thing as "reference leaf"
null, // no such thing as "map leaf"

ArrayElementLeafProperty.class,
null, // array reference leaf --- but there's no such thing as "reference leaf"
null, // no such thing as "map leaf"

SingleElementNodeProperty.class,
SingleReferenceNodeProperty.class,
SingleMapNodeProperty.class,

ArrayElementNodeProperty.class,
ArrayReferenceNodeProperty.class,
null, // map is always a single property (Map doesn't implement Collection)
};

propImpls = new Constructor[implClasses.length];
for( int i=0; i<propImpls.length; i++ ) {
if(implClasses[i]!=null)
// this pointless casting necessary for Mustang
propImpls[i] = (Constructor)implClasses[i].getConstructors()[0];
}
}

/**
* Creates/obtains a properly configured {@link Property}
* object from the given description.
*/
public static Property create( JAXBContextImpl grammar, RuntimePropertyInfo info ) {

PropertyKind kind = info.kind();

switch(kind) {
case ATTRIBUTE:
return new AttributeProperty(grammar,(RuntimeAttributePropertyInfo)info);
case VALUE:
return new ValueProperty(grammar,(RuntimeValuePropertyInfo)info);
case ELEMENT:
if(((RuntimeElementPropertyInfo)info).isValueList())
return new ListElementProperty(grammar,(RuntimeElementPropertyInfo) info);
break;
case REFERENCE:
case MAP:
break;
default:
assert false;
}


boolean isCollection = info.isCollection();
boolean isLeaf = isLeaf(info);

Constructor<? extends Property> c = propImpls[(isLeaf?0:6)+(isCollection?3:0)+kind.propertyIndex];
try {
return c.newInstance( grammar, info );
} catch (InstantiationException e) {
throw new InstantiationError(e.getMessage());
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if(t instanceof Error)
throw (Error)t;
if(t instanceof RuntimeException)
throw (RuntimeException)t;

throw new AssertionError(t);
}
public static Property create( JAXBContextImpl grammar, RuntimePropertyInfo info) {
return info.create(grammar);
}

/**
* Look for the case that can be optimized as a leaf,
* which is a kind of type whose XML representation is just PCDATA.
*/
static boolean isLeaf(RuntimePropertyInfo info) {
public static boolean isLeaf(RuntimePropertyInfo info) {
Collection<? extends RuntimeTypeInfo> types = info.ref();
if(types.size()!=1) return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @author Kohsuke Kawaguchi (kk@kohsuke.org)
*/
final class SingleElementLeafProperty<BeanT> extends PropertyImpl<BeanT> {
public final class SingleElementLeafProperty<BeanT> extends PropertyImpl<BeanT> {

private final Name tagName;
private final boolean nillable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
/**
* @author Kohsuke Kawaguchi (kk@kohsuke.org)
*/
final class SingleElementNodeProperty<BeanT,ValueT> extends PropertyImpl<BeanT> {
public final class SingleElementNodeProperty<BeanT,ValueT> extends PropertyImpl<BeanT> {

private final Accessor<BeanT,ValueT> acc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
/**
* @author Kohsuke Kawaguchi
*/
final class SingleMapNodeProperty<BeanT,ValueT extends Map> extends PropertyImpl<BeanT> {
public final class SingleMapNodeProperty<BeanT,ValueT extends Map> extends PropertyImpl<BeanT> {

private final Accessor<BeanT,ValueT> acc;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/**
* @author Kohsuke Kawaguchi
*/
final class SingleReferenceNodeProperty<BeanT,ValueT> extends PropertyImpl<BeanT> {
public final class SingleReferenceNodeProperty<BeanT,ValueT> extends PropertyImpl<BeanT> {

private final Accessor<BeanT,ValueT> acc;

Expand Down

0 comments on commit 168bf0d

Please sign in to comment.