Example usage for com.google.gson FieldAttributes getDeclaredClass

List of usage examples for com.google.gson FieldAttributes getDeclaredClass

Introduction

In this page you can find the example usage for com.google.gson FieldAttributes getDeclaredClass.

Prototype

public Class<?> getDeclaredClass() 

Source Link

Document

Returns the Class object that was declared for this field.

Usage

From source file:com.jd.survey.web.settings.NowwebmanagerController.java

License:Open Source License

@Override
public boolean shouldSkipField(FieldAttributes fieldattributes) {
    Class<?> owner = fieldattributes.getDeclaringClass();
    Class<?> c = fieldattributes.getDeclaredClass();
    String f = fieldattributes.getName();
    boolean isSkip = false;

    if (owner == target) {
        if (ArrayUtils.contains(fields, f)) {
            log.debug("fitler field:{} for class:{}", f, owner);
            isSkip = true;//  ww  w.  ja  va 2  s.c  om
        }
        if (ArrayUtils.contains(clazz, c)) {
            log.debug("fitler class:{} for class:{}", c, owner);
            isSkip = true;
        }
        if (reverse) {
            isSkip = !isSkip;
        }
    }

    return isSkip;
}

From source file:com.xpbytes.gson.hal.HalReflection.java

License:Apache License

/**
 * Get the type of the field as an item. Will walk collections to find the inner type.
 *
 * @see #getFieldType(Field)/*from   w ww  . ja  v a  2 s.  c  om*/
 * @see #getFieldType(FieldAttributes)
 * @see #getFieldItemizedType(Field)
 *
 * @param attributes the field attributes
 * @return the type
 */
static Class<?> getFieldItemizedType(FieldAttributes attributes) {
    if (Collection.class.isAssignableFrom(attributes.getDeclaredClass())) {
        ParameterizedType gType = (ParameterizedType) attributes.getDeclaredType();

        Type[] actualTypeArguments = gType.getActualTypeArguments();
        if (actualTypeArguments != null && actualTypeArguments.length > 0)
            return (Class) actualTypeArguments[0];

        return getFieldType(attributes);
    }
    return getFieldType(attributes);
}

From source file:com.xpbytes.gson.hal.HalReflection.java

License:Apache License

/**
 * Gets the type of a field./* ww  w .j av  a  2s.  c  o m*/
 *
 * @see #getFieldType(Field)
 * @see #getFieldItemizedType(Field)
 * @see #getFieldItemizedType(FieldAttributes)
 *
 * @param attributes the field attributes
 * @return the type
 */
static Class<?> getFieldType(FieldAttributes attributes) {
    return attributes.getDeclaredClass();
}

From source file:es.sm2.openppm.utils.json.Exclusion.java

License:Open Source License

public boolean shouldSkipField(FieldAttributes f) {

    boolean exclusion = false;

    if (nameExlusion.isEmpty()) {
        exclusion = typeToSkip.equals(f.getDeclaredClass());
    } else {//w  ww . jav  a  2  s  .  co m
        exclusion = (typeToSkip.equals(f.getDeclaredClass()) && nameExlusion.contains(f.getName()));
    }
    return exclusion;
}

From source file:org.eclipse.mylyn.internal.gerrit.core.client.JSonSupport.java

License:Open Source License

