
spoon.support.reflect.declaration.CtAnnotationImpl Maven / Gradle / Ivy
/*
* Spoon - http://spoon.gforge.inria.fr/
* Copyright (C) 2006 INRIA Futurs
*
* This software is governed by the CeCILL-C License under French law and
* abiding by the rules of distribution of free software. You can use, modify
* and/or redistribute the software under the terms of the CeCILL-C license as
* circulated by CEA, CNRS and INRIA at http://www.cecill.info.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package spoon.support.reflect.declaration;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import spoon.Launcher;
import spoon.reflect.code.CtCodeElement;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationType;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtSimpleType;
import spoon.reflect.eval.PartialEvaluator;
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.reference.CtTypeReference;
import spoon.reflect.visitor.CtVisitor;
/**
* The implementation for {@link spoon.reflect.declaration.CtAnnotation}.
*
* @author Renaud Pawlak
*/
public class CtAnnotationImpl extends CtElementImpl
implements CtAnnotation {
class AnnotationInvocationHandler implements InvocationHandler {
CtAnnotation extends Annotation> annotation;
public AnnotationInvocationHandler(
CtAnnotation extends Annotation> annotation) {
super();
this.annotation = annotation;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String fieldname = method.getName();
if (fieldname.equals("toString")) {
return CtAnnotationImpl.this.toString();
} else if (fieldname.equals("annotationType")) {
return annotation.getAnnotationType().getActualClass();
}
Object ret = getElementValue(fieldname);
// This is done here because return types should not be CT types;
// CtLiteral vs String.
if (ret instanceof CtLiteral>) {
CtLiteral> l = (CtLiteral>) ret;
return l.getValue();
}
return ret;
}
}
private static final long serialVersionUID = 1L;
CtTypeReference annotationType;
Map elementValues = new TreeMap() {
private static final long serialVersionUID = 3501647177461995350L;
@Override
public Object put(String key, Object value) {
if (value instanceof Class[]) {
Class>[] valsNew = (Class>[]) value;
ArrayList> ret = new ArrayList>(
valsNew.length);
for (int i = 0; i < valsNew.length; i++) {
Class> class1 = valsNew[i];
ret.add(i, getFactory().Type().createReference(class1));
}
return super.put(key, ret);
}
if (value instanceof Class) {
return super.put(key,
getFactory().Type().createReference((Class>) value));
}
return super.put(key, value);
}
};
public CtAnnotationImpl() {
super();
}
public void accept(CtVisitor visitor) {
visitor.visitCtAnnotation(this);
}
protected void appendValues(String elementName, Object... values) {
if (!elementValues.containsKey(elementName)) {
elementValues.put(elementName, values);
} else {
Object o = elementValues.get(elementName);
if (o.getClass().isArray()) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy