Java Code Examples for sun.reflect.ConstantPool

The following examples show how to use sun.reflect.ConstantPool. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Build an AnnotatedType from the parameters supplied.
 *
 * This method and {@code buildAnnotatedTypes} are probably
 * the entry points you are looking for.
 *
 * @param rawAnnotations the byte[] encoding of all type annotations on this declaration
 * @param cp the ConstantPool needed to parse the embedded Annotation
 * @param decl the declaration this type annotation is on
 * @param container the Class this type annotation is on (may be the same as decl)
 * @param type the type the AnnotatedType corresponds to
 * @param filter the type annotation targets included in this AnnotatedType
 */
public static AnnotatedType buildAnnotatedType(byte[] rawAnnotations,
        ConstantPool cp,
        AnnotatedElement decl,
        Class<?> container,
        Type type,
        TypeAnnotationTarget filter) {
    TypeAnnotation[] tas = parseTypeAnnotations(rawAnnotations,
                                                cp,
                                                decl,
                                                container);
    List<TypeAnnotation> l = new ArrayList<>(tas.length);
    for (TypeAnnotation t : tas) {
        TypeAnnotationTargetInfo ti = t.getTargetInfo();
        if (ti.getTarget() == filter)
            l.add(t);
    }
    TypeAnnotation[] typeAnnotations = l.toArray(EMPTY_TYPE_ANNOTATION_ARRAY);
    return AnnotatedTypeFactory.buildAnnotatedType(type,
                                                   LocationInfo.BASE_LOCATION,
                                                   typeAnnotations,
                                                   typeAnnotations,
                                                   decl);
}
 
Example 2
Source Project: hottub   Source File: TypeAnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static TypeAnnotation parseTypeAnnotation(ByteBuffer buf,
        ConstantPool cp,
        AnnotatedElement baseDecl,
        Class<?> container) {
    try {
        TypeAnnotationTargetInfo ti = parseTargetInfo(buf);
        LocationInfo locationInfo = LocationInfo.parseLocationInfo(buf);
        Annotation a = AnnotationParser.parseAnnotation(buf, cp, container, false);
        if (ti == null) // Inside a method for example
            return null;
        return new TypeAnnotation(ti, locationInfo, a, baseDecl);
    } catch (IllegalArgumentException | // Bad type in const pool at specified index
            BufferUnderflowException e) {
        throw new AnnotationFormatError(e);
    }
}
 
Example 3
private static TypeAnnotation[] parseTypeAnnotations(byte[] rawAnnotations,
        ConstantPool cp,
        AnnotatedElement baseDecl,
        Class<?> container) {
    if (rawAnnotations == null)
        return EMPTY_TYPE_ANNOTATION_ARRAY;

    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int annotationCount = buf.getShort() & 0xFFFF;
    List<TypeAnnotation> typeAnnotations = new ArrayList<>(annotationCount);

    // Parse each TypeAnnotation
    for (int i = 0; i < annotationCount; i++) {
         TypeAnnotation ta = parseTypeAnnotation(buf, cp, baseDecl, container);
         if (ta != null)
             typeAnnotations.add(ta);
    }

    return typeAnnotations.toArray(EMPTY_TYPE_ANNOTATION_ARRAY);
}
 
