org.apache.cxf.common.util.ReflectionUtil Java Examples

The following examples show how to use org.apache.cxf.common.util.ReflectionUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataWriterImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static void setContextualNamespaceDecls(Object mapper, Map<String, String> nsctxt) {
    try {
        Method m = ReflectionUtil.getDeclaredMethod(mapper.getClass(),
                                                    "setContextualNamespaceDecls", new Class<?>[]{String[].class});
        String[] args = new String[nsctxt.size() * 2];
        int ai = 0;
        for (Entry<String, String> nsp : nsctxt.entrySet()) {
            args[ai++] = nsp.getValue();
            args[ai++] = nsp.getKey();
        }
        m.invoke(mapper, new Object[]{args});
    } catch (Exception e) {
        // ignore
        LOG.log(Level.WARNING, "Failed to set the contextual namespace map", e);
    }

}
 
Example #2
Source File: ClientProxyImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected boolean getValuesFromBeanParamField(Object bean,
                                            Field f,
                                            Class<? extends Annotation> annClass,
                                            Map<String, BeanPair> values) {
    boolean jaxrsParamAnnAvailable = false;
    Annotation fieldAnnotation = f.getAnnotation(annClass);
    if (fieldAnnotation != null) {
        jaxrsParamAnnAvailable = true;
        Object value = ReflectionUtil.accessDeclaredField(f, bean, Object.class);
        if (value != null) {
            String annotationValue = AnnotationUtils.getAnnotationValue(fieldAnnotation);
            values.put(annotationValue, new BeanPair(value, f.getAnnotations()));
        }
    }
    return jaxrsParamAnnAvailable;
}
 
Example #3
Source File: URLConnectionHTTPConduit.java    From cxf with Apache License 2.0 6 votes vote down vote up
private OutputStream connectAndGetOutputStream(Boolean b) throws IOException {
    OutputStream cout = null;

    if (b != null && b) {
        String method = connection.getRequestMethod();
        connection.connect();
        try {
            java.lang.reflect.Field f = ReflectionUtil.getDeclaredField(HttpURLConnection.class, "method");
            ReflectionUtil.setAccessible(f).set(connection, "POST");
            cout = connection.getOutputStream();
            ReflectionUtil.setAccessible(f).set(connection, method);
        } catch (Throwable t) {
            logStackTrace(t);
        }

    } else {
        cout = connection.getOutputStream();
    }
    return cout;
}
 
Example #4
Source File: JettyContinuationProviderFactory.java    From cxf with Apache License 2.0 6 votes vote down vote up
public ContinuationProvider createContinuationProvider(Message inMessage,
                                                       HttpServletRequest req,
                                                       HttpServletResponse resp) {
    if (!disableJettyContinuations) {
        ServletRequest r2 = req;
        while (r2 instanceof ServletRequestWrapper) {
            r2 = ((ServletRequestWrapper)r2).getRequest();
        }
        if (!r2.getClass().getName().contains("jetty")) {
            return null;
        }

        try {
            Method m = r2.getClass().getMethod("isAsyncSupported");
            Object o = ReflectionUtil.setAccessible(m).invoke(r2);
            if (((Boolean)o).booleanValue()) {
                return new JettyContinuationProvider(req, resp, inMessage);
            }
        } catch (Throwable t) {
            //ignore - either not a proper Jetty request object or classloader issue
            //or similar.
        }
    }
    return null;
}
 
Example #5
Source File: BeanResourceInfo.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void setParamField(Class<?> cls) {
    if (Object.class == cls || cls == null) {
        return;
    }
    for (Field f : ReflectionUtil.getDeclaredFields(cls)) {
        for (Annotation a : f.getAnnotations()) {
            if (AnnotationUtils.isParamAnnotationClass(a.annotationType())) {
                if (paramFields == null) {
                    paramFields = new ArrayList<>();
                }
                paramsAvailable = true;
                paramFields.add(f);
            }
        }
    }
    setParamField(cls.getSuperclass());
}
 
