A wrapper around reflection to resolve generics. : Generic « Reflection « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Reflection » GenericScreenshots 
A wrapper around reflection to resolve generics.
 
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * A wrapper around reflection to resolve generics.
 
 @author Simone Gianni <simoneg@apache.org>
 */
// TODO there is vast space for caching and optimizations here!
public class GenericClass {

  private Class<?> myclass = null;

  private Map<String, Class<?>> genericMap = new HashMap<String, Class<?>>();

  private Map<String, String> reverseIntermediate = new HashMap<String, String>();

  public static GenericClass forClass(Class<?> concrete) {
    return new GenericClass(concrete);
  }

  public static GenericClass forField(Field field) {
    return forGenericType(field.getGenericType());
  }

  public static GenericClass forReturnType(Method method) {
    return forGenericType(method.getGenericReturnType());
  }

  public static GenericClass forParameter(Method method, int index) {
    return forGenericType(method.getGenericParameterTypes()[index]);
  }

  public static GenericClass forGenericType(Type type) {
    if (type instanceof Class) {
      return forClass((Class<?>type);
    else if (type instanceof ParameterizedType) {
      return new GenericClass((ParameterizedTypetype);
    else {
      return forClass(Object.class);
      // throw new MagmaException("Dont know how to build a GenericClass out of
      // {0}", type.getClass());
    }
  }

  private GenericClass(Class<?> concrete) {
    TypeVariable<?>[] parameters = concrete.getTypeParameters();
    // if (parameters.length > 0) throw new MagmaException("Cannot parse {0}, it
    // is a generic class, use a concrete class instead", concrete);
    myclass = concrete;
    recurse(concrete);
    coalesceMap();
  }

  private GenericClass(ParameterizedType type) {
    myclass = (Class<?>type.getRawType();
    recurse(myclass, myclass, type);
    coalesceMap();
  }

  private void coalesceMap() {
    int cnt = reverseIntermediate.size();
    while (reverseIntermediate.size() && cnt > 0) {
      for (Iterator<Map.Entry<String, String>> iterator = this.reverseIntermediate.entrySet()
          .iterator(); iterator.hasNext();) {
        Map.Entry<String, String> entry = iterator.next();
        String target = entry.getValue();
        String search = entry.getKey();
        Class<?> clazz = genericMap.get(search);
        if (clazz == null)
          continue;
        if (genericMap.containsKey(target))
          throw new RuntimeException(
              "Impossible situation, it's a bug : {0} generic is both {1} and bound to {2}"
                  + target + genericMap.get(target+ search);
        genericMap.put(target, clazz);
        iterator.remove();
      }
      cnt--;
    }
    if (reverseIntermediate.size() 0) {
      for (Iterator<Map.Entry<String, String>> iterator = this.reverseIntermediate.entrySet()
          .iterator(); iterator.hasNext();) {
        Map.Entry<String, String> entry = iterator.next();
        String target = entry.getValue();
        String search = entry.getKey();
        Class<?> clazz = genericMap.get(search);
        if (clazz == null)
          clazz = Object.class;
        if (genericMap.containsKey(target))
          throw new RuntimeException(
              "Impossible situation, it's a bug : {0} generic is both {1} and bound to {2}"+
              target+ genericMap.get(target)+ search);
        genericMap.put(target, clazz);
      }
    }
  }

  public Class<?> getBaseClass() {
    return this.myclass;
  }

  private void recurse(Class<?> clazz, Class<?> simplesup, ParameterizedType partype) {
    Type[] typeArguments = partype.getActualTypeArguments();
    TypeVariable<?>[] parameters = simplesup.getTypeParameters();

    for (int i = 0; i < typeArguments.length; i++) {
      if (typeArguments[iinstanceof Class) {
        genericMap.put(simplesup.getName() "--" + parameters[i].getName(),
            (ClasstypeArguments[i]);
      else if (typeArguments[iinstanceof TypeVariable) {
        reverseIntermediate.put(clazz.getName() "--"
            ((TypeVariable<?>typeArguments[i]).getName(), simplesup.getName() "--"
            + parameters[i].getName());
      }
    }

    recurse(simplesup);
  }

  private void recurse(Class<?> clazz) {
    Type supclass = clazz.getGenericSuperclass();
    Class simplesup = clazz.getSuperclass();
    if (supclass == null)
      return;
    if (supclass instanceof ParameterizedType) {
      recurse(clazz, simplesup, (ParameterizedTypesupclass);
    else {
      recurse(simplesup);
    }
  }

  /**
   * Return real, "generics dereferenced", parameter types for the given method.
   
   @param method
   *          The method to analyze
   @return The real classes, dereferencing generics
   */
  public GenericClass[] getParameterTypes(Method method) {
    Class<?> declaring = method.getDeclaringClass();
    String declname = declaring.getName();

    Type[] parameterTypes = method.getGenericParameterTypes();
    GenericClass[] ret = new GenericClass[parameterTypes.length];
    for (int i = 0; i < parameterTypes.length; i++) {
      Type type = parameterTypes[i];
      if (type instanceof Class) {
        ret[i= forClass((Class<?>type);
      else if (type instanceof TypeVariable) {
        String name = ((TypeVariable<?>type).getName();
        Class<?> sub = genericMap.get(declname + "--" + name);
        if (sub == null)
          sub = Object.class;
        ret[i= forClass(sub);
      else {
        ret[i= forGenericType(type);
      }
    }
    return ret;
  }

  public GenericClass resolveType(Type type) {
    if (type instanceof Class) {
      return forClass((Class<?>type);
    else if (type instanceof TypeVariable) {
      GenericDeclaration gd = ((TypeVariable<?>type).getGenericDeclaration();
      Class<?> acclass = null;
      if (gd instanceof Class) {
        acclass = (Class<?>gd;
      else if (gd instanceof Method) {
        acclass = ((Methodgd).getDeclaringClass();
      else if (gd instanceof Constructor) {
        acclass = ((Constructor<?>gd).getDeclaringClass();
      }
      String name = ((TypeVariable<?>type).getName();
      return forClass(genericMap.get(acclass.getName() "--" + name));
    else {
      return forGenericType(type);
    }
  }

  /**
   * Search for all occurrencies of a specific method.
   * <p>
   * The type parameters passed in may be Class or null. If they are null, that
   * means that we don't know which class they should be, if they are a class,
   * that means we are searching for that class, and the comparison is made on
   * dereferenced generics.
   * </p>
   * <p>
   * Specifying no parameter types explicitly means "a method without
   * parameters".
   * </p>
   
   @param name
   *          The name of the method
   @param parameterTypes
   *          The types of the parameters
   @return A list of {@link MethodDef}, ordered with methods from current
   *         class first, then method from superclass and so on.
   */
  public List<MethodDef> findMethods(String name, Class<?>... parameterTypes) {
    List<MethodDef> founds = new ArrayList<MethodDef>();
    Class<?> current = myclass;
    while (current != null) {
      Method[] methods = current.getDeclaredMethods();
      for (Method method : methods) {
        if (!method.isBridge() && !method.isSynthetic() && method.getName().equals(name)) {
          Type[] types = method.getGenericParameterTypes();
          if (types.length == parameterTypes.length) {
            GenericClass[] genericClasses = getParameterTypes(method);
            Class<?>[] classes = toRawClasses(genericClasses);
            boolean good = true;
            for (int i = 0; i < types.length; i++) {
              if (parameterTypes[i!= null) {
                if (!classes[i].equals(parameterTypes[i])) {
                  good = false;
                  break;
                }
              }
            }
            if (good) {
              MethodDef def = new MethodDef(method, genericClasses);
              if (!founds.contains(def))
                founds.add(def);
            }
          }
        }
      }
      current = current.getSuperclass();
    }
    return founds;
  }

  public static Class<?>[] toRawClasses(GenericClass[] genclasses) {
    Class<?>[] ret = new Class<?>[genclasses.length];
    for (int i = 0; i < genclasses.length; i++) {
      ret[i= genclasses[i].getBaseClass();
    }
    return ret;
  }

  /**
   * Search for all methods having that name, no matter which parameter they
   * take.
   
   @param name
   *          The name of the methods
   @return A list of {@link MethodDef}, ordered with methods from current
   *         class first, then method from superclass and so on.
   */
  public List<MethodDef> findAllMethods(String name) {
    List<MethodDef> founds = new ArrayList<MethodDef>();
    Class<?> current = myclass;
    while (current != null) {
      Method[] methods = current.getDeclaredMethods();
      for (Method method : methods) {
        if (!method.isBridge() && !method.isSynthetic() && method.getName().equals(name)) {
          MethodDef def = new MethodDef(method);
          if (!founds.contains(def))
            founds.add(def);
        }
      }
      current = current.getSuperclass();
    }
    return founds;
  }

  public List<MethodDef> getMethods() {
    List<MethodDef> founds = new ArrayList<MethodDef>();
    Class<?> current = myclass;
    while (current != null) {
      Method[] methods = current.getDeclaredMethods();
      for (Method method : methods) {
        if (!method.isBridge() && !method.isSynthetic()) {
          MethodDef def = new MethodDef(method);
          if (!founds.contains(def))
            founds.add(def);
        }
      }
      current = current.getSuperclass();
    }
    return founds;
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof GenericClass))
      return false;
    GenericClass oth = (GenericClassobj;
    return this.getBaseClass().equals(oth.getBaseClass());
  }

  public class MethodDef {
    private Method method = null;

    private GenericClass[] params = null;

    MethodDef(Method m) {
      this.method = m;
    }

    MethodDef(Method m, GenericClass[] params) {
      this.method = m;
      this.params = params;
    }

    public String getName() {
      return this.method.getName();
    }

    public Method getBaseMethod() {
      return this.method;
    }

    public GenericClass[] getParameterTypes() {
      if (this.params == null) {
        this.params = GenericClass.this.getParameterTypes(method);
      }
      return this.params;
    }

    public GenericClass getReturnType() {
      return resolveType(method.getGenericReturnType());
    }

    @Override
    public boolean equals(Object other) {
      if (!(other instanceof MethodDef))
        return false;
      MethodDef oth = (MethodDefother;
      return (method.getName().equals(oth.method.getName()))
          && (Arrays.equals(getParameterTypes(), oth.getParameterTypes()));
    }

    public Class<?> getDeclaringClass() {
      return this.method.getDeclaringClass();
    }
  }

}

   
  
Related examples in the same category
1. Get Generic Super class
2. Generic Class reflection
3. Get field type and generic type by field name
4. get Return Type and get Generic Return Type
5. get Exception Types and get Generic Exception Types
6. get Parameter Types and get Generic ParameterTypes
7. Generic method reflection
8. get Generic Parameter Types from Constructor
9. Class Declaration Spy
10. All information about a class
11. Get all implemented generic interfaces
12. If right-hand side type may be assigned to the left-hand side type following the Java generics rules.
13. Get all interface and object classes that are generalizations of the provided class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.