EventBusUtilGenerator extends Generator { String addHandlerParamJson( JClassType type ) { StringBuilder json = new StringBuilder( 128 ); json.append( '"' ).append( '{' ); JMethod[] methods = type.getMethods(); for ( JMethod eventMethod : methods ) { if ( eventMethod.isPublic() && !eventMethod.isStatic() && ( eventMethod.isConstructor() == null ) && !"void".equalsIgnoreCase( eventMethod.getReturnType().getSimpleSourceName() ) && !eventMethod.getName().equals( "getAssociatedType" ) ) { String propertyName = StringUtils.uncapitalize( eventMethod.getName().substring( 3 ) ); String simpleType = type.getField( propertyName ).getType().getSimpleSourceName(); if ( "string".equalsIgnoreCase( simpleType ) ) { json.append( "\\\"" + propertyName + "\\\":\\\"\" + event." + eventMethod.getName() + "() + \"\\\"" ); } else { json.append( "\\\"" + propertyName + "\\\":\" + event." + eventMethod.getName() + "() + \"" ); } json.append( ',' ); } } int lastIndex = json.lastIndexOf( "," ); if ( lastIndex == -1 ) { json.append( '}' ); } else { json.setCharAt( lastIndex, '}' ); } return json.append( '"' ).toString(); }  @Override String generate( TreeLogger logger, GeneratorContext context, String typeName );  }
@Test public void addHandlerParamJson_ReturnsValidEmptyJson_WhenNoGettersWereFound() throws Exception { JClassType type = mock( JClassType.class ); when( type.getMethods() ).thenReturn( new JMethod[ 0 ] ); EventBusUtilGenerator generator = new EventBusUtilGenerator(); String s = generator.addHandlerParamJson( type ); assertEquals( s, "\"{}\"" ); assertJsonIsValid( s, Collections.emptyMap() ); }
@Test public void addHandlerParamJson_ReturnsValidEmptyJson_WhenTwoSimpleGettersWereFound() throws Exception { JType stringType = mockType( "string" ); JField stringField = mockField( stringType, "stringField" ); JMethod getStringField = mockMethod( stringField ); JType intType = mockType( "int" ); JField intField = mockField( intType, "intField" ); JMethod getIntField = mockMethod( intField ); JClassType type = mock( JClassType.class ); when( type.getField( stringField.getName() ) ).thenReturn( stringField ); when( type.getField( intField.getName() ) ).thenReturn( intField ); when( type.getMethods() ).thenReturn( new JMethod[] { getStringField, getIntField } ); EventBusUtilGenerator generator = new EventBusUtilGenerator(); String s = generator.addHandlerParamJson( type ); Map<String, Object> expectedContent = new HashMap<>( 4 ); expectedContent.put( stringField.getName(), "stringValue" ); expectedContent.put( intField.getName(), 1 ); assertJsonIsValid( s, expectedContent ); }