Example #6
Source File: AbstractResourceInfo.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void findContextFields(Class<?> cls, Object provider) {
    if (cls == Object.class || cls == null) {
        return;
    }
    for (Field f : ReflectionUtil.getDeclaredFields(cls)) {
        for (Annotation a : f.getAnnotations()) {
            if (a.annotationType() == Context.class
                && (f.getType().isInterface() || f.getType() == Application.class)) {
                contextFields = addContextField(contextFields, f);
                checkContextClass(f.getType());
                if (!InjectionUtils.VALUE_CONTEXTS.contains(f.getType().getName())) {
                    addToMap(getFieldProxyMap(true), f, getFieldThreadLocalProxy(f, provider));
                }
            }
        }
    }
    findContextFields(cls.getSuperclass(), provider);
}
 
Example #7
Source File: JAXRSUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static Response copyResponseIfNeeded(Response response) {
    if (!(response instanceof ResponseImpl)) {
        Response r = fromResponse(response).build();
        Field[] declaredFields = ReflectionUtil.getDeclaredFields(response.getClass());
        for (Field f : declaredFields) {
            Class<?> declClass = f.getType();
            if (declClass == Annotation[].class) {
                try {
                    Annotation[] fieldAnnotations =
                        ReflectionUtil.accessDeclaredField(f, response, Annotation[].class);
                    ((ResponseImpl)r).setEntityAnnotations(fieldAnnotations);
                } catch (Throwable ex) {
                    LOG.warning("Custom annotations if any can not be copied");
                }
                break;
            }
        }
        return r;
    }
    return response;
}
 
Example #8
Source File: JAXBUtilsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetNamespaceMapper() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(GreetMe.class);
    Marshaller marshaller = ctx.createMarshaller();
    Map<String, String> nspref = new HashMap<>();
    nspref.put("http://cxf.apache.org/hello_world_soap_http/types", "x");
    JAXBUtils.setNamespaceMapper(nspref, marshaller);
    String mapperkey = null;
    if (marshaller.getClass().getName().contains(".internal.")) {
        mapperkey = "com.sun.xml.internal.bind.namespacePrefixMapper";
    } else if (marshaller.getClass().getName().contains("com.sun")) {
        mapperkey = "com.sun.xml.bind.namespacePrefixMapper";
    } else if (marshaller.getClass().getName().contains("eclipse")) {
        mapperkey = "eclipselink.namespace-prefix-mapper";
    }
    if (mapperkey != null) {
        Object mapper = marshaller.getProperty(mapperkey);
        assertNotNull(mapper);

        // also verify this mapper has setContextualNamespaceDecls
        Method m = ReflectionUtil.getDeclaredMethod(mapper.getClass(),
                                                    "setContextualNamespaceDecls", new Class<?>[]{String[].class});
        assertNotNull(m);
    }

}
 
Example #9
Source File: JAXBDataBinding.java    From cxf with Apache License 2.0 6 votes vote down vote up
private static Field getElField(String partName, final Class<?> wrapperType) {
    String fieldName = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.VARIABLE);
    Field[] fields = ReflectionUtil.getDeclaredFields(wrapperType);
    for (Field field : fields) {
        XmlElement el = field.getAnnotation(XmlElement.class);
        if (el != null
            && partName.equals(el.name())) {
            return field;
        }

        XmlElementRef xmlElementRefAnnotation = field.getAnnotation(XmlElementRef.class);
        if (xmlElementRefAnnotation != null && partName.equals(xmlElementRefAnnotation.name())) {
            return field;
        }

        if (field.getName().equals(fieldName)) {
            return field;
        }
    }
    return null;
}
 
Example #10
Source File: ResourceInjector.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static boolean processable(Class<?> cls, Object o) {
    if (cls.getName().startsWith("java.")
        || cls.getName().startsWith("javax.")) {
        return false;
    }
    NoJSR250Annotations njsr = cls.getAnnotation(NoJSR250Annotations.class);
    if (njsr != null) {
        for (String s : njsr.unlessNull()) {
            try {
                Field f = getField(cls, s);
                ReflectionUtil.setAccessible(f);
                if (f.get(o) == null) {
                    return true;
                }
            } catch (Exception ex) {
                return true;
            }
        }
        return false;
    }
    return true;
}
 
Example #11
Source File: CxfCdiAutoSetup.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
private void dump(final LogFacade log, final ServerProviderFactory spf, final String description, final String fieldName) {
    final Field field = ReflectionUtil.getDeclaredField(ProviderFactory.class, fieldName);
    if (!field.isAccessible()) {
        field.setAccessible(true);
    }
    try {
        final Collection<ProviderInfo<?>> providers = Collection.class.cast(field.get(spf));
        log.info("     " + description);
        providers.stream().map(ProviderInfo::getProvider).forEach(o -> {
            try {
                log.info("       - " + o);
            } catch (final RuntimeException re) {
                // no-op: maybe cdi context is not active
            }
        });
    } catch (IllegalAccessException e) {
        // ignore, not that a big deal
    }
}
 
