001package net.gdface.codegen.thrift.metadata; 002 003import com.facebook.swift.codec.metadata.DecoratorThriftFieldMetadata; 004import com.facebook.swift.codec.metadata.ThriftFieldMetadataUtil; 005import com.google.common.base.Predicate; 006import com.google.common.reflect.TypeToken; 007 008import net.gdface.annotation.CodegenInvalidValue; 009import net.gdface.annotation.CodegenLength; 010 011public class CodegenPreAllocFilter implements Predicate<DecoratorThriftFieldMetadata> { 012 public static final CodegenPreAllocFilter PREALLOC_FILTER = new CodegenPreAllocFilter(); 013 public CodegenPreAllocFilter() { 014 } 015 016 @Override 017 public boolean apply(DecoratorThriftFieldMetadata input) { 018 Class<?> javaType = TypeToken.of(input.getThriftType().getJavaType()).getRawType(); 019 if(javaType.isPrimitive() || javaType.isEnum()){ 020 return true; 021 } 022 if(input.isCanPrimitive() && null !=ThriftFieldMetadataUtil.extractFieldAnnotation(input,CodegenInvalidValue.class)){ 023 return true; 024 } 025 CodegenLength ann = ThriftFieldMetadataUtil.extractFieldAnnotation(input,CodegenLength.class); 026 if(ann != null){ 027 return ann.prealloc(); 028 } 029 return false; 030 } 031 032}