Java Code Examples for com.squareup.javapoet.ArrayTypeName#of()

The following examples show how to use com.squareup.javapoet.ArrayTypeName#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodRouterGenerator.java    From componentrouter with Apache License 2.0 7 votes vote down vote up
private void addArrConvertMethod(TypeSpec.Builder builder, Map<String, String> arrConvertMap) {
    for (Map.Entry<String, String> entry : arrConvertMap.entrySet()) {
        String methodName = entry.getKey();
        String type = entry.getValue();

        TypeName typeName = TypeNameUtils.getTypeName(type.replace("[]", ""));
        ArrayTypeName arrayTypeName = ArrayTypeName.of(typeName);
        System.out.println(arrayTypeName);
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(methodName)
                .addModifiers(Modifier.PRIVATE, Modifier.STATIC)
                .addParameter(Object[].class, "objs")
                .returns(arrayTypeName)
                .addStatement("$T arr=new $T[objs.length]", arrayTypeName, typeName);
        methodBuilder.beginControlFlow("for(int i = 0;i<objs.length;i++)")
                .addStatement("arr[i]=($T)objs[i]", typeName)
                .endControlFlow();
        methodBuilder.addStatement("return arr");
        builder.addMethod(methodBuilder.build());
    }
}
 
Example 2
Source File: PsiTypeUtils.java    From litho with Apache License 2.0 6 votes vote down vote up
static TypeName getTypeName(PsiType type) {
  if (type instanceof PsiPrimitiveType) {
    // float
    return getPrimitiveTypeName((PsiPrimitiveType) type);
  } else if (type instanceof PsiClassType && ((PsiClassType) type).getParameterCount() > 0) {
    // Set<Integer>
    PsiClassType classType = (PsiClassType) type;
    return ParameterizedTypeName.get(
        guessClassName(classType.rawType().getCanonicalText()),
        getTypeNameArray(classType.getParameters()));
  } else if (type instanceof PsiArrayType) {
    // int[]
    PsiType componentType = ((PsiArrayType) type).getComponentType();
    return ArrayTypeName.of(getTypeName(componentType));
  } else if (type.getCanonicalText().contains("?")) {
    // ? extends Type
    return getWildcardTypeName(type.getCanonicalText());
  } else {
    return guessClassName(type.getCanonicalText());
  }
}
 
Example 3
Source File: VarArgSetterSupport.java    From anno4j with Apache License 2.0 6 votes vote down vote up
@Override
MethodSpec buildSignature(RDFSClazz domainClazz, OntGenerationConfig config) throws RepositoryException {
    if (getRanges() != null) {
        MethodSpec.Builder setter = buildParameterlessSetterSignature(domainClazz, config);

        // Get the vararg parameter type:
        TypeName paramType = ArrayTypeName.of(getParameterType(config, false));


        return setter.addParameter(paramType, "values")
                     .varargs()
                     .build();
    } else {
        return null;
    }
}
 
Example 4
Source File: AnnotatedDurableEntityClass.java    From mnemonic with Apache License 2.0 6 votes vote down vote up
protected void genNFieldInfo() {
  FieldInfo dynfieldinfo, fieldinfo;
  List<long[]> finfo = new ArrayList<long[]>();
  for (String name : m_gettersinfo.keySet()) {
    dynfieldinfo = m_dynfieldsinfo.get(name);
    if (dynfieldinfo.id > 0) {
      finfo.add(new long[]{dynfieldinfo.id, dynfieldinfo.fieldoff, dynfieldinfo.fieldsize});
    }
  }

  fieldinfo = new FieldInfo();
  fieldinfo.name = String.format("m_nfieldinfo_%s", Utils.genRandomString());
  fieldinfo.type = ArrayTypeName.of(ArrayTypeName.of(TypeName.LONG));
  String initlstr = Utils.toInitLiteral(finfo);
  fieldinfo.specbuilder = FieldSpec.builder(fieldinfo.type, fieldinfo.name, Modifier.PRIVATE, Modifier.STATIC)
      .initializer("$1L", initlstr);
  m_fieldsinfo.put("nfieldinfo", fieldinfo);
}
 
Example 5
Source File: TypeUtils.java    From grouter-android with Apache License 2.0 5 votes vote down vote up
public static TypeName getTypeName(String name) {
    switch (name) {
        case "void":
            return TypeName.VOID;
        case "int":
            return TypeName.get(int.class);
        case "int[]":
            return TypeName.get(int[].class);
        case "long":
            return TypeName.get(long.class);
        case "long[]":
            return TypeName.get(long[].class);
        case "double":
            return TypeName.get(double.class);
        case "double[]":
            return TypeName.get(double[].class);
        case "float":
            return TypeName.get(float.class);
        case "float[]":
            return TypeName.get(float[].class);
        case "boolean":
            return TypeName.get(boolean.class);
        case "boolean[]":
            return TypeName.get(boolean[].class);
        case "short":
            return TypeName.get(short.class);
        case "short[]":
            return TypeName.get(short[].class);
        case "char":
            return TypeName.get(char.class);
        case "char[]":
            return TypeName.get(char[].class);
        default:
            if (name.endsWith("[]")) {
                return ArrayTypeName.of(ClassName.bestGuess(name.substring(0, name.length() - 2)));
            }
            return ClassName.bestGuess(name);
    }
}
 
Example 6
Source File: IntrospectorUtil.java    From reflection-no-reflection with Apache License 2.0 5 votes vote down vote up
public TypeName getClassName(String className) {
    if (className.endsWith("[]")) {
        final String componentName = className.substring(0, className.lastIndexOf('['));
        return ArrayTypeName.of(getClassName(componentName));
    } else if (className.contains(".")) {
        final String packageName = className.substring(0, className.lastIndexOf('.'));
        final String simpleName = className.substring(className.lastIndexOf('.')+1);
        return ClassName.get(packageName, simpleName);
    } else {
        //for primitives
        switch (className) {
            case "short" :
                return TypeName.get(short.class);
            case "byte" :
                return TypeName.get(byte.class);
            case "int" :
                return TypeName.get(int.class);
            case "long" :
                return TypeName.get(long.class);
            case "float" :
                return TypeName.get(float.class);
            case "double" :
                return TypeName.get(double.class);
            case "boolean" :
                return TypeName.get(boolean.class);
            case "char" :
                return TypeName.get(char.class);
        }
        throw new RuntimeException("Impossible to get typename for " + className);
    }
}
 
Example 7
Source File: AnnotationCreatorClassPoolVisitor.java    From reflection-no-reflection with Apache License 2.0 5 votes vote down vote up
private JavaFile buildAnnotationImpl(Class<? extends Annotation> annotationClass) {
    String aClassName = annotationClass.getName();
    MethodSpec annotationTypeMethod = MethodSpec.methodBuilder("annotationType")
        .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
        .returns(ClassName.get(java.lang.Class.class))
        .addStatement("return $L.class", aClassName)
        .build();

    TypeSpec.Builder annotationImplType = TypeSpec.classBuilder(annotationClass.getSimpleName() + "$$Impl")
        .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
        .addSuperinterface(util.getClassName(annotationClass))
        .addMethod(annotationTypeMethod);

    System.out.println("annotation methods " + annotationClass.getMethods().size());
    System.out.println("annotation fields " + annotationClass.getFields().length);
    System.out.println("annotation " + annotationClass.toString());

    for (Method method : annotationClass.getMethods()) {
        TypeName type;
        if (method.getReturnType().isArray()) {
            //important to use the component here as there is no method get(TypeName)
            //we fail to be detected as an array (see ArrayTypeName.get implementation)
            type = ArrayTypeName.of(util.getClassName(method.getReturnType().getComponentType()));
        } else {
            type = TypeName.get(method.getReturnType());
        }
        FieldSpec field = FieldSpec.builder(type, method.getName(), Modifier.PRIVATE).build();
        annotationImplType.addField(field);

        MethodSpec setterMethod = createSetterMethod(type, method.getName());
        annotationImplType.addMethod(setterMethod);
        MethodSpec getterMethod = createGetterMethod(type, method.getName());
        annotationImplType.addMethod(getterMethod);
    }

    return JavaFile.builder(targetPackageName, annotationImplType.build()).build();
}
 
