Java Code Examples for java.lang.reflect.Type

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project alljoyn_java, under directory /src/org/alljoyn/bus/.

Source file: InterfaceDescription.java

  19 
vote

/** 
 * Get the DBus property signature.
 * @param method the method
 */
public static String getPropertySig(Method method) throws AnnotationBusException {
  Type type=null;
  if (method.getName().startsWith("get")) {
    type=method.getGenericReturnType();
  }
 else   if (method.getName().startsWith("set")) {
    type=method.getGenericParameterTypes()[0];
  }
  BusProperty busProperty=method.getAnnotation(BusProperty.class);
  if (busProperty != null && busProperty.signature().length() > 0) {
    return Signature.typeSig(type,busProperty.signature());
  }
  return Signature.typeSig(type,null);
}
 

Example 2

From project AmDroid, under directory /AmenLib/src/main/java/com.jaeckel/amenoid/api/.

Source file: AmenServiceImpl.java

  19 
vote

public List<User> following(Long id) throws IOException {
  List<User> result=new ArrayList<User>();
  log.debug("followers()");
  HashMap<String,String> params=createAuthenticatedParams();
  HttpUriRequest httpGet=RequestFactory.createGETRequest(serviceUrl + "/users/" + id+ "/following.json",params);
  HttpResponse response=httpclient.execute(httpGet);
  HttpEntity responseEntity=response.getEntity();
  final String responseString=EntityUtils.toString(responseEntity,"UTF-8");
  Type collectionType=new TypeToken<Collection<User>>(){
  }
.getType();
  result=gson.fromJson(responseString,collectionType);
  return result;
}
 

Example 3

From project android-joedayz, under directory /Proyectos/ConsumirWS/src/net/ivanvega/ConsumirWS/.

Source file: ConsumirWSActivity.java

  18 
vote

/** 
 * Metodo que recibe una cadena JSON y la convierte en una lista de objetos AndroidOS para despues cargarlos en la lista
 * @param strJson (String) Cadena JSON
 */
private void crearLista(String strJson){
  gson=new Gson();
  Type lstT=new TypeToken<ArrayList<AndroidOS>>(){
  }
.getType();
  ArrayList<AndroidOS> arrListAOS=new ArrayList<AndroidOS>();
  arrListAOS=gson.fromJson(strJson,lstT);
  lsvAndroidOS.setAdapter(new ArrayAdapter<AndroidOS>(getApplication(),android.R.layout.simple_list_item_1,arrListAOS));
}
 

Example 4

From project android_8, under directory /src/com/google/gson/internal/.

Source file: $Gson$Types.java

  18 
vote

/** 
 * Returns a two element array containing this map's key and value types in positions 0 and 1 respectively.
 */
public static Type[] getMapKeyAndValueTypes(Type context,Class<?> contextRawType){
  if (context == Properties.class) {
    return new Type[]{String.class,String.class};
  }
  Type mapType=getSupertype(context,contextRawType,Map.class);
  ParameterizedType mapParameterizedType=(ParameterizedType)mapType;
  return mapParameterizedType.getActualTypeArguments();
}
 

Example 5

From project ATHENA, under directory /helpers/tags-sub-collection/src/test/java/org/fracturedatlas/athena/plugin/tags/.

Source file: TagsContainerTest.java

  18 
vote

@Test public void testTagsSubCollectionUnknownType(){
  String jsonResponse=tix.path("unknowntypes/tags").type("application/json").get(String.class);
  Type listType=new TypeToken<List<String>>(){
  }
.getType();
  List<Tag> tags=gson.fromJson(jsonResponse,listType);
  assertNotNull(tags);
  assertEquals(0,tags.size());
}
 

Example 6

From project beintoo-android-sdk, under directory /BeintooSDK/src/com/beintoo/beintoosdk/.

Source file: BeintooAchievements.java

  18 
vote

/** 
 * Returns the list of available achievements for the current app
 * @return a list of AchievementWrap
 */