Example 4
Source Project: openjdk-8   Source File: AnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Object parseIntArray(int length,
                             ByteBuffer buf, ConstantPool constPool) {
    int[] result = new  int[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'I') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 5
private static Annotation[][] parseParameterAnnotations2(
                byte[] rawAnnotations,
                ConstantPool constPool,
                Class<?> container) {
    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int numParameters = buf.get() & 0xFF;
    Annotation[][] result = new Annotation[numParameters][];

    for (int i = 0; i < numParameters; i++) {
        int numAnnotations = buf.getShort() & 0xFFFF;
        List<Annotation> annotations =
            new ArrayList<Annotation>(numAnnotations);
        for (int j = 0; j < numAnnotations; j++) {
            Annotation a = parseAnnotation(buf, constPool, container, false);
            if (a != null) {
                AnnotationType type = AnnotationType.getInstance(
                                          a.annotationType());
                if (type.retention() == RetentionPolicy.RUNTIME)
                    annotations.add(a);
            }
        }
        result[i] = annotations.toArray(EMPTY_ANNOTATIONS_ARRAY);
    }
    return result;
}
 
Example 6
private static Object parseStringArray(int length,
                                ByteBuffer buf,  ConstantPool constPool) {
    String[] result = new String[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 's') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = constPool.getUTF8At(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 7
private static Object parseAnnotationArray(int length,
                                           Class<? extends Annotation> annotationType,
                                           ByteBuffer buf,
                                           ConstantPool constPool,
                                           Class<?> container) {
    Object[] result = (Object[]) Array.newInstance(annotationType, length);
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == '@') {
            result[i] = parseAnnotation(buf, constPool, container, true);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 8
private static Object parseLongArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    long[] result = new long[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'J') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = constPool.getLongAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 9
private static Object parseDoubleArray(int length,
                                ByteBuffer buf, ConstantPool constPool) {
    double[] result = new  double[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'D') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = constPool.getDoubleAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 10
Source Project: hottub   Source File: TypeAnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static TypeAnnotation[] parseTypeAnnotations(byte[] rawAnnotations,
        ConstantPool cp,
        AnnotatedElement baseDecl,
        Class<?> container) {
    if (rawAnnotations == null)
        return EMPTY_TYPE_ANNOTATION_ARRAY;

    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int annotationCount = buf.getShort() & 0xFFFF;
    List<TypeAnnotation> typeAnnotations = new ArrayList<>(annotationCount);

    // Parse each TypeAnnotation
    for (int i = 0; i < annotationCount; i++) {
         TypeAnnotation ta = parseTypeAnnotation(buf, cp, baseDecl, container);
         if (ta != null)
             typeAnnotations.add(ta);
    }

    return typeAnnotations.toArray(EMPTY_TYPE_ANNOTATION_ARRAY);
}
 
Example 11
/**
 * Build an array of AnnotatedTypes for the class decl's implemented
 * interfaces.
 *
 * @param rawAnnotations the byte[] encoding of all type annotations on this declaration
 * @param cp the ConstantPool needed to parse the embedded Annotation
 * @param decl the Class whose annotated implemented interfaces is being built
 */
public static AnnotatedType[] buildAnnotatedInterfaces(byte[] rawAnnotations,
        ConstantPool cp,
        Class<?> decl) {
    if (decl == Object.class ||
            decl.isArray() ||
            decl.isPrimitive() ||
            decl == Void.TYPE)
        return AnnotatedTypeFactory.EMPTY_ANNOTATED_TYPE_ARRAY;
    return buildAnnotatedTypes(rawAnnotations,
                               cp,
                               decl,
                               decl,
                               decl.getGenericInterfaces(),
                               TypeAnnotationTarget.CLASS_IMPLEMENTS);
}
 
Example 12
Source Project: jdk8u_jdk   Source File: AnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Map<Class<? extends Annotation>, Annotation> parseAnnotations2(
            byte[] rawAnnotations,
            ConstantPool constPool,
            Class<?> container,
            Class<? extends Annotation>[] selectAnnotationClasses) {
    Map<Class<? extends Annotation>, Annotation> result =
        new LinkedHashMap<Class<? extends Annotation>, Annotation>();
    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int numAnnotations = buf.getShort() & 0xFFFF;
    for (int i = 0; i < numAnnotations; i++) {
        Annotation a = parseAnnotation2(buf, constPool, container, false, selectAnnotationClasses);
        if (a != null) {
            Class<? extends Annotation> klass = a.annotationType();
            if (AnnotationType.getInstance(klass).retention() == RetentionPolicy.RUNTIME &&
                result.put(klass, a) != null) {
                    throw new AnnotationFormatError(
                        "Duplicate annotation for class: "+klass+": " + a);
        }
    }
    }
    return result;
}
 
Example 13
Source Project: hottub   Source File: AnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Object parseByteArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    byte[] result = new byte[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'B') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (byte) constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 14
Source Project: java-n-IDE-for-Android   Source File: AnnotationParser.java    License: Apache License 2.0 6 votes vote down vote up
private static Object parseLongArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    long[] result = new long[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'J') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = constPool.getLongAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 15
Source Project: jdk8u-jdk   Source File: AnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Annotation[][] parseParameterAnnotations2(
                byte[] rawAnnotations,
                ConstantPool constPool,
                Class<?> container) {
    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int numParameters = buf.get() & 0xFF;
    Annotation[][] result = new Annotation[numParameters][];

    for (int i = 0; i < numParameters; i++) {
        int numAnnotations = buf.getShort() & 0xFFFF;
        List<Annotation> annotations =
            new ArrayList<Annotation>(numAnnotations);
        for (int j = 0; j < numAnnotations; j++) {
            Annotation a = parseAnnotation(buf, constPool, container, false);
            if (a != null) {
                AnnotationType type = AnnotationType.getInstance(
                                          a.annotationType());
                if (type.retention() == RetentionPolicy.RUNTIME)
                    annotations.add(a);
            }
        }
        result[i] = annotations.toArray(EMPTY_ANNOTATIONS_ARRAY);
    }
    return result;
}
 
Example 16
private static Object parseEnumArray(int length, Class<? extends Enum<?>> enumType,
                                     ByteBuffer buf,
                                     ConstantPool constPool,
                                     Class<?> container) {
    Object[] result = (Object[]) Array.newInstance(enumType, length);
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'e') {
            result[i] = parseEnumValue(enumType, buf, constPool, container);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 17
Source Project: javaide   Source File: AnnotationParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static Map<Class, Annotation> parseAnnotations2(
            byte[] rawAnnotations,
            ConstantPool constPool,
            Class container) {
    Map<Class, Annotation> result = new LinkedHashMap<Class, Annotation>();
    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int numAnnotations = buf.getShort() & 0xFFFF;
    for (int i = 0; i < numAnnotations; i++) {
        Annotation a = parseAnnotation(buf, constPool, container, false);
        if (a != null) {
            Class klass = a.annotationType();
            AnnotationType type = AnnotationType.getInstance(klass);
            if (type.retention() == RetentionPolicy.RUNTIME)
                if (result.put(klass, a) != null)
                    throw new AnnotationFormatError(
                        "Duplicate annotation for class: "+klass+": " + a);
        }
    }
    return result;
}
 
Example 18
Source Project: jdk8u_jdk   Source File: AnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Object parseLongArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    long[] result = new long[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'J') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = constPool.getLongAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 19
Source Project: hottub   Source File: AnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Object parseCharArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    char[] result = new char[length];
    boolean typeMismatch = false;
    byte tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'C') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (char) constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 20
Source Project: javaide   Source File: AnnotationParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static Object parseIntArray(int length,
                             ByteBuffer buf, ConstantPool constPool) {
    int[] result = new  int[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'I') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 21
Source Project: openjdk-8   Source File: AnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Object parseByteArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    byte[] result = new byte[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'B') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (byte) constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 22
Source Project: jdk8u-jdk   Source File: AnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Object parseByteArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    byte[] result = new byte[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'B') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (byte) constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 23
private static Object parseCharArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    char[] result = new char[length];
    boolean typeMismatch = false;
    byte tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'C') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (char) constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 24
private static TypeAnnotation parseTypeAnnotation(ByteBuffer buf,
        ConstantPool cp,
        AnnotatedElement baseDecl,
        Class<?> container) {
    try {
        TypeAnnotationTargetInfo ti = parseTargetInfo(buf);
        LocationInfo locationInfo = LocationInfo.parseLocationInfo(buf);
        Annotation a = AnnotationParser.parseAnnotation(buf, cp, container, false);
        if (ti == null) // Inside a method for example
            return null;
        return new TypeAnnotation(ti, locationInfo, a, baseDecl);
    } catch (IllegalArgumentException | // Bad type in const pool at specified index
            BufferUnderflowException e) {
        throw new AnnotationFormatError(e);
    }
}
 
Example 25
Source Project: javaide   Source File: AnnotationParser.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses the enum constant member value at the current position in the
 * specified byte buffer, resolving constant references in the specified
 * constant pool.  The cursor of the byte buffer must point to a
 * "enum_const_value structure" as described in the
 * RuntimeVisibleAnnotations_attribute:
 *
 *       {
 *           u2   type_name_index;
 *           u2   const_name_index;
 *       } enum_const_value;
 */
private static Object parseEnumValue(Class enumType, ByteBuffer buf,
                                     ConstantPool constPool,
                                     Class container) {
    int typeNameIndex = buf.getShort() & 0xFFFF;
    String typeName  = constPool.getUTF8At(typeNameIndex);
    int constNameIndex = buf.getShort() & 0xFFFF;
    String constName = constPool.getUTF8At(constNameIndex);

    if (!typeName.endsWith(";")) {
        // support now-obsolete early jsr175-format class files.
        if (!enumType.getName().equals(typeName))
        return new AnnotationTypeMismatchExceptionProxy(
            typeName + "." + constName);
    } else if (enumType != parseSig(typeName, container)) {
        return new AnnotationTypeMismatchExceptionProxy(
            typeName + "." + constName);
    }

    try {
        return  Enum.valueOf(enumType, constName);
    } catch(IllegalArgumentException e) {
        return new EnumConstantNotPresentExceptionProxy(
            (Class<? extends Enum>)enumType, constName);
    }
}
 
Example 26
Source Project: openjdk-8   Source File: AnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Annotation[][] parseParameterAnnotations2(
                byte[] rawAnnotations,
                ConstantPool constPool,
                Class<?> container) {
    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int numParameters = buf.get() & 0xFF;
    Annotation[][] result = new Annotation[numParameters][];

    for (int i = 0; i < numParameters; i++) {
        int numAnnotations = buf.getShort() & 0xFFFF;
        List<Annotation> annotations =
            new ArrayList<Annotation>(numAnnotations);
        for (int j = 0; j < numAnnotations; j++) {
            Annotation a = parseAnnotation(buf, constPool, container, false);
            if (a != null) {
                AnnotationType type = AnnotationType.getInstance(
                                          a.annotationType());
                if (type.retention() == RetentionPolicy.RUNTIME)
                    annotations.add(a);
            }
        }
        result[i] = annotations.toArray(EMPTY_ANNOTATIONS_ARRAY);
    }
    return result;
}
 
Example 27
Source Project: jdk8u_jdk   Source File: AnnotationParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static Object parseBooleanArray(int length,
                                 ByteBuffer buf, ConstantPool constPool) {
    boolean[] result = new boolean[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'Z') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (constPool.getIntAt(index) != 0);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 28
private static Annotation[][] parseParameterAnnotations2(
                byte[] rawAnnotations,
                ConstantPool constPool,
                Class<?> container) {
    ByteBuffer buf = ByteBuffer.wrap(rawAnnotations);
    int numParameters = buf.get() & 0xFF;
    Annotation[][] result = new Annotation[numParameters][];

    for (int i = 0; i < numParameters; i++) {
        int numAnnotations = buf.getShort() & 0xFFFF;
        List<Annotation> annotations =
            new ArrayList<Annotation>(numAnnotations);
        for (int j = 0; j < numAnnotations; j++) {
            Annotation a = parseAnnotation(buf, constPool, container, false);
            if (a != null) {
                AnnotationType type = AnnotationType.getInstance(
                                          a.annotationType());
                if (type.retention() == RetentionPolicy.RUNTIME)
                    annotations.add(a);
            }
        }
        result[i] = annotations.toArray(EMPTY_ANNOTATIONS_ARRAY);
    }
    return result;
}
 
Example 29
private static Object parseShortArray(int length,
                               ByteBuffer buf, ConstantPool constPool) {
    short[] result = new short[length];
    boolean typeMismatch = false;
    int tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'S') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (short) constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}
 
Example 30
Source Project: java-n-IDE-for-Android   Source File: AnnotationParser.java    License: Apache License 2.0 6 votes vote down vote up
private static Object parseCharArray(int length,
                              ByteBuffer buf, ConstantPool constPool) {
    char[] result = new char[length];
    boolean typeMismatch = false;
    byte tag = 0;

    for (int i = 0; i < length; i++) {
        tag = buf.get();
        if (tag == 'C') {
            int index = buf.getShort() & 0xFFFF;
            result[i] = (char) constPool.getIntAt(index);
        } else {
            skipMemberValue(tag, buf);
            typeMismatch = true;
        }
    }
    return typeMismatch ? exceptionProxy(tag) : result;
}