Example #12
Source File: JAXBUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
public static Class<?> getValidClass(Class<?> cls) {
    if (cls.isEnum() || cls.isArray()) {
        return cls;
    }

    if (cls == Object.class || cls == String.class || cls.isPrimitive() || cls.isAnnotation()
        || "javax.xml.ws.Holder".equals(cls.getName())) {
        return null;
    } else if (cls.isInterface()
        || "javax.xml.ws.wsaddressing.W3CEndpointReference".equals(cls.getName())) {
        return cls;
    }

    Constructor<?> cons = ReflectionUtil.getDeclaredConstructor(cls);
    if (cons == null) {
        cons = ReflectionUtil.getConstructor(cls);
        if (cons == null) {
            return null;
        }
    }
    return cls;
}
 
Example #13
Source File: ResourceInjector.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static Field getField(Class<?> cls, String name) {
    if (cls == null) {
        return null;
    }
    try {
        Field f = ReflectionUtil.getDeclaredField(cls, name);
        if (f == null) {
            f = getField(cls.getSuperclass(), name);
        }
        return f;
    } catch (Exception ex) {
        return getField(cls.getSuperclass(), name);
    }
}
 
Example #14
Source File: SecurityTokenServiceProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void walkDom(String pfx, Element element, Binder<Node> binder, Object parent) {
    try {
        Object o = binder.getJAXBNode(element);
        if (o instanceof JAXBElement) {
            o = ((JAXBElement<?>)o).getValue();
        }
        //System.out.println(pfx + DOMUtils.getElementQName(element) + " ->  "
        //    + (o == null ? "null" : o.getClass()));
        if (o == null && parent != null) {
            // if it's not able to bind to an object, it's possibly an xsd:any
            // we'll check the parent for the standard "any" and replace with
            // the original element.
            Field f = parent.getClass().getDeclaredField("any");
            if (f.getAnnotation(XmlAnyElement.class) != null) {
                Object old = ReflectionUtil.setAccessible(f).get(parent);
                if (old instanceof Element
                    && DOMUtils.getElementQName(element).equals(DOMUtils.getElementQName((Element)old))) {
                    ReflectionUtil.setAccessible(f).set(parent, element);
                }
            }
        }
        if (o == null) {
            return;
        }
        Node nd = element.getFirstChild();
        while (nd != null) {
            if (nd instanceof Element) {
                walkDom(pfx + "  ", (Element)nd, binder, o);
            }
            nd = nd.getNextSibling();
        }
    } catch (Throwable t) {
        //ignore -this is a complete hack anyway
    }
}
 
Example #15
Source File: ResourceInjector.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Collection<Method> getAnnotatedMethods(Class<? extends Annotation> acls) {

        Collection<Method> methods = new LinkedList<>();
        addAnnotatedMethods(acls, getTarget().getClass().getMethods(), methods);
        addAnnotatedMethods(acls, ReflectionUtil.getDeclaredMethods(getTarget().getClass()), methods);
        if (getTargetClass() != getTarget().getClass()) {
            addAnnotatedMethods(acls, getTargetClass().getMethods(), methods);
            addAnnotatedMethods(acls, ReflectionUtil.getDeclaredMethods(getTargetClass()), methods);
        }
        return methods;
    }
 
Example #16
Source File: DefaultSecurityContext.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected boolean checkGroup(Principal principal, String role) {
    if (principal.getName().equals(role)) {
        return true;
    }

    Enumeration<? extends Principal> members;
    try {
        Method m = ReflectionUtil.getMethod(principal.getClass(), "members");
        m.setAccessible(true);
        @SuppressWarnings("unchecked")
        Enumeration<? extends Principal> ms = (Enumeration<? extends Principal>)m.invoke(principal);
        members = ms;
    } catch (Exception e) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("Unable to invoke memebers in " + principal.getName() + ":" + e.getMessage());
        }
        return false;
    }
    
    while (members.hasMoreElements()) {
        // this might be a plain role but could represent a group consisting of other groups/roles
        Principal member = members.nextElement();
        if (member.getName().equals(role)
            || isGroupPrincipal(member) 
            && checkGroup((GroupPrincipal)member, role)) {
            return true;
        }
    }
    return false;
}
 
