BeanInjector injector = new BeanInjector( injectionInfo ); if ( injector.hasProperty( targetStepMeta, target.getAttributeKey() ) ) { injector.setProperty( targetStepMeta, target.getAttributeKey(), null, source.getField() ); } else {
case ARRAY: Object existArray = data != null ? extendArray( s, obj, index + 1 ) : checkArray( s, obj, index ); if ( existArray == null ) { next = createObject( s.leafClass, root ); Array.set( existArray, index, next ); case LIST: List<Object> existList = data != null ? extendList( s, obj, index + 1 ) : checkList( s, obj, index ); if ( existList == null ) { next = createObject( s.leafClass, root ); existList.set( index, next ); next = s.field.get( obj ); if ( next == null ) { next = createObject( s.leafClass, root ); s.field.set( obj, next ); Object existArray = data != null ? extendArray( s, obj, index + 1 ) : checkArray( s, obj, index ); if ( existArray == null ) { break; case LIST: List<Object> existList = data != null ? extendList( s, obj, index + 1 ) : checkList( s, obj, index ); if ( existList == null ) {
@SuppressWarnings ( "unchecked" ) private Object getPropVal( Object obj, String propName, Queue<BeanLevelInfo> beanInfos ) { BeanLevelInfo info = beanInfos.remove(); if ( beanInfos.isEmpty() ) { return getObjFromBeanInfo( obj, info ); } obj = getObjFromBeanInfo( obj, info ); switch ( info.dim ) { case LIST: return ( (List) requireNonNull( obj ) ).stream() .map( o -> getPropVal( o, propName, newLinkedList( beanInfos ) ) ) .collect( Collectors.toList() ); case ARRAY: return Arrays.stream( (Object[]) requireNonNull( obj ) ) .map( o -> getPropVal( o, propName, newLinkedList( beanInfos ) ) ) .toArray( Object[]::new ); case NONE: return getPropVal( obj, propName, beanInfos ); } throw new IllegalStateException( "Unexpected value of BeanLevelInfo.dim " + info.dim ); }
setProperty( root, prop, 0, data != null ? data.get( 0 ) : null, dataName, dataValue ); } catch ( Exception ex ) { throw new KettleException( "Error inject property '" + propName + "' into " + root.getClass(), ex ); if ( data != null ) { for ( int i = 0; i < data.size(); i++ ) { setProperty( root, prop, i, data.get( i ), dataName, dataValue ); boolean found = setProperty( root, prop, i, null, null, dataValue ); if ( !found ) { break;
BeanInjector injector = new BeanInjector( injectionInfo ); if ( injector.hasProperty( targetStepMeta, target.getAttributeKey() ) ) { injector.setProperty( targetStepMeta, target.getAttributeKey(), rows, source.getField() ); wasInjection = true; injector.runPostInjectionProcessing( targetStepMeta );
/** * Sets the properties of this StepMetaProps on {@param stepMetaInterface} * <p> * This method mutates the stepMeta, as opposed to returning a new instance, to match * more cleanly to Kettle's {@link StepMetaInterface#loadXML} design, which loads props into * an instance. */ public StepMetaInterface to( StepMetaInterface stepMetaInterface ) { BeanInjectionInfo info = new BeanInjectionInfo( stepMetaInterface.getClass() ); BeanInjector injector = new BeanInjector( info ); info.getProperties().values().forEach( property -> assignValueForProp( property, stepMetaInterface, injector ) ); return stepMetaInterface; }
/** * Retrieves the raw prop value from root object. * <p> * The similar {@link #getProperty(Object, String)} method (also in this class )should be * revisited and possibly eliminated. That version attempts to retrieve indexed prop * vals from lists/arrays, but doesn't provide a way to retrieve the list or array objects * themselves. */ public Object getPropVal( Object root, String propName ) { Queue<BeanLevelInfo> beanInfos = newLinkedList( Optional.ofNullable( info.getProperties().get( propName ) ) .orElseThrow( () -> new IllegalArgumentException( "Property not found: " + propName ) ) .path ); beanInfos.remove(); // pop off root return getPropVal( root, propName, beanInfos ); }
private void injectVal( BeanInjectionInfo.Property beanInfoProp, Prop prop, StepMetaInterface stepMetaInterface, BeanInjector injector ) { if ( prop.value == null || prop.value.size() == 0 ) { prop.value = singletonList( null ); } try { injector.setProperty( stepMetaInterface, beanInfoProp.getName(), prop.value.stream() .map( value -> { RowMetaAndData rmad = new RowMetaAndData(); rmad.addValue( new ValueMetaString( prop.getName() ), envSubs( value ) ); return rmad; } ).collect( Collectors.toList() ), beanInfoProp.getName() ); } catch ( KettleException e ) { throw new RuntimeException( e ); } }
/** * Retuns an instance of this class with stepMeta properties mapped * to a list of {@link PropGroup} */ public static StepMetaProps from( StepMetaInterface stepMeta ) { StepMetaProps propMap = new StepMetaProps( stepMeta ); propMap.stepMeta = stepMeta; // use metadata injection to extract properties BeanInjectionInfo info = new BeanInjectionInfo( stepMeta.getClass() ); BeanInjector injector = new BeanInjector( info ); propMap.populateGroups( stepMeta, info, injector ); return propMap; }
@SuppressWarnings ( "unchecked" ) private List<Object> getPropVal( StepMetaInterface stepMeta, BeanInjector injector, BeanInjectionInfo.Property prop ) { try { List ret; Object o = injector.getPropVal( stepMeta, prop.getName() ); if ( o instanceof List ) { ret = (List<Object>) o; } else if ( o instanceof Object[] ) { ret = asList( (Object[]) o ); } else { ret = singletonList( o ); } return maybeEncrypt( prop.getName(), ret ); } catch ( Exception e ) { throw new RuntimeException( e ); } }
/** * Check enum property. */ protected void check( String propertyName, EnumGetter getter, Class<?> enumType ) throws KettleException { ValueMetaInterface valueMeta = new ValueMetaString( "f" ); Object[] values = enumType.getEnumConstants(); for ( Object v : values ) { injector.setProperty( meta, propertyName, setValue( valueMeta, v ), "f" ); assertEquals( v, getter.get() ); } try { injector.setProperty( meta, propertyName, setValue( valueMeta, "###" ), "f" ); fail( "Should be passed to enum" ); } catch ( KettleException ex ) { } skipPropertyTest( propertyName ); }
private static BeanInjector buildBeanInjectorFor( Class<?> clazz ) { BeanInjectionInfo metaBeanInfo = new BeanInjectionInfo( clazz ); return new BeanInjector( metaBeanInfo ); }
/** * Check string-to-int property. */ protected void checkStringToInt( String propertyName, IntGetter getter, String[] codes, int[] ids ) throws KettleException { if ( codes.length != ids.length ) { throw new RuntimeException( "Wrong codes/ids sizes" ); } ValueMetaInterface valueMetaString = new ValueMetaString( "f" ); for ( int i = 0; i < codes.length; i++ ) { injector.setProperty( meta, propertyName, setValue( valueMetaString, codes[i] ), "f" ); assertEquals( ids[i], getter.get() ); } skipPropertyTest( propertyName ); }
protected void setup( T meta ) { KettleLogStore.init(); this.meta = meta; info = new BeanInjectionInfo( meta.getClass() ); injector = new BeanInjector( info ); nonTestedProperties = new HashSet<>( info.getProperties().keySet() ); }
/** * Check long property. */ protected void check( String propertyName, LongGetter getter ) throws KettleException { ValueMetaInterface valueMetaString = new ValueMetaString( "f" ); injector.setProperty( meta, propertyName, setValue( valueMetaString, "1" ), "f" ); assertEquals( 1, getter.get() ); injector.setProperty( meta, propertyName, setValue( valueMetaString, "45" ), "f" ); assertEquals( 45, getter.get() ); ValueMetaInterface valueMetaInteger = new ValueMetaInteger( "f" ); injector.setProperty( meta, propertyName, setValue( valueMetaInteger, 1234L ), "f" ); assertEquals( 1234, getter.get() ); injector.setProperty( meta, propertyName, setValue( valueMetaInteger, Long.MAX_VALUE ), "f" ); assertEquals( Long.MAX_VALUE, getter.get() ); skipPropertyTest( propertyName ); }
/** * Check boolean property. */ protected void check( String propertyName, BooleanGetter getter ) throws KettleException { ValueMetaInterface valueMetaString = new ValueMetaString( "f" ); injector.setProperty( meta, propertyName, setValue( valueMetaString, "Y" ), "f" ); assertEquals( true, getter.get() ); injector.setProperty( meta, propertyName, setValue( valueMetaString, "N" ), "f" ); assertEquals( false, getter.get() ); ValueMetaInterface valueMetaBoolean = new ValueMetaBoolean( "f" ); injector.setProperty( meta, propertyName, setValue( valueMetaBoolean, true ), "f" ); assertEquals( true, getter.get() ); injector.setProperty( meta, propertyName, setValue( valueMetaBoolean, false ), "f" ); assertEquals( false, getter.get() ); skipPropertyTest( propertyName ); }
/** * Check int property. */ protected void check( String propertyName, IntGetter getter ) throws KettleException { ValueMetaInterface valueMetaString = new ValueMetaString( "f" ); injector.setProperty( meta, propertyName, setValue( valueMetaString, "1" ), "f" ); assertEquals( 1, getter.get() ); injector.setProperty( meta, propertyName, setValue( valueMetaString, "45" ), "f" ); assertEquals( 45, getter.get() ); ValueMetaInterface valueMetaInteger = new ValueMetaInteger( "f" ); injector.setProperty( meta, propertyName, setValue( valueMetaInteger, 1234L ), "f" ); assertEquals( 1234, getter.get() ); injector.setProperty( meta, propertyName, setValue( valueMetaInteger, (long) Integer.MAX_VALUE ), "f" ); assertEquals( Integer.MAX_VALUE, getter.get() ); skipPropertyTest( propertyName ); }
@Test public void testInjectionSets() throws Exception { MetaBeanLevel1 obj = new MetaBeanLevel1(); RowMeta meta = new RowMeta(); meta.addValueMeta( new ValueMetaString( "f1" ) ); meta.addValueMeta( new ValueMetaString( "f2" ) ); meta.addValueMeta( new ValueMetaString( "fstrint" ) ); meta.addValueMeta( new ValueMetaString( "fstrlong" ) ); meta.addValueMeta( new ValueMetaString( "fstrboolean" ) ); // TODO STLOCALE List<RowMetaAndData> rows = new ArrayList<>(); rows.add( new RowMetaAndData( meta, "<sep>", "/tmp/file.txt", "123", "1234567891213", "y" ) ); rows.add( new RowMetaAndData( meta, "<sep>", "/tmp/file2.txt", "123", "1234567891213", "y" ) ); BeanInjector inj = buildBeanInjectorFor( MetaBeanLevel1.class ); inj.setProperty( obj, "SEPARATOR", rows, "f1" ); inj.setProperty( obj, "FILENAME", rows, "f2" ); inj.setProperty( obj, "FILENAME_ARRAY", rows, "f2" ); inj.setProperty( obj, "FBOOLEAN", rows, "fstrboolean" ); inj.setProperty( obj, "FINT", rows, "fstrint" ); inj.setProperty( obj, "FLONG", rows, "fstrlong" ); inj.setProperty( obj, "FIRST", rows, "fstrint" ); assertEquals( "<sep>", obj.getSub().getSeparator() ); assertEquals( "/tmp/file.txt", obj.getSub().getFiles()[0].getName() ); assertTrue( obj.fboolean ); assertEquals( 123, obj.fint ); assertEquals( 1234567891213L, obj.flong ); assertEquals( "123", obj.getSub().first() ); assertArrayEquals( new String[] { "/tmp/file.txt", "/tmp/file2.txt" }, obj.getSub().getFilenames() ); }
@Test public void testInjectionConstant() throws Exception { MetaBeanLevel1 obj = new MetaBeanLevel1(); BeanInjector inj = buildBeanInjectorFor( MetaBeanLevel1.class ); inj.setProperty( obj, "SEPARATOR", null, "<sep>" ); inj.setProperty( obj, "FINT", null, "123" ); inj.setProperty( obj, "FLONG", null, "1234567891213" ); inj.setProperty( obj, "FBOOLEAN", null, "true" ); inj.setProperty( obj, "FILENAME", null, "f1" ); inj.setProperty( obj, "FILENAME_ARRAY", null, "f2" ); assertEquals( "<sep>", obj.getSub().getSeparator() ); assertTrue( obj.fboolean ); assertEquals( 123, obj.fint ); assertEquals( 1234567891213L, obj.flong ); assertNull( obj.getSub().getFiles() ); assertNull( obj.getSub().getFilenames() ); obj.getSub().files = new MetaBeanLevel3[] { new MetaBeanLevel3(), new MetaBeanLevel3() }; obj.getSub().filenames = new String[] { "", "", "" }; inj.setProperty( obj, "FILENAME", null, "f1" ); inj.setProperty( obj, "FILENAME_ARRAY", null, "f2" ); assertEquals( 2, obj.getSub().getFiles().length ); assertEquals( "f1", obj.getSub().getFiles()[0].getName() ); assertEquals( "f1", obj.getSub().getFiles()[1].getName() ); assertArrayEquals( new String[] { "f2", "f2", "f2" }, obj.getSub().getFilenames() ); }
/** * Check string property. */ protected void check( String propertyName, StringGetter getter, String... values ) throws KettleException { ValueMetaInterface valueMeta = new ValueMetaString( "f" ); if ( values.length == 0 ) { values = new String[] { "v", "v2", null }; } String correctValue = null; for ( String v : values ) { injector.setProperty( meta, propertyName, setValue( valueMeta, v ), "f" ); if ( v != null ) { // only not-null values injected correctValue = v; } assertEquals( correctValue, getter.get() ); } skipPropertyTest( propertyName ); }