build/annotationProcessors/utils/GeneratableElementIterator.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 package org.mozilla.gecko.annotationProcessors.utils;
michael@0 6
michael@0 7 import org.mozilla.gecko.annotationProcessors.AnnotationInfo;
michael@0 8 import org.mozilla.gecko.annotationProcessors.classloader.AnnotatableEntity;
michael@0 9
michael@0 10 import java.lang.annotation.Annotation;
michael@0 11 import java.lang.reflect.AnnotatedElement;
michael@0 12 import java.lang.reflect.InvocationTargetException;
michael@0 13 import java.lang.reflect.Member;
michael@0 14 import java.lang.reflect.Method;
michael@0 15 import java.util.Arrays;
michael@0 16 import java.util.Iterator;
michael@0 17
michael@0 18 /**
michael@0 19 * Iterator over the methods in a given method list which have the WrappedJNIMethod
michael@0 20 * annotation. Returns an object containing both the annotation (Which may contain interesting
michael@0 21 * parameters) and the argument.
michael@0 22 */
michael@0 23 public class GeneratableElementIterator implements Iterator<AnnotatableEntity> {
michael@0 24 private final Member[] mObjects;
michael@0 25 private AnnotatableEntity mNextReturnValue;
michael@0 26 private int mElementIndex;
michael@0 27
michael@0 28 private boolean mIterateEveryEntry;
michael@0 29
michael@0 30 public GeneratableElementIterator(Class<?> aClass) {
michael@0 31 // Get all the elements of this class as AccessibleObjects.
michael@0 32 Member[] aMethods = aClass.getDeclaredMethods();
michael@0 33 Member[] aFields = aClass.getDeclaredFields();
michael@0 34 Member[] aCtors = aClass.getConstructors();
michael@0 35
michael@0 36 // Shove them all into one buffer.
michael@0 37 Member[] objs = new Member[aMethods.length + aFields.length + aCtors.length];
michael@0 38
michael@0 39 int offset = 0;
michael@0 40 System.arraycopy(aMethods, 0, objs, 0, aMethods.length);
michael@0 41 offset += aMethods.length;
michael@0 42 System.arraycopy(aFields, 0, objs, offset, aFields.length);
michael@0 43 offset += aFields.length;
michael@0 44 System.arraycopy(aCtors, 0, objs, offset, aCtors.length);
michael@0 45
michael@0 46 // Sort the elements to ensure determinism.
michael@0 47 Arrays.sort(objs, new AlphabeticAnnotatableEntityComparator());
michael@0 48 mObjects = objs;
michael@0 49
michael@0 50 // Check for "Wrap ALL the things" flag.
michael@0 51 for (Annotation annotation : aClass.getDeclaredAnnotations()) {
michael@0 52 final String annotationTypeName = annotation.annotationType().getName();
michael@0 53 if (annotationTypeName.equals("org.mozilla.gecko.mozglue.generatorannotations.WrapEntireClassForJNI")) {
michael@0 54 mIterateEveryEntry = true;
michael@0 55 break;
michael@0 56 }
michael@0 57 }
michael@0 58
michael@0 59 findNextValue();
michael@0 60 }
michael@0 61
michael@0 62 /**
michael@0 63 * Find and cache the next appropriately annotated method, plus the annotation parameter, if
michael@0 64 * one exists. Otherwise cache null, so hasNext returns false.
michael@0 65 */
michael@0 66 private void findNextValue() {
michael@0 67 while (mElementIndex < mObjects.length) {
michael@0 68 Member candidateElement = mObjects[mElementIndex];
michael@0 69 mElementIndex++;
michael@0 70 for (Annotation annotation : ((AnnotatedElement) candidateElement).getDeclaredAnnotations()) {
michael@0 71 // WrappedJNIMethod has parameters. Use Reflection to obtain them.
michael@0 72 Class<? extends Annotation> annotationType = annotation.annotationType();
michael@0 73 final String annotationTypeName = annotationType.getName();
michael@0 74 if (annotationTypeName.equals("org.mozilla.gecko.mozglue.generatorannotations.WrapElementForJNI")) {
michael@0 75 String stubName = null;
michael@0 76 boolean isStaticStub = false;
michael@0 77 boolean isMultithreadedStub = false;
michael@0 78 boolean noThrow = false;
michael@0 79 try {
michael@0 80 // Determine the explicitly-given name of the stub to generate, if any.
michael@0 81 final Method stubNameMethod = annotationType.getDeclaredMethod("stubName");
michael@0 82 stubNameMethod.setAccessible(true);
michael@0 83 stubName = (String) stubNameMethod.invoke(annotation);
michael@0 84
michael@0 85 // Detemine if the generated stub should be static.
michael@0 86 final Method staticStubMethod = annotationType.getDeclaredMethod("generateStatic");
michael@0 87 staticStubMethod.setAccessible(true);
michael@0 88 isStaticStub = (Boolean) staticStubMethod.invoke(annotation);
michael@0 89
michael@0 90 // Determine if the generated stub is to allow calls from multiple threads.
michael@0 91 final Method multithreadedStubMethod = annotationType.getDeclaredMethod("allowMultithread");
michael@0 92 multithreadedStubMethod.setAccessible(true);
michael@0 93 isMultithreadedStub = (Boolean) multithreadedStubMethod.invoke(annotation);
michael@0 94
michael@0 95 // Determine if ignoring exceptions
michael@0 96 final Method noThrowMethod = annotationType.getDeclaredMethod("noThrow");
michael@0 97 noThrowMethod.setAccessible(true);
michael@0 98 noThrow = (Boolean) noThrowMethod.invoke(annotation);
michael@0 99
michael@0 100 } catch (NoSuchMethodException e) {
michael@0 101 System.err.println("Unable to find expected field on WrapElementForJNI annotation. Did the signature change?");
michael@0 102 e.printStackTrace(System.err);
michael@0 103 System.exit(3);
michael@0 104 } catch (IllegalAccessException e) {
michael@0 105 System.err.println("IllegalAccessException reading fields on WrapElementForJNI annotation. Seems the semantics of Reflection have changed...");
michael@0 106 e.printStackTrace(System.err);
michael@0 107 System.exit(4);
michael@0 108 } catch (InvocationTargetException e) {
michael@0 109 System.err.println("InvocationTargetException reading fields on WrapElementForJNI annotation. This really shouldn't happen.");
michael@0 110 e.printStackTrace(System.err);
michael@0 111 System.exit(5);
michael@0 112 }
michael@0 113
michael@0 114 // If the method name was not explicitly given in the annotation generate one...
michael@0 115 if (stubName.isEmpty()) {
michael@0 116 String aMethodName = candidateElement.getName();
michael@0 117 stubName = aMethodName.substring(0, 1).toUpperCase() + aMethodName.substring(1);
michael@0 118 }
michael@0 119
michael@0 120 AnnotationInfo annotationInfo = new AnnotationInfo(
michael@0 121 stubName, isStaticStub, isMultithreadedStub, noThrow);
michael@0 122 mNextReturnValue = new AnnotatableEntity(candidateElement, annotationInfo);
michael@0 123 return;
michael@0 124 }
michael@0 125 }
michael@0 126
michael@0 127 // If no annotation found, we might be expected to generate anyway using default arguments,
michael@0 128 // thanks to the "Generate everything" annotation.
michael@0 129 if (mIterateEveryEntry) {
michael@0 130 AnnotationInfo annotationInfo = new AnnotationInfo(
michael@0 131 candidateElement.getName(), false, false, false);
michael@0 132 mNextReturnValue = new AnnotatableEntity(candidateElement, annotationInfo);
michael@0 133 return;
michael@0 134 }
michael@0 135 }
michael@0 136 mNextReturnValue = null;
michael@0 137 }
michael@0 138
michael@0 139 @Override
michael@0 140 public boolean hasNext() {
michael@0 141 return mNextReturnValue != null;
michael@0 142 }
michael@0 143
michael@0 144 @Override
michael@0 145 public AnnotatableEntity next() {
michael@0 146 AnnotatableEntity ret = mNextReturnValue;
michael@0 147 findNextValue();
michael@0 148 return ret;
michael@0 149 }
michael@0 150
michael@0 151 @Override
michael@0 152 public void remove() {
michael@0 153 throw new UnsupportedOperationException("Removal of methods from GeneratableElementIterator not supported.");
michael@0 154 }
michael@0 155 }

mercurial