Example 8
Source File: ExtendedTypeElement.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
public TypeName asTypeName() {
  final ClassName className = ClassName.get(getTypeElement());
  if (isArrayElement) {
    return ArrayTypeName.of(className);
  }
  return className;
}
 
Example 9
Source File: TypeUtils.java    From grouter-android with Apache License 2.0 5 votes vote down vote up
public static TypeName getTypeName(String name) {
    switch (name) {
        case "void":
            return TypeName.VOID;
        case "int":
            return TypeName.get(int.class);
        case "int[]":
            return TypeName.get(int[].class);
        case "long":
            return TypeName.get(long.class);
        case "long[]":
            return TypeName.get(long[].class);
        case "double":
            return TypeName.get(double.class);
        case "double[]":
            return TypeName.get(double[].class);
        case "float":
            return TypeName.get(float.class);
        case "float[]":
            return TypeName.get(float[].class);
        case "boolean":
            return TypeName.get(boolean.class);
        case "boolean[]":
            return TypeName.get(boolean[].class);
        case "short":
            return TypeName.get(short.class);
        case "short[]":
            return TypeName.get(short[].class);
        case "char":
            return TypeName.get(char.class);
        case "char[]":
            return TypeName.get(char[].class);
        default:
            if (name.endsWith("[]")) {
                return ArrayTypeName.of(ClassName.bestGuess(name.substring(0, name.length() - 2)));
            }
            return ClassName.bestGuess(name);
    }
}
 
Example 10
Source File: JniProcessor.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
TypeName toTypeName(TypeMirror t, boolean useJni) {
    if (t.getKind() == TypeKind.ARRAY) {
        return ArrayTypeName.of(toTypeName(((ArrayType) t).getComponentType(), useJni));
    }
    TypeName typeName = TypeName.get(t);
    if (useJni && shouldDowncastToObjectForJni(typeName)) {
        return TypeName.OBJECT;
    }
    return typeName;
}
 
Example 11
Source File: AsArray.java    From raml-java-tools with Apache License 2.0 5 votes vote down vote up
@Override
public TypeName typeName(ReferencePluginContext referencePluginContext, TypeDeclaration ramlType, TypeName currentSuggestion) {

    if ( ramlType instanceof ArrayTypeDeclaration) {
        return ArrayTypeName.of(((ParameterizedTypeName)currentSuggestion).typeArguments.get(0).box());
    } else {

        return currentSuggestion;
    }
}
 
Example 12
Source File: TypeNameUtils.java    From componentrouter with Apache License 2.0 5 votes vote down vote up
public static TypeName getTypeName(String fullClassName) {
    if (fullClassName.contains("[]")) {
        TypeName simpleName = getTypeName(fullClassName.replace("[]", ""));
        return ArrayTypeName.of(simpleName);
    }
    int index = fullClassName.lastIndexOf(".");
    if (index == -1) {
        fullClassName = fullClassName.toLowerCase();
        switch (fullClassName) {
            case "short":
                return TypeName.SHORT;
            case "int":
                return TypeName.INT;
            case "long":
                return TypeName.LONG;
            case "float":
                return TypeName.FLOAT;
            case "double":
                return TypeName.DOUBLE;
            case "char":
                return TypeName.CHAR;
            case "byte":
                return TypeName.BYTE;
            case "boolean":
                return TypeName.BOOLEAN;
            default:
                return TypeName.INT;
        }
    }
    String packageName = fullClassName.substring(0, index);
    String className = fullClassName.replace(packageName, "").replace(".", "");
    return ClassName.get(packageName, className);
}
 