public List<AchievementWrap> getAppAchievements(){
  String apiUrl=apiPreUrl + "achievement";
  HeaderParams header=new HeaderParams();
  header.getKey().add("apikey");
  header.getValue().add(DeveloperConfiguration.apiKey);
  BeintooConnection conn=new BeintooConnection();
  String json=conn.httpRequest(apiUrl,header,null);
  Gson gson=new Gson();
  Type mapType=new TypeToken<List<AchievementWrap>>(){
  }
.getType();
  List<AchievementWrap> achievements=gson.fromJson(json,mapType);
  return achievements;
}
 

Example 7

From project blueprint-namespaces, under directory /blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/.

Source file: GenericType.java

  18 
vote

static Class<?> getConcreteClass(Type type){
  Type ntype=collapse(type);
  if (ntype instanceof Class)   return (Class<?>)ntype;
  if (ntype instanceof ParameterizedType)   return getConcreteClass(collapse(((ParameterizedType)ntype).getRawType()));
  throw new RuntimeException("Unknown type " + type);
}
 

Example 8

From project AirCastingAndroidClient, under directory /src/main/java/pl/llp/aircasting/helper/.

Source file: SettingsHelper.java

  17 
vote

public List<ExternalSensorDescriptor> knownSensors(){
  String json=getConnectedSensorsAsJSON();
  if (Strings.isNullOrEmpty(json)) {
    return newArrayList();
  }
  Type type=new TypeToken<List<ExternalSensorDescriptor>>(){
  }
.getType();
  try {
    return gson.fromJson(json,type);
  }
 catch (  JsonSyntaxException e) {
    writeString(SENSORS,"");
  }
  return newArrayList();
}
 

Example 9

From project airlift, under directory /event/src/main/java/io/airlift/event/client/.

Source file: EventTypeMetadata.java

  17 
vote

private Class<?> extractIterableType(Method method){
  Type[] types=getTypeParameters(Iterable.class,method.getGenericReturnType());
  if ((types == null) || (types.length != 1)) {
    addMethodError("Unable to get type parameter for iterable [%s]",method,method.getGenericReturnType());
    return null;
  }
  Type type=types[0];
  if (!(type instanceof Class)) {
    addMethodError("Iterable type parameter [%s] must be an exact type",method,type);
    return null;
  }
  if (isIterable((Class<?>)type)) {
    addMethodError("Iterable of iterable is not supported",method);
    return null;
  }
  return (Class<?>)type;
}
 

Example 10

From project Arecibo, under directory /util/src/main/java/com/ning/arecibo/util/lifecycle/.

Source file: LifecycledProvider.java

  17 
vote

/** 
 * This code is copied from Guice because they didn't have the foresite to make it public...
 * @param type look at the source of the Guice 1.0 TypeLiteral class
 * @return look at the source of the Guice 1.0 TypeLiteral class
 */
private static Class<?> getRawType(Type type){
  if (type instanceof Class<?>) {
    return (Class<?>)type;
  }
 else {
    if (type instanceof ParameterizedType) {
      ParameterizedType parameterizedType=(ParameterizedType)type;
      Type rawType=parameterizedType.getRawType();
      if (!(rawType instanceof Class<?>)) {
        throw new RuntimeException("Bad things");
      }
      return (Class<?>)rawType;
    }
    if (type instanceof GenericArrayType) {
      return Object[].class;
    }
    throw new RuntimeException("Bad things");
  }
}
 

Example 11

From project arquillian-core, under directory /core/impl-base/src/main/java/org/jboss/arquillian/core/impl/.

Source file: Reflections.java

  17 
vote

public static Class<?> getType(Type type){
  if (type instanceof Class<?>) {
    return (Class<?>)type;
  }
 else   if (type instanceof ParameterizedType) {
    return getType(((ParameterizedType)type).getActualTypeArguments()[0]);
  }
 else   if (type instanceof WildcardType) {
    for (    Type wildType : ((WildcardType)type).getUpperBounds()) {
      Type upperType=getType(wildType);
      if (upperType != null) {
        return getType(upperType);
      }
    }
    for (    Type wildType : ((WildcardType)type).getLowerBounds()) {
      Type lowerType=getType(wildType);
      if (lowerType != null) {
        return getType(lowerType);
      }
    }
  }
  return null;
}
 

Example 12

