/** * Copy from the copy method in StructUtil. Did not want to drag that code in. maybe this actually * should go to struct. * * @param from * @param to * @param excludes * @return * @throws Exception */ public static <T extends struct> T xcopy(struct from, T to, String... excludes) throws Exception { Arrays.sort(excludes); for (Field f : from.fields()) { if (Arrays.binarySearch(excludes, f.getName()) >= 0) continue; Object o = f.get(from); if (o == null) continue; Field tof = to.getField(f.getName()); if (tof != null) try { tof.set(to, Converter.cnv(tof.getGenericType(), o)); } catch (Exception e) { System.out.println( "Failed to convert " + f.getName() + " from " + from.getClass() + " to " + to.getClass() + " value " + o + " exception " + e); } } return to; }
public static final void main(String[] args) throws Exception { // Generate a PBE key SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE"); SecretKey skey = skFac.generateSecret(new PBEKeySpec("test123".toCharArray())); // Initialize the PBE cipher Cipher cipher = Cipher.getInstance(ALGO); cipher.init(Cipher.ENCRYPT_MODE, skey); // Permit access to the Cipher.spi field (a CipherSpi object) Field spi = Cipher.class.getDeclaredField("spi"); spi.setAccessible(true); Object value = spi.get(cipher); // Permit access to the CipherSpi.engineGetKeySize method Method engineGetKeySize = PKCS12PBECipherCore$PBEWithSHA1AndDESede.class.getDeclaredMethod( "engineGetKeySize", Key.class); engineGetKeySize.setAccessible(true); // Check the key size int keySize = (int) engineGetKeySize.invoke(value, skey); if (keySize == KEYSIZE) { System.out.println(ALGO + ".engineGetKeySize returns " + keySize + " bits, as expected"); System.out.println("OK"); } else { throw new Exception("ERROR: " + ALGO + " key size is incorrect"); } }
public static Object getAdapterSelf(Class<?> adapterClass, Object adapter) throws NoSuchFieldException, IllegalAccessException { Field self = adapterClass.getDeclaredField("self"); return self.get(adapter); }