Example 13
Source File: ControllerProcessor.java    From AndServer with Apache License 2.0 4 votes vote down vote up
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);
    mFiler = processingEnv.getFiler();
    mElements = processingEnv.getElementUtils();
    mLog = new Logger(processingEnv.getMessager());

    mContext = TypeName.get(mElements.getTypeElement(Constants.CONTEXT_TYPE).asType());
    mStringUtils = TypeName.get(mElements.getTypeElement(Constants.STRING_UTIL_TYPE).asType());
    mCollectionUtils = TypeName.get(mElements.getTypeElement(Constants.COLLECTION_UTIL_TYPE).asType());
    mTypeWrapper = ClassName.get(mElements.getTypeElement(Constants.TYPE_WRAPPER_TYPE));
    mMediaType = TypeName.get(mElements.getTypeElement(Constants.MEDIA_TYPE).asType());
    mOnRegisterType = TypeName.get(mElements.getTypeElement(Constants.ON_REGISTER_TYPE).asType());
    mRegisterType = TypeName.get(mElements.getTypeElement(Constants.REGISTER_TYPE).asType());

    mBodyMissing = TypeName.get(mElements.getTypeElement(Constants.BODY_MISSING).asType());
    mCookieMissing = TypeName.get(mElements.getTypeElement(Constants.COOKIE_MISSING).asType());
    mParamMissing = TypeName.get(mElements.getTypeElement(Constants.PARAM_MISSING).asType());
    mHeaderMissing = TypeName.get(mElements.getTypeElement(Constants.HEADER_MISSING).asType());
    mPathMissing = TypeName.get(mElements.getTypeElement(Constants.PATH_MISSING).asType());
    mParamError = TypeName.get(mElements.getTypeElement(Constants.PARAM_ERROR).asType());

    mConverter = TypeName.get(mElements.getTypeElement(Constants.CONVERTER_TYPE).asType());
    mAdapter = TypeName.get(mElements.getTypeElement(Constants.ADAPTER_TYPE).asType());
    mMappingAdapter = TypeName.get(mElements.getTypeElement(Constants.MAPPING_ADAPTER_TYPE).asType());
    mHandler = TypeName.get(mElements.getTypeElement(Constants.HANDLER_TYPE).asType());
    mMappingHandler = TypeName.get(mElements.getTypeElement(Constants.MAPPING_HANDLER_TYPE).asType());
    mView = TypeName.get(mElements.getTypeElement(Constants.VIEW_TYPE).asType());
    mViewObject = TypeName.get(mElements.getTypeElement(Constants.VIEW_TYPE_OBJECT).asType());

    mRequest = TypeName.get(mElements.getTypeElement(Constants.REQUEST_TYPE).asType());
    mMultipartRequest = TypeName.get(mElements.getTypeElement(Constants.MULTIPART_REQUEST_TYPE).asType());
    mResponse = TypeName.get(mElements.getTypeElement(Constants.RESPONSE_TYPE).asType());
    mHttpMethod = TypeName.get(mElements.getTypeElement(Constants.HTTP_METHOD_TYPE).asType());
    mSession = TypeName.get(mElements.getTypeElement(Constants.SESSION_TYPE).asType());
    mRequestBody = TypeName.get(mElements.getTypeElement(Constants.REQUEST_BODY_TYPE).asType());
    mMultipartFile = TypeName.get(mElements.getTypeElement(Constants.MULTIPART_FILE_TYPE).asType());
    mMultipartFileArray = ArrayTypeName.of(mMultipartFile);
    mMultipartFileList = ParameterizedTypeName.get(ClassName.get(List.class), mMultipartFile);

    mAddition = TypeName.get(mElements.getTypeElement(Constants.ADDITION_TYPE).asType());
    mMapping = TypeName.get(mElements.getTypeElement(Constants.MAPPING_TYPE).asType());
    mMimeTypeMapping = TypeName.get(mElements.getTypeElement(Constants.MIME_MAPPING_TYPE).asType());
    mMethodMapping = TypeName.get(mElements.getTypeElement(Constants.METHOD_MAPPING_TYPE).asType());
    mPairMapping = TypeName.get(mElements.getTypeElement(Constants.PAIR_MAPPING_TYPE).asType());
    mPathMapping = TypeName.get(mElements.getTypeElement(Constants.PATH_MAPPING_TYPE).asType());
    mMappingList = ParameterizedTypeName.get(ClassName.get(Map.class), mMapping, mHandler);
}
 