From project arquillian-graphene, under directory /graphene-webdriver/graphene-webdriver-impl/src/main/java/org/jboss/arquillian/graphene/enricher/.

Source file: PageFragmentsEnricher.java

  17 
vote

private void initializePageObjectFields(Object testCase,List<Field> fields){
  for (  Field i : fields) {
    try {
      Type type=i.getGenericType();
      Object page=null;
      if (type instanceof TypeVariable) {
        page=getActualType(i,testCase).newInstance();
      }
 else {
        Class<?> declaredClass=i.getType();
        page=declaredClass.newInstance();
      }
      enrich(page);
      Factory.setObjectToField(i,testCase,page);
    }
 catch (    Exception ex) {
      throw new RuntimeException("Can not initialise Page Object!");
    }
  }
}
 

Example 13

From project Blitz, under directory /src/com/laxser/blitz/lama/core/.

Source file: GenericUtils.java

  17 
vote

/** 
 * ????, ????, ????: Generic ??????????????????????
 * @param genericType - Generic ??????
 * @return ???????
 */
public static Class<?>[] getActualClass(Type genericType){
  if (genericType instanceof ParameterizedType) {
    Type[] actualTypes=((ParameterizedType)genericType).getActualTypeArguments();
    Class<?>[] actualClasses=new Class<?>[actualTypes.length];
    for (int i=0; i < actualTypes.length; i++) {
      Type actualType=actualTypes[i];
      if (actualType instanceof Class<?>) {
        actualClasses[i]=(Class<?>)actualType;
      }
 else       if (actualType instanceof GenericArrayType) {
        Type componentType=((GenericArrayType)actualType).getGenericComponentType();
        actualClasses[i]=Array.newInstance((Class<?>)componentType,0).getClass();
      }
    }
    return actualClasses;
  }
  return EMPTY_CLASSES;
}
 

Example 14

From project aerogear-controller, under directory /src/main/java/org/jboss/aerogear/controller/util/.

Source file: TypeNameExtractor.java

  15 
vote

public String nameFor(Type generic){
  if (generic instanceof ParameterizedType) {
    return nameFor((ParameterizedType)generic);
  }
  if (generic instanceof WildcardType) {
    return nameFor((WildcardType)generic);
  }
  if (generic instanceof TypeVariable<?>) {
    return nameFor(((TypeVariable<?>)generic));
  }
  return nameFor((Class<?>)generic);
}
 

Example 15

From project ANNIS, under directory /annis-interfaces/src/main/java/annis/provider/.

Source file: SaltProjectProvider.java

  15 
vote

@Override public void writeTo(SaltProject project,Class<?> type,Type genericType,Annotation[] annotations,MediaType mediaType,MultivaluedMap<String,Object> httpHeaders,OutputStream entityStream) throws IOException, WebApplicationException {
  Resource resource=new XMIResourceImpl();
  resource.getContents().add(project);
  for (  SCorpusGraph corpusGraph : project.getSCorpusGraphs()) {
    for (    SDocument doc : corpusGraph.getSDocuments()) {
      if (doc.getSDocumentGraph() != null) {
        resource.getContents().add(doc.getSDocumentGraph());
      }
    }
  }
  try {
    resource.save(entityStream,null);
  }
 catch (  Exception ex) {
    log.error("exception when serializing SaltProject",ex);
  }
}
 

Example 16

From project arquillian-extension-drone, under directory /drone-impl/src/main/java/org/jboss/arquillian/drone/impl/.

Source file: DroneRegistrar.java

  15 
vote

private static Class<?> getFirstGenericParameterType(Class<?> clazz,Class<?> rawType){
  for (  Type interfaceType : clazz.getGenericInterfaces()) {
    if (interfaceType instanceof ParameterizedType) {
      ParameterizedType ptype=(ParameterizedType)interfaceType;
      if (rawType.isAssignableFrom((Class<?>)ptype.getRawType())) {
        return (Class<?>)ptype.getActualTypeArguments()[0];
      }
    }
  }
  return null;
}
 

Example 17

From project arquillian-extension-warp, under directory /impl/src/main/java/org/jboss/arquillian/warp/impl/client/execution/.

Source file: DefaultRequestEnrichmentService.java

  15 
