org.jboss.jandex.AnnotationInstance Maven / Gradle / Ivy
/*
* JBoss, Home of Professional Open Source.
* Copyright 2013 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.jandex;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* An annotation instance represents a specific usage of an annotation on a
* target. It contains a set of values, as well as a reference to the target
* itself (e.g. class, field, method, etc).
*
*
* Thread-Safety
*
* This class is immutable and can be shared between threads without safe
* publication.
*
* @author Jason T. Greene
*
*/
public final class AnnotationInstance {
private static final AnnotationValue[] ANNOTATION_VALUES_TYPE = new AnnotationValue[0];
static final InstanceNameComparator NAME_COMPARATOR = new InstanceNameComparator();
static final AnnotationInstance[] EMPTY_ARRAY = new AnnotationInstance[0];
private final DotName name;
private AnnotationTarget target;
private final AnnotationValue[] values;
static class InstanceNameComparator implements Comparator {
public int compare(AnnotationInstance instance, AnnotationInstance instance2) {
return instance.name().compareTo(instance2.name());
}
}
AnnotationInstance(AnnotationInstance instance, AnnotationTarget target) {
this.name = instance.name;
this.values = instance.values;
this.target = target;
}
AnnotationInstance(DotName name, AnnotationTarget target, AnnotationValue[] values) {
this.name = name;
this.target = target;
this.values = values != null && values.length > 0 ? values : AnnotationValue.EMPTY_VALUE_ARRAY;
}
/**
* Construct a new mock annotation instance. The passed values array will be defensively copied.
*
* @param name the name of the annotation instance
* @param target the thing the annotation is declared on
* @param values the values of this annotation instance
* @return the new mock Annotation Instance
*/
public static final AnnotationInstance create(DotName name, AnnotationTarget target, AnnotationValue[] values) {
if (name == null)
throw new IllegalArgumentException("Name can't be null");
if (values == null)
throw new IllegalArgumentException("Values can't be null");
values = values.clone();
// Sort entries so they can be binary searched
Arrays.sort(values, new Comparator() {
public int compare(AnnotationValue o1, AnnotationValue o2) {
return o1.name().compareTo(o2.name());
}
});
return new AnnotationInstance(name, target, values);
}
/**
* Construct a new mock annotation instance. The passed values list will be defensively copied.
*
* @param name the name of the annotation instance
* @param target the thing the annotation is declared on
* @param values the values of this annotation instance
* @return the new mock Annotation Instance
*/
public static final AnnotationInstance create(DotName name, AnnotationTarget target, List values) {
if (name == null)
throw new IllegalArgumentException("Name can't be null");
if (values == null)
throw new IllegalArgumentException("Values can't be null");
return create(name, target, values.toArray(ANNOTATION_VALUES_TYPE));
}
/**
* The name of this annotation in DotName form.
*
* @return the name of this annotation
*/
public DotName name() {
return name;
}
/**
* The Java element that this annotation was declared on. This can be
* a class, a field, a method, or a method parameter. In addition it may
* be null if this instance is a nested annotation, in which case there is
* no target.
*
* @return the target this annotation instance refers to
*/
public AnnotationTarget target() {
return target;
}
/**
* Returns a value that corresponds with the specified parameter name.
* If the parameter was not specified by this instance then null is
* returned. Note that this also applies to a defaulted parameter,
* which is not recorded in the target class.
*
* @param name the parameter name
* @return the value of the specified parameter, or null if not provided
*/
public AnnotationValue value(final String name) {
int result = Arrays.binarySearch(values, name, new Comparator
© 2015 - 2024 Weber Informatics LLC | Privacy Policy