Example 14
Source File: ListSetters.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private ArrayTypeName asArrayOfModeledEnum() {
    return ArrayTypeName.of(modeledEnumElement());
}
 
Example 15
Source File: ListSetters.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private ArrayTypeName asArray() {
    return ArrayTypeName.of(listElementType());
}
 
Example 16
Source File: ListSetters.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private TypeName asConsumerBuilderArray() {
    ParameterizedTypeName consumerBuilder = ParameterizedTypeName.get(ClassName.get(Consumer.class),
                                                                      listElementConsumerBuilderType());
    return ArrayTypeName.of(consumerBuilder);
}
 
Example 17
Source File: TypeUtils.java    From jackdaw with Apache License 2.0 4 votes vote down vote up
public static TypeName getArrayTypeName(final Element parameter) {
    return ArrayTypeName.of(TypeName.get(parameter.asType()));
}
 
Example 18
Source File: TypeVariableResolver.java    From kripton with Apache License 2.0 4 votes vote down vote up
/**
 * Resolve.
 *
 * @param inputType the input type
 * @return the type name
 */
public TypeName resolve(TypeName inputType) {
	if (!hasTypeVariables())
		return inputType;
	
	if (inputType.isPrimitive() || inputType.isBoxedPrimitive() || inputType.equals(TypeName.get(String.class))) {
		return inputType;
	}
	
	if (TypeName.VOID.equals(inputType)) return inputType;
	
	TypeName resolved=null;
	if (inputType instanceof ParameterizedTypeName)
	{
		ParameterizedTypeName inputParametrizedTypeName=(ParameterizedTypeName)inputType;
		
		TypeName[] typeArguments=new TypeName[inputParametrizedTypeName.typeArguments.size()];
		int i=0;
		for (TypeName item: inputParametrizedTypeName.typeArguments)
		{
			typeArguments[i]=resolve(item);
			i++;
		}
		
		resolved=ParameterizedTypeName.get((ClassName)resolve(inputParametrizedTypeName.rawType), typeArguments);
		
		return resolved;
	} else if (inputType instanceof ArrayTypeName)
	{
		ArrayTypeName inputTypeName=(ArrayTypeName)inputType;
		
		resolved=ArrayTypeName.of(resolve(inputTypeName.componentType));
		
		return resolved;
	} else {
		// it's not a type variable
		if (inputType.toString().contains(".")) {
			return inputType;
		}
		// ASSERT: simple typeName 
		if (typeVariableMap.containsKey(inputType.toString())) {
			TypeName type = typeVariableMap.get(inputType.toString());
			return type;
		} else if (declaredTypeArgumentList.size()==1){
			resolved = declaredTypeArgumentList.get(0);
			// if we found an unique type variable, we use it.
			//typeVariableMap = new HashMap<>();
			//typeVariableMap.put(inputTypeName.toString(), resolved);

			return resolved;
		}
		
		AssertKripton.assertTrue(false, "In type hierarchy of %s '%s' there is a unresolved type variable named '%s'. Define it with @BindTypeVariables", element.getKind()==ElementKind.CLASS ? "class" :"interface", element.getQualifiedName(),
				inputType.toString());
		
		return resolved;
	}
	
}
 
Example 19
Source File: JTypeName.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
private ArrayTypeName arrayName( JArrayType type ) {
    return ArrayTypeName.of( typeName( type.getComponentType() ) );
}
 
Example 20
Source File: TypeUtility.java    From kripton with Apache License 2.0 2 votes vote down vote up
/**
 * Array type name.
 *
 * @param type
 *            the type
 * @return the array type name
 */
public static ArrayTypeName arrayTypeName(Type type) {
	return ArrayTypeName.of(type);
}