public void addAnnotationParamDefaultValue(final AnnotationParameterValue annotationParamDefaultValue) { if (annotationParamDefaultValues == null) { annotationParamDefaultValues = new AnnotationParameterValueList(); } this.annotationParamDefaultValues.add(annotationParamDefaultValue); }
/** * Add annotation default values. (Only called in the case of annotation class definitions, when the annotation * has default parameter values.) */ void addAnnotationParamDefaultValues(final AnnotationParameterValueList paramNamesAndValues) { if (this.annotationDefaultParamValues == null) { this.annotationDefaultParamValues = paramNamesAndValues; } else { this.annotationDefaultParamValues.addAll(paramNamesAndValues); } }
void convertWrapperArraysToPrimitiveArrays() { if (annotationParamValues != null) { annotationParamValues.convertWrapperArraysToPrimitiveArrays(getClassInfo()); } }
/** * @return The string representations of all annotation parameter values in this list, by calling * {@link AnnotationParameterValue#toString()} for each item in the list. */ public List<String> getAsStrings() { if (this.isEmpty()) { return Collections.emptyList(); } else { final List<String> toStringVals = new ArrayList<>(this.size()); for (final AnnotationParameterValue apv : this) { toStringVals.add(apv.toString()); } return toStringVals; } }
/** * Render as a string, into a StringBuilder buffer. * * @param buf * The buffer. */ void toString(final StringBuilder buf) { buf.append("@").append(getName()); if (annotationParamValues != null && !annotationParamValues.isEmpty()) { buf.append('('); for (int i = 0; i < annotationParamValues.size(); i++) { if (i > 0) { buf.append(", "); } final AnnotationParameterValue annotationParamValue = annotationParamValues.get(i); if (annotationParamValues.size() > 1 || !"value".equals(annotationParamValue.getName())) { annotationParamValue.toString(buf); } else { annotationParamValue.toStringParamValueOnly(buf); } } buf.append(')'); } }
@Override public int compareTo(final AnnotationInfo o) { final int diff = getName().compareTo(o.getName()); if (diff != 0) { return diff; } if (annotationParamValues == null && o.annotationParamValues == null) { return 0; } else if (annotationParamValues == null) { return -1; } else if (o.annotationParamValues == null) { return 1; } else { for (int i = 0, max = Math.max(annotationParamValues.size(), o.annotationParamValues.size()); i < max; i++) { if (i >= annotationParamValues.size()) { return -1; } else if (i >= o.annotationParamValues.size()) { return 1; } else { final int diff2 = annotationParamValues.get(i).compareTo(o.annotationParamValues.get(i)); if (diff2 != 0) { return diff2; } } } } return 0; }
annotationParamValues.convertWrapperArraysToPrimitiveArrays(classInfo); annotationParamValuesHasBeenConvertedToPrimitive = true; classInfo.annotationDefaultParamValues.convertWrapperArraysToPrimitiveArrays(classInfo); classInfo.annotationDefaultParamValuesHasBeenConvertedToPrimitive = true; allParamValues.put(annotationParamValue.getName(), annotationParamValue.getValue()); annotationParamValuesWithDefaults = new AnnotationParameterValueList(); for (final Entry<String, Object> ent : allParamValues.entrySet()) { annotationParamValuesWithDefaults.add(new AnnotationParameterValue(ent.getKey(), ent.getValue()));
/** * Resolves the {@link Class}[] of the {@code HandlesTypes}-annotations present on the given * {@code ServletContainerInitializer} implementation. * * @param servletContainerInitializerClassInfo The {@link ClassInfo} object representing the {@code ServletContainerInitializer} implementation * @return The classes listed in the {@code HandlesTypes} annotation of the given class. */ private List<ClassInfo> resolveHandledTypes(ClassInfo servletContainerInitializerClassInfo) { AnnotationInfo handlesTypes = servletContainerInitializerClassInfo.getAnnotationInfo(HANDLES_TYPES); Object[] value = (Object[]) handlesTypes.getParameterValues().get("value"); return Arrays.stream(value) .map(AnnotationClassRef.class::cast) .map(annotationClassRef -> { ClassInfo classInfo = annotationClassRef.getClassInfo(); if (classInfo == null) { log.warn("{} not found in the scan result, but declared in the @HandlesTypes annotation of {}", annotationClassRef.getName(), servletContainerInitializerClassInfo.getName()); } return classInfo; }) .filter(Objects::nonNull) .collect(Collectors.toList()); }
/** * @return The names of all {@link AnnotationParameterValue} objects in this list, by calling * {@link AnnotationParameterValue#getName()} for each item in the list. */ public List<String> getNames() { if (this.isEmpty()) { return Collections.emptyList(); } else { final List<String> classNames = new ArrayList<>(this.size()); for (final AnnotationParameterValue apv : this) { classNames.add(apv.getName()); } return classNames; } }
/** Read annotation entry from classfile. */ private AnnotationInfo readAnnotation() throws IOException, IllegalArgumentException { // Lcom/xyz/Annotation; -> Lcom.xyz.Annotation; final String annotationClassName = getConstantPoolClassDescriptor( inputStreamOrByteBuffer.readUnsignedShort()); final int numElementValuePairs = inputStreamOrByteBuffer.readUnsignedShort(); AnnotationParameterValueList paramVals = null; if (numElementValuePairs > 0) { paramVals = new AnnotationParameterValueList(numElementValuePairs); for (int i = 0; i < numElementValuePairs; i++) { final String paramName = getConstantPoolString(inputStreamOrByteBuffer.readUnsignedShort()); final Object paramValue = readAnnotationElementValue(); paramVals.add(new AnnotationParameterValue(paramName, paramValue)); } } return new AnnotationInfo(annotationClassName, paramVals); }
/** * @return A list of {@link AnnotationParameterValue} objects for each of the default parameter values for this * annotation, if this is an annotation class with default parameter values, otherwise the empty list. */ public AnnotationParameterValueList getAnnotationDefaultParameterValues() { if (!scanResult.scanSpec.enableAnnotationInfo) { throw new IllegalArgumentException("Please call ClassGraph#enableAnnotationInfo() before #scan()"); } if (!isAnnotation) { throw new IllegalArgumentException("Class is not an annotation: " + getName()); } if (annotationDefaultParamValues == null) { return AnnotationParameterValueList.EMPTY_LIST; } if (!annotationDefaultParamValuesHasBeenConvertedToPrimitive) { annotationDefaultParamValues.convertWrapperArraysToPrimitiveArrays(this); annotationDefaultParamValuesHasBeenConvertedToPrimitive = true; } return annotationDefaultParamValues; }