vote

private boolean isType(RequestFilter<?> filter,Type expectedType){
  Type[] interfaces=filter.getClass().getGenericInterfaces();
  for (  Type type : interfaces) {
    if (type instanceof ParameterizedType) {
      ParameterizedType parametrizedType=(ParameterizedType)type;
      if (parametrizedType.getRawType() == RequestFilter.class) {
        return parametrizedType.getActualTypeArguments()[0] == expectedType;
      }
    }
  }
  return false;
}
 

Example 18

From project arquillian_deprecated, under directory /extensions/drone/src/main/java/org/jboss/arquillian/drone/impl/.

Source file: DroneRegistrar.java

  15 
vote

private static Class<?> getFirstGenericParameterType(Class<?> clazz,Class<?> rawType){
  for (  Type interfaceType : clazz.getGenericInterfaces()) {
    if (interfaceType instanceof ParameterizedType) {
      ParameterizedType ptype=(ParameterizedType)interfaceType;
      if (rawType.isAssignableFrom((Class<?>)ptype.getRawType())) {
        return (Class<?>)ptype.getActualTypeArguments()[0];
      }
    }
  }
  return null;
}
 

Example 19

From project atunit, under directory /atunit-guice/src/main/java/atunit/guice/.

Source file: GuiceContainer.java

  15 
vote

@Override @SuppressWarnings("unchecked") protected void configure(){
  Multimap<Type,Field> fieldsByType=HashMultimap.create();
  for (  Field field : fields.keySet()) {
    fieldsByType.put(field.getGenericType(),field);
  }
  for (  Type type : fieldsByType.keySet()) {
    Collection<Field> fields=fieldsByType.get(type);
    if (fields.size() == 1) {
      Field field=Iterables.getOnlyElement(fields);
      TypeLiteral literal=TypeLiteral.get(type);
      bind(literal).toInstance(this.fields.get(field));
    }
  }
}
 

Example 20

From project atunit_1, under directory /src/atunit/guice/.

Source file: GuiceContainer.java

  15 
vote

@Override @SuppressWarnings("unchecked") protected void configure(){
  Multimap<Type,Field> fieldsByType=Multimaps.newHashMultimap();
  for (  Field field : fields.keySet()) {
    fieldsByType.put(field.getGenericType(),field);
  }
  for (  Type type : fieldsByType.keySet()) {
    Collection<Field> fields=fieldsByType.get(type);
    if (fields.size() == 1) {
      Field field=Iterables.getOnlyElement(fields);
      TypeLiteral literal=TypeLiteral.get(type);
      bind(literal).toInstance(this.fields.get(field));
    }
  }
}
 

Example 21

From project avro, under directory /lang/java/avro/src/main/java/org/apache/avro/reflect/.

Source file: ReflectData.java

  15 
vote

@Override protected boolean isRecord(Object datum){
  if (datum == null)   return false;
  if (super.isRecord(datum))   return true;
  if (datum instanceof Collection)   return false;
  if (datum instanceof Map)   return false;
  return getSchema(datum.getClass()).getType() == Schema.Type.RECORD;
}
 

Example 22

From project beanvalidation-tck, under directory /tests/src/main/java/org/hibernate/beanvalidation/tck/tests/bootstrap/.

Source file: ConfigurationTest.java

  15 
vote

@Test @SpecAssertion(section="4.4.3",id="a") public void testProviderUnderTestDefinesSubInterfaceOfConfiguration(){
  boolean foundSubinterfaceOfConfiguration=false;
  Type[] types=TestUtil.getValidationProviderUnderTest().getClass().getGenericInterfaces();
  for (  Type type : types) {
    if (type instanceof ParameterizedType) {
      ParameterizedType paramType=(ParameterizedType)type;
      Type[] typeArguments=paramType.getActualTypeArguments();
      for (      Type typeArgument : typeArguments) {
        if (typeArgument instanceof Class && Configuration.class.isAssignableFrom((Class)typeArgument)) {
          foundSubinterfaceOfConfiguration=true;
        }
      }
    }
  }
  assertTrue(foundSubinterfaceOfConfiguration,"Could not find subinterface of Configuration");
}