@Override public Type unNestArray() { if (existing.isArray()) { return existing(existing.getComponentType()); } else { throw new RuntimeException("not an array type: " + this); } }
@Override public Type nestArray() { return existing(Array.newInstance(existing, 0).getClass()); }
private static PrimitiveType primitive(Class<?> existingPrimitive, String identifier, Class<?> boxedType) { if (!existingPrimitive.isPrimitive()) { throw new RuntimeException("Not a primitive " + existingPrimitive); } PrimitiveType existingType = new PrimitiveType(existingPrimitive, identifier, identifier, existing(boxedType)); primitives.put(existingPrimitive, existingType); return existingType; }
private ImmutableList<Parameter> convertParameters(Class<?>[] parameterTypes) { ArrayList<Parameter> parameters = new ArrayList<Parameter>(); for (int i = 0; i < parameterTypes.length; i++) { Class<?> paramClass = parameterTypes[i]; parameters.add( new Parameter( ExistingType.existing(paramClass), "arg"+i, i)); } return ImmutableList.from(parameters); }
public static ExistingType existing(Class<?> existing) { if (existing.isPrimitive()) { ExistingType existingType = primitives.get(existing); if (existingType == null) { throw new RuntimeException("Unexpected primitive " + existing); } return existingType; } String identifier; String signature; if (existing.isArray()) { ExistingType component = existing(existing.getComponentType()); identifier = "[" + component.getSignature(); signature = identifier; } else { identifier = existing.getName().replace(".", "/"); signature = "L" + identifier + ";"; } return new ExistingType(existing, identifier, signature); }
@Override public Field getField(String name) { // TODO: add static java.lang.reflect.Field field; try { field = existing.getField(name); } catch (NoSuchFieldException e) { return null; } catch (SecurityException e) { return null; } if (field != null) { return new Field(MemberFlags.fromReflection(field), existing(field.getType()), field.getName()); } return null; }
@Override public Method getMethod(String methodName, int parameterCount) { // TODO: add static java.lang.reflect.Method[] declaredMethods = existing.getDeclaredMethods(); for (java.lang.reflect.Method method : declaredMethods) { Class<?>[] parameterTypes = method.getParameterTypes(); if (method.getName().equals(methodName) && parameterTypes.length == parameterCount) { ImmutableList<Parameter> parameters = convertParameters(parameterTypes); return new Method( this.getClassIdentifier(), MemberFlags.fromReflection(method), existing(method.getReturnType()), methodName, parameters, ImmutableList.<Statement>empty(), existing.isInterface()); } } if (existing.getSuperclass() != null) { return existing(existing.getSuperclass()).getMethod(methodName, parameterCount); } return null; }