Example #17
Source File: DOMUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Try to get the DOM DocumentFragment from the SAAJ DocumentFragment with JAVA9 afterwards
 * @param fragment The original documentFragment we need to check
 * @return The DOM DocumentFragment
 */
public static DocumentFragment getDomDocumentFragment(DocumentFragment fragment) {
    if (fragment != null && isJava9SAAJ()) {
        //java9 plus hack
        Field f = GET_DOCUMENT_FRAGMENT_FIELDS.get(fragment.getClass());
        if (f != null) {
            return ReflectionUtil.accessDeclaredField(f, fragment, DocumentFragment.class);
        }
    }
    return fragment;
}
 
Example #18
Source File: DOMUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Try to get the DOM Node from the SAAJ Node with JAVA9 afterwards
 * @param node The original node we need check
 * @return The DOM node
 */
public static Node getDomElement(Node node) {
    if (node != null && isJava9SAAJ()) {
        //java9plus hack
        Method method = GET_DOM_ELEMENTS_METHODS.get(node.getClass());
        if (method != null) {
            try {
                return (Node)ReflectionUtil.setAccessible(method).invoke(node);
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return node;
}
 
Example #19
Source File: DOMUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected Method computeValue(Class<?> type) {
    try {
        return ReflectionUtil.getMethod(type, "getDomElement");
    } catch (NoSuchMethodException e) {
        //best effort to try, do nothing if NoSuchMethodException
        return null;
    }
}
 
Example #20
Source File: AnnotationProcessor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void processMethods(AnnotationVisitor visitor, Class<? extends Object> targetClass) {

        if (targetClass.getSuperclass() != null) {
            processMethods(visitor, targetClass.getSuperclass());
        }
        for (Method element : ReflectionUtil.getDeclaredMethods(targetClass)) {
            for (Class<? extends Annotation> clz : annotationTypes) {
                Annotation ann = element.getAnnotation(clz);
                if (ann != null) {
                    visitor.visitMethod(element, ann);
                }
            }
        }
    }
 
Example #21
Source File: AnnotationProcessor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void processFields(AnnotationVisitor visitor, Class<? extends Object> targetClass) {
    if (targetClass.getSuperclass() != null) {
        processFields(visitor, targetClass.getSuperclass());
    }
    for (Field element : ReflectionUtil.getDeclaredFields(targetClass)) {
        for (Class<? extends Annotation> clz : annotationTypes) {
            Annotation ann = element.getAnnotation(clz);
            if (ann != null) {
                visitor.visitField(element, ann);
            }
        }
    }
}
 
Example #22
Source File: JAXBUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static Object createEscapeHandler(Class<?> cls, String simpleClassName) {
    try {
        String postFix = getPostfix(cls);
        if (postFix == null) {
            LOG.log(Level.WARNING, "Failed to create" + simpleClassName + " for unknown jaxb class:"
                + cls);
            return null;
        }
        Class<?> handlerClass = ClassLoaderUtils.loadClass("com.sun.xml" + postFix
                                                               + ".bind.marshaller." + simpleClassName,
                                                           cls);
        Class<?> handlerInterface = ClassLoaderUtils
            .loadClass("com.sun.xml" + postFix + ".bind.marshaller.CharacterEscapeHandler",
                       cls);
        Object targetHandler = ReflectionUtil.getDeclaredField(handlerClass, "theInstance").get(null);
        return ProxyHelper.getProxy(cls.getClassLoader(),
                                    new Class[] {handlerInterface},
                                    new EscapeHandlerInvocationHandler(targetHandler));
    } catch (Exception e) {
        if ("NoEscapeHandler".equals(simpleClassName)) {
            //this class doesn't exist in JAXB 2.2 so expected
            LOG.log(Level.FINER, "Failed to create " + simpleClassName);
        } else {
            LOG.log(Level.INFO, "Failed to create " + simpleClassName);
        }
    }
    return null;
}
 
Example #23
Source File: PhaseInterceptorChainTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
AbstractPhaseInterceptor<Message> setUpPhaseInterceptor(final String phase,
                                               final String id,
                                               Set<String> b,
                                               Set<String> a) throws Exception {

    AbstractPhaseInterceptor<Message> p = control
        .createMock(AbstractPhaseInterceptor.class);

    if (a == null) {
        a = new SortedArraySet<>();
    }
    if (b == null) {
        b = new SortedArraySet<>();
    }
    Field f = AbstractPhaseInterceptor.class.getDeclaredField("before");
    ReflectionUtil.setAccessible(f);
    f.set(p, b);

    f = AbstractPhaseInterceptor.class.getDeclaredField("after");
    ReflectionUtil.setAccessible(f);
    f.set(p, a);

    f = AbstractPhaseInterceptor.class.getDeclaredField("phase");
    ReflectionUtil.setAccessible(f);
    f.set(p, phase);

    f = AbstractPhaseInterceptor.class.getDeclaredField("id");
    ReflectionUtil.setAccessible(f);
    f.set(p, id);

    return p;
}
 
Example #24
Source File: CrossOriginResourceSharingFilter.java    From cxf with Apache License 2.0 5 votes vote down vote up
private <T extends Annotation> T  getAnnotation(Method m,
                                                Class<T> annClass) {
    if (m == null) {
        return null;
    }
    return ReflectionUtil.getAnnotationForMethodOrContainingClass(m,  annClass);
}
 
Example #25
Source File: SecurityTokenServiceProvider.java    From steady with Apache License 2.0 5 votes vote down vote up
private void walkDom(String pfx, Element element, Binder<Node> binder, Object parent) {
    try {
        Object o = binder.getJAXBNode(element);
        if (o instanceof JAXBElement) {
            o = ((JAXBElement<?>)o).getValue();
        }
        //System.out.println(pfx + DOMUtils.getElementQName(element) + " ->  " 
        //    + (o == null ? "null" : o.getClass()));
        if (o == null && parent != null) {
            // if it's not able to bind to an object, it's possibly an xsd:any
            // we'll check the parent for the standard "any" and replace with 
            // the original element.
            Field f = parent.getClass().getDeclaredField("any");
            if (f.getAnnotation(XmlAnyElement.class) != null) {
                Object old = ReflectionUtil.setAccessible(f).get(parent);
                if (old instanceof Element
                    && DOMUtils.getElementQName(element).equals(DOMUtils.getElementQName((Element)old))) {
                    ReflectionUtil.setAccessible(f).set(parent, element);
                }
            }
        }
        if (o == null) {
            return;
        }
        Node nd = element.getFirstChild();
        while (nd != null) {
            if (nd instanceof Element) {
                walkDom(pfx + "  ", (Element)nd, binder, o);
            }
            nd = nd.getNextSibling();
        }
    } catch (Throwable t) {
        //ignore -this is a complete hack anyway
    }
}
 
Example #26
Source File: SecurityTokenServiceProvider.java    From steady with Apache License 2.0 5 votes vote down vote up
private void walkDom(String pfx, Element element, Binder<Node> binder, Object parent) {
    try {
        Object o = binder.getJAXBNode(element);
        if (o instanceof JAXBElement) {
            o = ((JAXBElement<?>)o).getValue();
        }
        //System.out.println(pfx + DOMUtils.getElementQName(element) + " ->  " 
        //    + (o == null ? "null" : o.getClass()));
        if (o == null && parent != null) {
            // if it's not able to bind to an object, it's possibly an xsd:any
            // we'll check the parent for the standard "any" and replace with 
            // the original element.
            Field f = parent.getClass().getDeclaredField("any");
            if (f.getAnnotation(XmlAnyElement.class) != null) {
                Object old = ReflectionUtil.setAccessible(f).get(parent);
                if (old instanceof Element
                    && DOMUtils.getElementQName(element).equals(DOMUtils.getElementQName((Element)old))) {
                    ReflectionUtil.setAccessible(f).set(parent, element);
                }
            }
        }
        if (o == null) {
            return;
        }
        Node nd = element.getFirstChild();
        while (nd != null) {
            if (nd instanceof Element) {
                walkDom(pfx + "  ", (Element)nd, binder, o);
            }
            nd = nd.getNextSibling();
        }
    } catch (Throwable t) {
        //ignore -this is a complete hack anyway
    }
}
 
Example #27
Source File: SecurityTokenServiceProvider.java    From steady with Apache License 2.0 5 votes vote down vote up
private void walkDom(String pfx, Element element, Binder<Node> binder, Object parent) {
    try {
        Object o = binder.getJAXBNode(element);
        if (o instanceof JAXBElement) {
            o = ((JAXBElement<?>)o).getValue();
        }
        //System.out.println(pfx + DOMUtils.getElementQName(element) + " ->  " 
        //    + (o == null ? "null" : o.getClass()));
        if (o == null && parent != null) {
            // if it's not able to bind to an object, it's possibly an xsd:any
            // we'll check the parent for the standard "any" and replace with 
            // the original element.
            Field f = parent.getClass().getDeclaredField("any");
            if (f.getAnnotation(XmlAnyElement.class) != null) {
                Object old = ReflectionUtil.setAccessible(f).get(parent);
                if (old instanceof Element
                    && DOMUtils.getElementQName(element).equals(DOMUtils.getElementQName((Element)old))) {
                    ReflectionUtil.setAccessible(f).set(parent, element);
                }
            }
        }
        if (o == null) {
            return;
        }
        Node nd = element.getFirstChild();
        while (nd != null) {
            if (nd instanceof Element) {
                walkDom(pfx + "  ", (Element)nd, binder, o);
            }
            nd = nd.getNextSibling();
        }
    } catch (Throwable t) {
        //ignore -this is a complete hack anyway
    }
}
 
Example #28
Source File: JAXBDataBinding.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void hackInNewInternalizationLogic(SchemaCompiler schemaCompiler,
                                           final OASISCatalogManager catalog,
                                           Options opts) {
    try {
        Field f = schemaCompiler.getClass().getDeclaredField("forest");
        ReflectionUtil.setAccessible(f);
        XMLSchemaInternalizationLogic logic = new XMLSchemaInternalizationLogic() {
            public XMLFilterImpl createExternalReferenceFinder(DOMForest parent) {
                return new ReferenceFinder(parent, catalog);
            }
        };

        Constructor<DOMForest> c = null;
        DOMForest forest = null;

        try {
            c = DOMForest.class.getConstructor(InternalizationLogic.class, Options.class);
            forest = c.newInstance(logic, opts);
        } catch (Throwable t) {
            c = DOMForest.class.getConstructor(InternalizationLogic.class);
            forest = c.newInstance(logic);
        }
        forest.setErrorHandler((ErrorReceiver)schemaCompiler);
        f.set(schemaCompiler, forest);
    } catch (Throwable ex)  {
        //ignore
    }
}
 
Example #29
Source File: TypesCodeWriter.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void setEncoding(String s) {
    if (s != null) {
        try {
            //requires XJC 2.2.5 or newer
            Field f = CodeWriter.class.getDeclaredField("encoding");
            ReflectionUtil.setAccessible(f).set(this, s);
        } catch (Throwable t) {
            //ignore - should be caught in JAXBDataBinding.checkEncoding already
        }
    }
}
 
Example #30
Source File: JAXBDataBindingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
void doNamespaceMappingTest(boolean internal, boolean asm) throws Exception {
    if (internal) {
        try {
            Class.forName("com.sun.xml.internal.bind.v2.ContextFactory");
        } catch (Throwable t) {
            //on a JVM (likely IBM's) that doesn't rename the ContextFactory package to include "internal"
            return;
        }
    }
    try {
        if (!asm) {
            ReflectionUtil.setAccessible(ReflectionUtil.getDeclaredField(ASMHelper.class, "badASM"))
                .set(null, Boolean.TRUE);
        }

        JAXBDataBinding db = createJaxbContext(internal);

        DataWriter<XMLStreamWriter> writer = db.createWriter(XMLStreamWriter.class);
        XMLOutputFactory writerFactory = XMLOutputFactory.newInstance();
        StringWriter stringWriter = new StringWriter();
        XMLStreamWriter xmlWriter = writerFactory.createXMLStreamWriter(stringWriter);
        QualifiedBean bean = new QualifiedBean();
        bean.setAriadne("spider");
        writer.write(bean, xmlWriter);
        xmlWriter.flush();
        String xml = stringWriter.toString();
        assertTrue("Failed to map namespace " + xml, xml.contains("greenland=\"uri:ultima:thule"));
    } finally {
        if (!asm) {
            ReflectionUtil.setAccessible(ReflectionUtil.getDeclaredField(ASMHelper.class, "badASM"))
                .set(null, Boolean.FALSE);
        }
    }
}