private Object getField(Object object, String name) throws NoSuchFieldException { if (object == null) throw new IllegalArgumentException("Invalid null object argument"); for (Class cls = object.getClass(); cls != null; cls = cls.getSuperclass()) { try { Field field = cls.getDeclaredField(name); field.setAccessible(true); return field.get(object); } catch (Exception ex) { // in case of an exception, we will throw a new // NoSuchFieldException object below ; } } // Arrived here because we couldn't find the field or an exception // was thrown during field access. throw new NoSuchFieldException("Could get value for field " + object.getClass().getName() + "." + name); }
private void verifyArrayFieldTypeAnnotations(Class c) throws NoSuchFieldException, NoSuchMethodException { Annotation anno; AnnotatedType at; at = c.getDeclaredField("typeAnnotatedArray").getAnnotatedType(); anno = at.getAnnotations()[0]; verifyTestAnn(arrayTA[0], anno, "array1"); arrayTA[0] = anno; for (int i = 1; i <= 3; i++) { at = ((AnnotatedArrayType) at).getAnnotatedGenericComponentType(); anno = at.getAnnotations()[0]; verifyTestAnn(arrayTA[i], anno, "array" + (i + 1)); arrayTA[i] = anno; } }
private void verifyMethodTypeAnnotations(Class c) throws NoSuchFieldException, NoSuchMethodException { Annotation anno; Executable typeAnnotatedMethod = c.getDeclaredMethod("typeAnnotatedMethod", TypeAnnotatedTestClass.class); anno = typeAnnotatedMethod.getAnnotatedReturnType().getAnnotations()[0]; verifyTestAnn(returnTA, anno, "return"); returnTA = anno; anno = typeAnnotatedMethod.getTypeParameters()[0].getAnnotations()[0]; verifyTestAnn(methodTypeParameterTA, anno, "methodTypeParameter"); methodTypeParameterTA = anno; anno = typeAnnotatedMethod.getAnnotatedParameterTypes()[0].getAnnotations()[0]; verifyTestAnn(formalParameterTA, anno, "formalParameter"); formalParameterTA = anno; anno = typeAnnotatedMethod.getAnnotatedExceptionTypes()[0].getAnnotations()[0]; verifyTestAnn(throwsTA, anno, "throws"); throwsTA = anno; }
private static int getFaviconId(String name) { try { Class<?> drawablesClass = R.raw.class; // Look for a favicon with the id R.raw.bookmarkdefaults_favicon_*. Field faviconField = drawablesClass.getField(name.replace("_title_", "_favicon_")); faviconField.setAccessible(true); return faviconField.getInt(null); } catch (IllegalAccessException | NoSuchFieldException e) { // We'll end up here for any default bookmark that doesn't have a favicon in // resources/raw/ (i.e., about:firefox). When this happens, the Favicons service will // fall back to the default branding icon for about pages. Non-about pages should always // specify an icon; otherwise, the placeholder globe favicon will be used. Log.d(LOGTAG, "No raw favicon resource found for " + name); } Log.e(LOGTAG, "Failed to find favicon resource ID for " + name); return FAVICON_ID_NOT_FOUND; }
private void verifyInnerFieldTypeAnnotations(Class c) throws NoSuchFieldException, NoSuchMethodException { AnnotatedType at = c.getDeclaredField("typeAnnotatedInner").getAnnotatedType(); Annotation anno = at.getAnnotations()[0]; verifyTestAnn(innerTA, anno, "inner"); innerTA = anno; }
public static void main(String argv[]) throws NoSuchFieldException, NoSuchMethodException { if (argv.length == 1 && argv[0].equals("buildagent")) { buildAgent(); return; } if (inst == null) { throw new RuntimeException("Instrumentation object was null"); } RedefineAnnotations test = new RedefineAnnotations(); test.testTransformAndVerify(); }
private void testTransformAndVerify() throws NoSuchFieldException, NoSuchMethodException { Class<TypeAnnotatedTestClass> c = TypeAnnotatedTestClass.class; Class<?> myClass = c; /* * Verify that the expected annotations are where they should be before transform. */ verifyClassTypeAnnotations(c); verifyFieldTypeAnnotations(c); verifyMethodTypeAnnotations(c); try { inst.addTransformer(new Transformer(), true); inst.retransformClasses(myClass); } catch (UnmodifiableClassException e) { throw new RuntimeException(e); } /* * Verify that the expected annotations are where they should be after transform. * Also verify that before and after are equal. */ verifyClassTypeAnnotations(c); verifyFieldTypeAnnotations(c); verifyMethodTypeAnnotations(c); }
private void verifyFieldTypeAnnotations(Class c) throws NoSuchFieldException, NoSuchMethodException { verifyBasicFieldTypeAnnotations(c); verifyInnerFieldTypeAnnotations(c); verifyArrayFieldTypeAnnotations(c); verifyMapFieldTypeAnnotations(c); }
private void verifyBasicFieldTypeAnnotations(Class c) throws NoSuchFieldException, NoSuchMethodException { Annotation anno = c.getDeclaredField("typeAnnotatedBoolean").getAnnotatedType().getAnnotations()[0]; verifyTestAnn(fieldTA, anno, "field"); fieldTA = anno; }
private void verifyMapFieldTypeAnnotations(Class c) throws NoSuchFieldException, NoSuchMethodException { Annotation anno; AnnotatedType atBase; AnnotatedType atParameter; atBase = c.getDeclaredField("typeAnnotatedMap").getAnnotatedType(); anno = atBase.getAnnotations()[0]; verifyTestAnn(mapTA[0], anno, "map1"); mapTA[0] = anno; atParameter = ((AnnotatedParameterizedType) atBase). getAnnotatedActualTypeArguments()[0]; anno = ((AnnotatedWildcardType) atParameter).getAnnotations()[0]; verifyTestAnn(mapTA[1], anno, "map2"); mapTA[1] = anno; anno = ((AnnotatedWildcardType) atParameter). getAnnotatedUpperBounds()[0].getAnnotations()[0]; verifyTestAnn(mapTA[2], anno, "map3"); mapTA[2] = anno; atParameter = ((AnnotatedParameterizedType) atBase). getAnnotatedActualTypeArguments()[1]; anno = ((AnnotatedParameterizedType) atParameter).getAnnotations()[0]; verifyTestAnn(mapTA[3], anno, "map4"); mapTA[3] = anno; anno = ((AnnotatedParameterizedType) atParameter). getAnnotatedActualTypeArguments()[0].getAnnotations()[0]; verifyTestAnn(mapTA[4], anno, "map5"); mapTA[4] = anno; }
public static void run(String baseURL, String resumptionToken, OutputStream out) throws IOException, ParserConfigurationException, SAXException, TransformerException, NoSuchFieldException { ListRecords listRecords = new ListRecords(baseURL, resumptionToken); while (listRecords != null) { NodeList errors = listRecords.getErrors(); if (errors != null && errors.getLength() > 0) { System.out.println("Found errors"); int length = errors.getLength(); for (int i=0; i<length; ++i) { Node item = errors.item(i); System.out.println(item); } System.out.println("Error record: " + listRecords.toString()); break; } // System.out.println(listRecords); out.write(listRecords.toString().getBytes("UTF-8")); out.write("\n".getBytes("UTF-8")); resumptionToken = listRecords.getResumptionToken(); System.out.println("resumptionToken: " + resumptionToken); if (resumptionToken == null || resumptionToken.length() == 0) { listRecords = null; } else { listRecords = new ListRecords(baseURL, resumptionToken); } } out.write("</harvest>\n".getBytes("UTF-8")); }
public static void run(String baseURL, String from, String until, String metadataPrefix, String setSpec, OutputStream out) throws IOException, ParserConfigurationException, SAXException, TransformerException, NoSuchFieldException { out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes("UTF-8")); out.write("<harvest>\n".getBytes("UTF-8")); out.write(new Identify(baseURL).toString().getBytes("UTF-8")); out.write("\n".getBytes("UTF-8")); out.write(new ListMetadataFormats(baseURL).toString().getBytes("UTF-8")); out.write("\n".getBytes("UTF-8")); out.write(new ListSets(baseURL).toString().getBytes("UTF-8")); out.write("\n".getBytes("UTF-8")); ListRecords listRecords = new ListRecords(baseURL, from, until, setSpec, metadataPrefix); while (listRecords != null) { NodeList errors = listRecords.getErrors(); if (errors != null && errors.getLength() > 0) { System.out.println("Found errors"); int length = errors.getLength(); for (int i=0; i<length; ++i) { Node item = errors.item(i); System.out.println(item); } System.out.println("Error record: " + listRecords.toString()); break; } // System.out.println(listRecords); out.write(listRecords.toString().getBytes("UTF-8")); out.write("\n".getBytes("UTF-8")); String resumptionToken = listRecords.getResumptionToken(); System.out.println("resumptionToken: " + resumptionToken); if (resumptionToken == null || resumptionToken.length() == 0) { listRecords = null; } else { listRecords = new ListRecords(baseURL, resumptionToken); } } out.write("</harvest>\n".getBytes("UTF-8")); }