alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (PrimitiveTypeAdapter.java)

This example Java source code file (PrimitiveTypeAdapter.java) is included in the alvinalexander.com "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

can, class, constructor, illegalaccessexception, instantiationexception, invocationtargetexception, jsonparseexception, nosuchmethodexception, primitivetypeadapter, reflection, runtimeexception, string, suppresswarnings, the

The PrimitiveTypeAdapter.java Java example source code

/*
 * Copyright (C) 2008 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
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.gson;

import com.google.gson.internal.Primitives;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Handles type conversion from some object to some primitive (or primitive
 * wrapper instance).
 *
 * @author Joel Leitch
 */
final class PrimitiveTypeAdapter {

  @SuppressWarnings("unchecked")
  public <T> T adaptType(Object from, Class to) {
    Class<?> aClass = Primitives.wrap(to);
    if (Primitives.isWrapperType(aClass)) {
      if (aClass == Character.class) {
        String value = from.toString();
        if (value.length() == 1) {
          return (T) (Character) from.toString().charAt(0);
        }
        throw new JsonParseException("The value: " + value + " contains more than a character.");
      }

      try {
        Constructor<?> constructor = aClass.getConstructor(String.class);
        return (T) constructor.newInstance(from.toString());
      } catch (NoSuchMethodException e) {
        throw new JsonParseException(e);
      } catch (IllegalAccessException e) {
        throw new JsonParseException(e);
      } catch (InvocationTargetException e) {
        throw new JsonParseException(e);
      } catch (InstantiationException e) {
        throw new JsonParseException(e);
      }
    } else if (Enum.class.isAssignableFrom(to)) {
      // Case where the type being adapted to is an Enum
      // We will try to convert from.toString() to the enum
      try {
        Method valuesMethod = to.getMethod("valueOf", String.class);
        return (T) valuesMethod.invoke(null, from.toString());
      } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
      } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
      } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
      }
    } else {
      throw new JsonParseException("Can not adapt type " + from.getClass() + " to " + to);
    }
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java PrimitiveTypeAdapter.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, alvinalexander.com
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.