FormatterJava15.java :  » Testing » MockCreator » net » sf » mockcreator » codegeneration » Java Open Source

Java Open Source » Testing » MockCreator 
MockCreator » net » sf » mockcreator » codegeneration » FormatterJava15.java
package net.sf.mockcreator.codegeneration;

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Type;


public class FormatterJava15 implements IFormatter {

  private Class clz;
  private Method method;
  private TypeUtilsJava15 typeUtils = new TypeUtilsJava15();
  
  public void setClass(Class clz) {
    this.clz = clz;
  }

  public void setMethod(Method method) {
    this.method = method;
  }

  public String getClassName() {
    StringBuffer sb = new StringBuffer();
    
    String clzName = clz.getSimpleName();
    int dot = clzName.lastIndexOf('.');
    if( dot >= 0 ) {
      clzName = clzName.substring(dot+1);
    }
    sb.append(clzName);
    
    Type[] types = clz.getTypeParameters();
    if( types.length > 0 ) {
      sb.append('<');
      for( int i=0; i<types.length; ++i ) {
        if( i != 0 ) sb.append(", ");
        sb.append(typeUtils.getFullCanonicalTypeSpellingForTypeVariable(types[i]));
      }
      sb.append('>');
    }
    
    return sb.toString();
  }

  public String getMockedClassName() {
    StringBuffer sb = new StringBuffer();
    
    String clzName = clz.getSimpleName();
    int dot = clzName.lastIndexOf('.');
    if( dot >= 0 ) {
      clzName = clzName.substring(dot+1);
    }
    sb.append(clzName);
    
    Type[] types = clz.getTypeParameters();
    if( types.length > 0 ) {
      sb.append('<');
      for( int i=0; i<types.length; ++i ) {
        if( i != 0 ) sb.append(", ");
        sb.append(typeUtils.getCanonicalTypeSpelling(types[i]));
      }
      sb.append('>');
    }
    
    return sb.toString();
  }
  
  public String getReturnType() {
    return typeUtils.getCanonicalTypeSpelling(method.getGenericReturnType());
  }

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

  public String getParameters() {
    Type[] params = method.getGenericParameterTypes();
    
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < params.length; ++i) {
          if( i > 0 ) sb.append(", ");
          
        if( i == params.length-1 && method.isVarArgs() ){
          if( params[i] instanceof GenericArrayType ) {
            GenericArrayType gat = (GenericArrayType) params[i];
            String p = typeUtils.getCanonicalTypeSpelling(gat.getGenericComponentType());
            sb.append(p+" ... p"+i);
          } else {
            Class clz = (Class) params[i];
            String p = typeUtils.getCanonicalTypeSpelling(clz.getComponentType());
            sb.append(p+" ... p"+i);
          }
        } else {
            sb.append(typeUtils.getCanonicalTypeSpelling(params[i]));
            sb.append(" ");
            sb.append("p"+i);
        }
      }
  
      return sb.toString();    
  }

  public String getObjectParameters() {
    Type[] params = method.getGenericParameterTypes();
    
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < params.length; ++i) {
          if( i > 0 ) sb.append(", ");
        
        if( i == params.length-1 && method.isVarArgs() ) {
          if( params[i] instanceof GenericArrayType ) {
            GenericArrayType gat = (GenericArrayType) params[i];
            String p = typeUtils.getCanonicalTypeSpelling(gat.getGenericComponentType());
            sb.append(p+" ... p"+i);
          } else {
            Class clz = (Class) params[i];
            String p = typeUtils.getCanonicalTypeSpelling(clz.getComponentType());
            sb.append(p+" ... p"+i);
          }
        } else {
            sb.append(typeUtils.getCanonicalTypeSpelling(Object.class));
            sb.append(" ");
            sb.append("p"+i);
        }
      }
  
      return sb.toString();    
  }  
  
  public String getExceptions() {
    Type[] exs = method.getGenericExceptionTypes();
    if( exs.length == 0 ) return "";
      
    StringBuffer sb = new StringBuffer();
    sb.append(" throws ");
      for (int i = 0; i < exs.length; ++i) {
        if( i != 0 ) sb.append(", ");
          sb.append(typeUtils.getCanonicalTypeSpelling(exs[i]));
      }
  
      return sb.toString();    
  }
  
  public String getEnum() {
    if( !clz.isEnum() ) return null;
    StringBuffer sb = new StringBuffer();
    sb.append("public static enum ");
    sb.append(clz.getSimpleName());
    sb.append(" {");
    
    Object[] constants = clz.getEnumConstants();
    for( int i=0; i<constants.length; ++i ) {
      if( i!=0 ) sb.append(", ");
      sb.append(constants[i]);
    }
    
    sb.append(" };");
    return sb.toString();
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.