public JSonSupport() {
    TypeToken<Map<Id, PatchSetApproval>> approvalMapType = new TypeToken<Map<ApprovalCategory.Id, PatchSetApproval>>() {
    };/*from   ww w .j  a v a2 s .  co  m*/
    ExclusionStrategy exclustionStrategy = new ExclusionStrategy() {

        public boolean shouldSkipField(FieldAttributes f) {
            // commentLinks requires instantiation of com.google.gwtexpui.safehtml.client.RegexFindReplace which is not on classpath
            if (f.getDeclaredClass() == List.class && f.getName().equals("commentLinks") //$NON-NLS-1$
                    && f.getDeclaringClass() == GerritConfig.class) {
                return true;
            }
            if (f.getDeclaredClass() == Map.class && f.getName().equals("given")) { //$NON-NLS-1$
                //return true;
            }
            // GSon 2.1 fails to deserialize the SubmitType enum
            if (f.getDeclaringClass() == Project.class && f.getName().equals("submitType")) { //$NON-NLS-1$
                return true;
            }
            return false;
        }

        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    };
    gson = JsonServlet.defaultGsonBuilder()
            .registerTypeAdapter(JSonResponse.class, new JSonResponseDeserializer())
            .registerTypeAdapter(Edit.class, new JsonDeserializer<Edit>() {
                public Edit deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    if (json.isJsonArray()) {
                        JsonArray array = json.getAsJsonArray();
                        if (array.size() == 4) {
                            return new Edit(array.get(0).getAsInt(), array.get(1).getAsInt(),
                                    array.get(2).getAsInt(), array.get(3).getAsInt());
                        }
                    }
                    return new Edit(0, 0);
                }
            })
            // ignore GerritForge specific AuthType "TEAMFORGE" which is unknown to Gerrit
            .registerTypeAdapter(AuthType.class, new JsonDeserializer<AuthType>() {

                @Override
                public AuthType deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    String jsonString = json.getAsString();
                    if (jsonString != null) {
                        try {
                            return AuthType.valueOf(jsonString);
                        } catch (IllegalArgumentException e) {
                            // ignore the error since the connector does not make use of AuthType
                            //GerritCorePlugin.logWarning("Ignoring unkown authentication type: " + jsonString, e);
                        }
                    }
                    return null;
                }
            })
            .registerTypeAdapter(approvalMapType.getType(), new JsonDeserializer<Map<Id, PatchSetApproval>>() {

                @Override
                public Map<Id, PatchSetApproval> deserialize(JsonElement json, Type typeOfT,
                        JsonDeserializationContext context) throws JsonParseException {
                    // Gerrit 2.2: the type of PatchSetPublishDetail.given changed from a map to a list
                    Map<Id, PatchSetApproval> map = new HashMap<ApprovalCategory.Id, PatchSetApproval>();
                    if (json.isJsonArray()) {
                        JsonArray array = json.getAsJsonArray();
                        for (Iterator<JsonElement> it = array.iterator(); it.hasNext();) {
                            JsonElement element = it.next();
                            Id key = context.deserialize(element, Id.class);
                            if (key.get() != null) {
                                // Gerrit < 2.1.x: json is map
                                element = it.next();
                            }
                            PatchSetApproval value = context.deserialize(element, PatchSetApproval.class);
                            if (key.get() == null) {
                                // Gerrit 2.2: json is a list, deduct key from value
                                key = value.getCategoryId();
                            }
                            map.put(key, value);
                        }
                    }
                    return map;
                }
            }).setExclusionStrategies(exclustionStrategy).create();
}

From source file:se.liu.imt.mi.eee.validation.json.AOMtoJSONandYAMLSerializer.java

License:LGPL

/**
 * Create an outputter with default encoding, indent and lineSeparator
 * TODO: fix node_i_d --> node_id/*from   w w  w.j  av a  2 s  .co m*/
 */
public AOMtoJSONandYAMLSerializer() {
    this.encoding = UTF8;
    this.indent = "    "; // 4 white space characters
    this.lineSeparator = "\r\n";

    gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .setExclusionStrategies(new ExclusionStrategy() {
                public boolean shouldSkipClass(Class<?> clazz) {
                    if (clazz == Archetype.class)
                        return true; // Avoid circular reference
                    return false;
                }

                /* Custom field exclusion goes here */
                public boolean shouldSkipField(FieldAttributes f) {
                    //System.out.println("---> "+f.getName());
                    if (f.getName().equalsIgnoreCase("termDefinitionMap"))
                        return true;
                    if (f.getName().equalsIgnoreCase("constraintDefinitionMap"))
                        return true;
                    if (f.getName().equalsIgnoreCase("hiddenOnForm"))
                        return true;
                    if (f.getName().equalsIgnoreCase("existence"))
                        return true;

                    if (f.getName().equalsIgnoreCase("existence")) {
                        System.out.println(" ==> " + f.getDeclaredClass().getCanonicalName());
                    }
                    //                  System.out.println(f.getDeclaredClass());
                    //                  return (f.getName().equalsIgnoreCase("code") && f.getDeclaredClass().equals(ArchetypeTerm.class));
                    //return (f.getName().equalsIgnoreCase("code")); //TODO: check that no other fields named "code" are there
                    return false;
                }
            }).registerTypeAdapter(CodePhrase.class, new CodePhraseConverter())
            .registerTypeAdapter(DvDateTime.class, new DvDateTimeConverter())
            .registerTypeAdapter(Interval.class, new IntervalConverterSequenceFormat())
            //.setDateFormat("yyyy-MM-dd'T'HH:mm:ss") // TODO: Check what to do with timezones (probably yoda stuff if DvDateTimeConverter does not suffice)
            .create();

    DumperOptions dumperOptions = new DumperOptions();
    //dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK);
    //dumperOptions.setLineBreak(LineBreak.WIN);
    dumperOptions.setWidth(120);
    //dumperOptions.setDefaultFlowStyle(FlowStyle.FLOW);
    //dumperOptions.setPrettyFlow(true);
    //dumperOptions.setDefaultScalarStyle(ScalarStyle.DOUBLE_QUOTED);           
    yaml = new Yaml(dumperOptions); // Yaml(new SyntaxSugarRepresenter());
    //yaml.setBeanAccess(BeanAccess.PROPERTY);

}

From source file:uk.ac.stfc.isis.ibex.epics.conversion.json.SpecificClassExclusionStrategy.java

License:Open Source License

/**
 * {@inheritDoc}/*from ww  w.  jav a  2s . com*/
 */
@Override
public boolean shouldSkipField(FieldAttributes f) {
    return excludedThisClass.equals(f.getDeclaredClass());
}