Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:33:35+00:00 2026-06-14T13:33:35+00:00

I wrote a parser for a file format called ASN.1 that uses Guice’s TypeLiteral.getFieldType(Field)

  • 0

I wrote a parser for a file format called ASN.1 that uses Guice’s TypeLiteral.getFieldType(Field) to convert generic fields into specific Java types so I can construct the correct type (similar to Jackson or GSON databinding). But since I already depend on Guava and it seems to have a new TypeLiteral replacement, I’d like to use TypeToken instead. According to the Guave TypeToken documentation:

TypeToken is similar to Guice’s TypeLiteral class, but with one important difference: it supports non-reified types such as T, List<T> or even List<? extends Number>; while TypeLiteral does not. TypeToken is also serializable and offers numerous additional utility methods.

Can you get the reified type of a field using TypeToken? In other words, how can I do the following in Guava?

import org.junit.Test;
import com.google.inject.TypeLiteral;
public class FieldTypeTest {
    public static class A<T> {
        T a;
    }
    public static class B {
        A<String> b;
    }

    @Test
    public void testTypeToken() throws SecurityException, NoSuchFieldException {
        TypeLiteral<?> reifiedA = TypeLiteral.get(B.class).getFieldType(B.class.getDeclaredField("b"));
        assertEquals(String.class, reifiedA.getFieldType(reifiedA.getRawType().getDeclaredField("a")).getRawType());
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:33:37+00:00Added an answer on June 14, 2026 at 1:33 pm

    From my head, not verified

    Type t = B.class.getDeclaredField("b").getGenericType();
    Class<?> p = TypeToken.of(t).resolve(
            /* T */  A.getTypeParameters()[0]).getRawType();
    // p should be String.class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an application (.net 2.0) that uses mif/mid data format to
I'm working with Quickbook's IIF file format and I need to write a parser
How do I write to a pre-specified binary file format in Java that I
I want to write a parser for Blender's .obj file. The file format seems
any recommendations for saving four strings into an own file format that is read
I have a project that has a non-standard file format something like: var foo
I'm trying to write a generic XML to Core Data parser using libxml2. Since
I am trying to write a parser in Python for a special text file
First some background. I'm parsing a simple file format, and wish to re-use the
I want to write a parser for NBT (Named Binary Tags) structures. The format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.