@Test public void testAddAnnotation_singleAnnotationWithNoise() throws Exception { Collection<String> annotations = Collections.singletonList(ANNOTATIONS[0]); inputStreamClass = getInputStream(ClassToAnnotateWithNoise.class); annotator.addAnnotations(inputStreamClass, outputClassFile, annotations); assertClassHasAnnotations(outputClassFile, annotations); }
public boolean addAnnotations(InputStream inputClass, File outputClassFile, Collection<String> annotations) throws Exception { boolean classIsModified = false; FileOutputStream outputClass = null; ClassWriter cw = new ClassWriter(false); try { ClassReader cr = new ClassReader(inputClass); List<String> annotationTypeDescs = new ArrayList<String>(2); if (annotations.contains("SharedStatics")) { annotationTypeDescs.add(SHAREDSTATICS_TYPE_DESC); } if (annotations.contains("MagicPermission")) { annotationTypeDescs.add(MAGICPERMISSION_TYPE_DESC); } MarkerClassVisitor mcv = new MarkerClassVisitor(cw, annotationTypeDescs); cr.accept(mcv, Attributes.getDefaultAttributes(), true); if (mcv.classIsModified()) { System.out.println("adding annotations " + annotations + " to file " + outputClassFile.getName()); classIsModified = true; outputClass = new FileOutputStream(outputClassFile); byte[] b = cw.toByteArray(); outputClass.write(b); } } finally { if (outputClass != null) { try { outputClass.close(); } catch (IOException e) { System.err.println("Can't close stream for file " + outputClassFile.getName()); } } } return classIsModified; }
Annotator { public boolean addAnnotations(InputStream inputClass, File outputClassFile, Collection<String> annotations) throws Exception { boolean classIsModified = false; FileOutputStream outputClass = null; ClassWriter cw = new ClassWriter(false); try { ClassReader cr = new ClassReader(inputClass); List<String> annotationTypeDescs = new ArrayList<String>(2); if (annotations.contains("SharedStatics")) { annotationTypeDescs.add(SHAREDSTATICS_TYPE_DESC); } if (annotations.contains("MagicPermission")) { annotationTypeDescs.add(MAGICPERMISSION_TYPE_DESC); } MarkerClassVisitor mcv = new MarkerClassVisitor(cw, annotationTypeDescs); cr.accept(mcv, Attributes.getDefaultAttributes(), true); if (mcv.classIsModified()) { System.out.println("adding annotations " + annotations + " to file " + outputClassFile.getName()); classIsModified = true; outputClass = new FileOutputStream(outputClassFile); byte[] b = cw.toByteArray(); outputClass.write(b); } } finally { if (outputClass != null) { try { outputClass.close(); } catch (IOException e) { System.err.println("Can't close stream for file " + outputClassFile.getName()); } } } return classIsModified; } }
Annotator { public boolean addAnnotations(InputStream inputClass, File outputClassFile, Collection<String> annotations) throws Exception { boolean classIsModified = false; FileOutputStream outputClass = null; ClassWriter cw = new ClassWriter(false); try { ClassReader cr = new ClassReader(inputClass); List<String> annotationTypeDescs = new ArrayList<String>(2); if (annotations.contains("SharedStatics")) { annotationTypeDescs.add(SHAREDSTATICS_TYPE_DESC); } if (annotations.contains("MagicPermission")) { annotationTypeDescs.add(MAGICPERMISSION_TYPE_DESC); } MarkerClassVisitor mcv = new MarkerClassVisitor(cw, annotationTypeDescs); cr.accept(mcv, Attributes.getDefaultAttributes(), true); if (mcv.classIsModified()) { System.out.println("adding annotations " + annotations + " to file " + outputClassFile.getName()); classIsModified = true; outputClass = new FileOutputStream(outputClassFile); byte[] b = cw.toByteArray(); outputClass.write(b); } } finally { if (outputClass != null) { try { outputClass.close(); } catch (IOException e) { System.err.println("Can't close stream for file " + outputClassFile.getName()); } } } return classIsModified; }  }
Annotator { public boolean addAnnotations(InputStream inputClass, File outputClassFile, Collection<String> annotations) throws Exception { boolean classIsModified = false; FileOutputStream outputClass = null; ClassWriter cw = new ClassWriter(false); try { ClassReader cr = new ClassReader(inputClass); List<String> annotationTypeDescs = new ArrayList<String>(2); if (annotations.contains("SharedStatics")) { annotationTypeDescs.add(SHAREDSTATICS_TYPE_DESC); } if (annotations.contains("MagicPermission")) { annotationTypeDescs.add(MAGICPERMISSION_TYPE_DESC); } MarkerClassVisitor mcv = new MarkerClassVisitor(cw, annotationTypeDescs); cr.accept(mcv, Attributes.getDefaultAttributes(), true); if (mcv.classIsModified()) { System.out.println("adding annotations " + annotations + " to file " + outputClassFile.getName()); classIsModified = true; outputClass = new FileOutputStream(outputClassFile); byte[] b = cw.toByteArray(); outputClass.write(b); } } finally { if (outputClass != null) { try { outputClass.close(); } catch (IOException e) { System.err.println("Can't close stream for file " + outputClassFile.getName()); } } } return classIsModified; }  boolean addAnnotations(InputStream inputClass, File outputClassFile, Collection<String> annotations); }
Annotator { public boolean addAnnotations(InputStream inputClass, File outputClassFile, Collection<String> annotations) throws Exception { boolean classIsModified = false; FileOutputStream outputClass = null; ClassWriter cw = new ClassWriter(false); try { ClassReader cr = new ClassReader(inputClass); List<String> annotationTypeDescs = new ArrayList<String>(2); if (annotations.contains("SharedStatics")) { annotationTypeDescs.add(SHAREDSTATICS_TYPE_DESC); } if (annotations.contains("MagicPermission")) { annotationTypeDescs.add(MAGICPERMISSION_TYPE_DESC); } MarkerClassVisitor mcv = new MarkerClassVisitor(cw, annotationTypeDescs); cr.accept(mcv, Attributes.getDefaultAttributes(), true); if (mcv.classIsModified()) { System.out.println("adding annotations " + annotations + " to file " + outputClassFile.getName()); classIsModified = true; outputClass = new FileOutputStream(outputClassFile); byte[] b = cw.toByteArray(); outputClass.write(b); } } finally { if (outputClass != null) { try { outputClass.close(); } catch (IOException e) { System.err.println("Can't close stream for file " + outputClassFile.getName()); } } } return classIsModified; }  boolean addAnnotations(InputStream inputClass, File outputClassFile, Collection<String> annotations);  }