2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.misc.rosetta;
24 import java.lang.reflect.Method;
25 import java.util.HashMap;
27 import java.util.TreeMap;
29 import javax.xml.bind.annotation.XmlType;
32 * For specific XML class, quickly find a Setter Method which will load the object
34 * Object type of Setter must match String at this time.
40 public class JaxSet<T> {
41 private static Map<Class<?>,JaxSet<?>> jsets = new HashMap<>();
42 private Map<String,Setter<T>> members;
44 private JaxSet(Class<?> cls) {
45 members = new TreeMap<>();
46 XmlType xmltype = cls.getAnnotation(XmlType.class);
47 Class<?> paramType[] = new Class[] {String.class};
48 for(String str : xmltype.propOrder()) {
50 String setName = "set" + Character.toUpperCase(str.charAt(0)) + str.subSequence(1, str.length());
51 Method meth = cls.getMethod(setName,paramType );
53 members.put(str, new Setter<T>(meth) {
54 public void set(T o, Object t) throws ParseException {
56 this.meth.invoke(o, t);
57 } catch (Exception e) {
58 throw new ParseException(e);
63 } catch (Exception e) {
69 public static abstract class Setter<O> {
70 protected final Method meth;
71 public Setter(Method meth) {
74 public abstract void set(O o, Object obj) throws ParseException;
77 public static <X> JaxSet<X> get(Class<?> cls) {
79 @SuppressWarnings("unchecked")
80 JaxSet<X> js = (JaxSet<X>)jsets.get(cls);
82 jsets.put(cls, js = new JaxSet<>(cls));
88 public Setter<T> get(String key) {
89 return members.get(key);