| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (

This example Java source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

arraylist, bar, blue, boolean, exception, foo, gson, hashmap, integer, override, red, reflection, string, testcase, typevariabletest, util

The Java example source code

 * Copyright (C) 2010 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.


import java.lang.reflect.Type;
import java.util.Arrays;
import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

 * Functional test for Gson serialization and deserialization of
 * classes with type variables.
 * @author Joel Leitch
public class TypeVariableTest extends TestCase {

  public void testAdvancedTypeVariables() throws Exception {
    Gson gson = new Gson();
    Bar bar1 = new Bar("someString", 1, true);
    ArrayList<Integer> arrayList = new ArrayList();
    arrayList.add(3);"key1", arrayList);"key2", new ArrayList<Integer>());
    String json = gson.toJson(bar1);

    Bar bar2 = gson.fromJson(json, Bar.class);
    assertEquals(bar1, bar2);

  public void testTypeVariablesViaTypeParameter() throws Exception {
    Gson gson = new Gson();
    Foo<String, Integer> original = new Foo("e", 5, false);"f", Arrays.asList(6, 7));
    Type type = new TypeToken<Foo() {}.getType();
    String json = gson.toJson(original, type);
    assertEquals(original, gson.<FoofromJson(json, type));

  public void testBasicTypeVariables() throws Exception {
    Gson gson = new Gson();
    Blue blue1 = new Blue(true);
    String json = gson.toJson(blue1);

    Blue blue2 = gson.fromJson(json, Blue.class);
    assertEquals(blue1, blue2);

  public static class Blue extends Red<Boolean> {
    public Blue() {

    public Blue(boolean value) {

    // Technically, we should implement hashcode too
    public boolean equals(Object o) {
      if (!(o instanceof Blue)) {
        return false;
      Blue blue = (Blue) o;
      return redField.equals(blue.redField);

  public static class Red<S> {
    protected S redField;

    public Red() {}

    public Red(S redField) {
      this.redField = redField;

  public static class Foo<S, T> extends Red {
    private S someSField;
    private T someTField;
    public final Map<S, List map = new HashMap>();

    public Foo() {}

    public Foo(S sValue, T tValue, Boolean redField) {
      this.someSField = sValue;
      this.someTField = tValue;

    // Technically, we should implement hashcode too
    public boolean equals(Object o) {
      if (!(o instanceof Foo<?, ?>)) {
        return false;
      Foo<S, T> realFoo = (Foo) o;
      return redField.equals(realFoo.redField)
        && someTField.equals(realFoo.someTField)
        && someSField.equals(realFoo.someSField)
        && map.equals(;

  public static class Bar extends Foo<String, Integer> {
    public Bar() {
      this("", 0, false);

    public Bar(String s, Integer i, boolean b) {
      super(s, i, b);

Other Java examples (source code examples)

Here is a short list of links related to this Java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.