@Override public TypeName getTypeName() { return ParameterizedTypeName.get(ClassNames.DIFF, mUnderlyingStateParamModel.getTypeName().box()); }
@Override public TypeName getTypeName() { return ParameterizedTypeName.get(ClassNames.DIFF, mUnderlyingPropModel.getTypeName().box()); }
@Override public TypeName getTypeName() { return mParamModel.getTypeName().box(); }
private static ParameterizedTypeName getPropMatcherType(PropModel prop) { final TypeName rawType = getRawType(prop.getTypeName()); // We can only match against unparameterized (i.e. raw) types. Thanks, Java. return ParameterizedTypeName.get(ClassNames.HAMCREST_MATCHER, rawType.box()); }
private static boolean hasMatchingField( MethodParamModel param, ImmutableList<FieldModel> fields) { for (FieldModel field : fields) { if (param.getName().equals(field.field.name) && (param.getTypeName().box().equals(field.field.type.box()) || isFromEventTypeSpecifiedInAnnotation(param, field.field.type))) { return true; } } return false; }
/** * Compare a given {@link PropModel} to the underlying propmodel of this instance. If a cached * name is provided, it will override the check with the underlying model. */ public boolean isSameUnderlyingPropModel(PropModel propModel, @Nullable String cachedName) { return (propModel.getName().equals(getName()) || propModel.getName().equals(cachedName)) && propModel.getTypeName().box().equals(mUnderlyingPropModel.getTypeName().box()) && propModel.isOptional() == isOptional() && propModel.getResType() == getResType() && propModel.getVarArgsSingleName().equals(getVarArgsSingleName()); } }
private static boolean hasMatchingInterStageOutput( SpecMethodModel<DelegateMethod, Void> method, MethodParamModel interStageInput) { for (MethodParamModel methodParam : method.methodParams) { if (methodParam.getName().equals(interStageInput.getName()) && methodParam.getTypeName() instanceof ParameterizedTypeName && ((ParameterizedTypeName) methodParam.getTypeName()).rawType.equals(ClassNames.OUTPUT) && ((ParameterizedTypeName) methodParam.getTypeName()).typeArguments.size() == 1 && ((ParameterizedTypeName) methodParam.getTypeName()).typeArguments.get(0).equals( interStageInput.getTypeName().box())) { return true; } } return false; }
public static boolean isPropOutput(SpecModel specModel, MethodParamModel methodParamModel) { final PropModel prop = getPropWithName(specModel, methodParamModel.getName()); return prop != null && methodParamModel.getTypeName() instanceof ParameterizedTypeName && ((ParameterizedTypeName) methodParamModel.getTypeName()).rawType.equals(OUTPUT) && ((ParameterizedTypeName) methodParamModel.getTypeName()).typeArguments.size() == 1 && ((ParameterizedTypeName) methodParamModel.getTypeName()).typeArguments.get(0) .equals(prop.getTypeName().box()); }
public static boolean isStateOutput(SpecModel specModel, MethodParamModel methodParamModel) { final StateParamModel stateValue = SpecModelUtils.getStateValueWithName(specModel, methodParamModel.getName()); return stateValue != null && methodParamModel.getTypeName() instanceof ParameterizedTypeName && ((ParameterizedTypeName) methodParamModel.getTypeName()).rawType.equals(OUTPUT) && ((ParameterizedTypeName) methodParamModel.getTypeName()).typeArguments.size() == 1 && ((ParameterizedTypeName) methodParamModel.getTypeName()).typeArguments.get(0) .equals(stateValue.getTypeName().box()); }
public static boolean isStateValue(SpecModel specModel, MethodParamModel methodParamModel) { final StateParamModel stateValue = SpecModelUtils.getStateValueWithName(specModel, methodParamModel.getName()); return stateValue != null && methodParamModel.getTypeName() instanceof ParameterizedTypeName && ((ParameterizedTypeName) methodParamModel.getTypeName()).rawType .equals(ClassNames.STATE_VALUE) && ((ParameterizedTypeName) methodParamModel.getTypeName()).typeArguments.size() == 1 && ((ParameterizedTypeName) methodParamModel.getTypeName()).typeArguments.get(0) .equals(stateValue.getTypeName().box()); }
static List<SpecModelValidationError> validateStateValues(SpecModel specModel) { final List<SpecModelValidationError> validationErrors = new ArrayList<>(); final ImmutableList<StateParamModel> stateValues = specModel.getStateValues(); for (int i = 0, size = stateValues.size(); i < size - 1; i++) { final StateParamModel thisStateValue = stateValues.get(i); for (int j = i + 1; j < size; j++) { final StateParamModel thatStateValue = stateValues.get(j); if (thisStateValue.getName().equals(thatStateValue.getName())) { if (!thisStateValue.getTypeName().box().equals(thatStateValue.getTypeName().box())) { validationErrors.add(new SpecModelValidationError( thatStateValue.getRepresentedObject(), "State values with the same name must have the same type.")); } if (thisStateValue.canUpdateLazily() != thatStateValue.canUpdateLazily()) { validationErrors.add(new SpecModelValidationError( thatStateValue.getRepresentedObject(), "State values with the same name must have the same annotated value for " + "canUpdateLazily().")); } } } } final Set<String> stateNameSet = stateValues.stream().map(StateParamModel::getName).collect(Collectors.toSet()); validateDuplicateName(stateNameSet, specModel.getProps(), validationErrors); validateDuplicateName(stateNameSet, specModel.getInjectProps(), validationErrors); validateDuplicateName(stateNameSet, specModel.getTreeProps(), validationErrors); return validationErrors; }
@Test public void testIncorrectTypeForResTypeWithVarArg() { when(mPropModel1.getResType()).thenReturn(ResType.BOOL); when(mPropModel1.hasVarArgs()).thenReturn(true); when(mPropModel1.getTypeName()) .thenReturn(ParameterizedTypeName.get(ClassNames.LIST, TypeName.INT.box())); List<SpecModelValidationError> validationErrors = PropValidation.validate( mSpecModel, PropValidation.COMMON_PROP_NAMES, PropValidation.VALID_COMMON_PROPS); assertThat(validationErrors).hasSize(1); assertThat(validationErrors.get(0).element).isEqualTo(mRepresentedObject1); assertThat(validationErrors.get(0).message) .isEqualTo( "A variable argument declared with resType BOOL must be one of the following types: " + "[java.util.List<java.lang.Boolean>]."); }
static TypeSpecDataHolder generateInterStageInputs(SpecModel specModel) { final TypeSpecDataHolder.Builder typeSpecDataHolder = TypeSpecDataHolder.newBuilder(); final ImmutableList<InterStageInputParamModel> interStageInputs = specModel.getInterStageInputs(); for (InterStageInputParamModel interStageInput : interStageInputs) { typeSpecDataHolder.addField( FieldSpec.builder(interStageInput.getTypeName().box(), interStageInput.getName()) .build()); } return typeSpecDataHolder.build(); }
@Before public void setup() { mSpecModel = mock(SpecModel.class); mStateModel = mock(StateParamModel.class); mPropModel = mock(PropModel.class); mDiffModel = mock(RenderDataDiffModel.class); mDiffRepresentedObject = new Object(); when(mSpecModel.getProps()).thenReturn(ImmutableList.<PropModel>of(mPropModel)); when(mSpecModel.getStateValues()).thenReturn(ImmutableList.of(mStateModel)); when(mSpecModel.getRenderDataDiffs()).thenReturn(ImmutableList.of(mDiffModel)); when(mStateModel.getName()).thenReturn("stateName"); when(mStateModel.getTypeName()).thenReturn(TypeName.INT); when(mPropModel.getName()).thenReturn("propName"); when(mPropModel.getTypeName()).thenReturn(TypeName.INT); when(mDiffModel.getName()).thenReturn("stateName"); when(mDiffModel.getTypeName()).thenReturn( ParameterizedTypeName.get(ClassNames.DIFF, TypeName.INT.box()) .annotated(AnnotationSpec.builder(State.class).build())); when(mDiffModel.getRepresentedObject()).thenReturn(mDiffRepresentedObject); when(mDiffModel.getAnnotations()).thenReturn(ImmutableList.of(annotation(State.class))); }
@Test public void testDiffModelHasDifferentParameterFromProp() { when(mDiffModel.getName()).thenReturn("propName"); when(mDiffModel.getTypeName()).thenReturn( ParameterizedTypeName.get(ClassNames.DIFF, TypeName.BOOLEAN.box()) .annotated(AnnotationSpec.builder(Prop.class).build())); when(mDiffModel.getAnnotations()).thenReturn(ImmutableList.of(annotation(Prop.class))); List<SpecModelValidationError> validationErrors = DiffValidation.validate(mSpecModel); assertSingleError(validationErrors, DiffValidation.PROP_MISMATCH_ERROR); }
@Test public void testDiffModelHasDifferentParameterFromState() { when(mDiffModel.getTypeName()).thenReturn( ParameterizedTypeName.get(ClassNames.DIFF, TypeName.BOOLEAN.box()) .annotated(AnnotationSpec.builder(State.class).build())); List<SpecModelValidationError> validationErrors = DiffValidation.validate(mSpecModel); assertSingleError(validationErrors, DiffValidation.STATE_MISMATCH_ERROR); }
@Test public void testNoErrorProp() { when(mDiffModel.getName()).thenReturn("propName"); when(mDiffModel.getTypeName()).thenReturn( ParameterizedTypeName.get(ClassNames.DIFF, TypeName.INT.box()) .annotated(AnnotationSpec.builder(Prop.class).build())); when(mDiffModel.getAnnotations()).thenReturn(ImmutableList.of(annotation(Prop.class))); List<SpecModelValidationError> validationErrors = DiffValidation.validate(mSpecModel); assertThat(validationErrors).hasSize(0); }
@Before public void ListUp() { mDiffTypeSpecWrappingInt = new TypeSpec.DeclaredTypeSpec( ClassNames.DIFF, ClassNames.DIFF.packageName() + "." + ClassNames.DIFF.simpleName(), () -> new TypeSpec(TypeName.OBJECT), ImmutableList.of(), ImmutableList.of(new TypeSpec(TypeName.INT.box()))); }