read runtime-visible annotations for fields
This commit is contained in:
parent
fa14d7a73d
commit
9549da9954
@ -0,0 +1,21 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2008 IBM Corporation.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package annotations;
|
||||
|
||||
/**
|
||||
* class with annotated fields
|
||||
*/
|
||||
public class AnnotatedClass4 {
|
||||
|
||||
@RuntimeInvisableAnnotation
|
||||
@RuntimeVisableAnnotation
|
||||
public static int foo;
|
||||
}
|
||||
@ -10,6 +10,7 @@ import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.ibm.wala.classLoader.IClass;
|
||||
import com.ibm.wala.classLoader.IField;
|
||||
import com.ibm.wala.classLoader.IMethod;
|
||||
import com.ibm.wala.classLoader.ShrikeCTMethod;
|
||||
import com.ibm.wala.classLoader.ShrikeClass;
|
||||
@ -22,6 +23,7 @@ import com.ibm.wala.ipa.cha.ClassHierarchyException;
|
||||
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
||||
import com.ibm.wala.shrikeCT.InvalidClassFileException;
|
||||
import com.ibm.wala.types.ClassLoaderReference;
|
||||
import com.ibm.wala.types.FieldReference;
|
||||
import com.ibm.wala.types.MethodReference;
|
||||
import com.ibm.wala.types.Selector;
|
||||
import com.ibm.wala.types.TypeReference;
|
||||
@ -29,6 +31,7 @@ import com.ibm.wala.types.annotations.Annotation;
|
||||
import com.ibm.wala.util.collections.HashSetFactory;
|
||||
import com.ibm.wala.util.config.AnalysisScopeReader;
|
||||
import com.ibm.wala.util.io.FileProvider;
|
||||
import com.ibm.wala.util.strings.Atom;
|
||||
|
||||
public class AnnotationTest extends WalaTestCase {
|
||||
|
||||
@ -142,4 +145,21 @@ public class AnnotationTest extends WalaTestCase {
|
||||
runtimeInvisibleAnnotations.toString());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassAnnotations4() throws Exception {
|
||||
|
||||
TypeReference typeRef = TypeReference.findOrCreate(ClassLoaderReference.Application, "Lannotations/AnnotatedClass4");
|
||||
FieldReference fieldRefUnderTest = FieldReference.findOrCreate(typeRef, Atom.findOrCreateUnicodeAtom("foo"), TypeReference.Int);
|
||||
|
||||
IField fieldUnderTest = cha.resolveField(fieldRefUnderTest);
|
||||
Assert.assertNotNull(fieldRefUnderTest.toString() + " not found", fieldUnderTest);
|
||||
|
||||
Collection<Annotation> annots = fieldUnderTest.getAnnotations();
|
||||
Assert
|
||||
.assertEquals(
|
||||
"[Annotation type <Application,Lannotations/RuntimeInvisableAnnotation>, Annotation type <Application,Lannotations/RuntimeVisableAnnotation>]",
|
||||
annots.toString());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,9 +82,10 @@ public final class ShrikeClass extends JVMClass<IClassLoader> {
|
||||
int accessFlags = cr.getFieldAccessFlags(i);
|
||||
Atom name = Atom.findOrCreateUnicodeAtom(cr.getFieldName(i));
|
||||
ImmutableByteArray b = ImmutableByteArray.make(cr.getFieldType(i));
|
||||
Collection<Annotation> annotations = null;
|
||||
annotations = getRuntimeInvisibleAnnotations(i);
|
||||
annotations = annotations.isEmpty() ? null : annotations;
|
||||
Collection<Annotation> annotations = HashSetFactory.make();
|
||||
annotations.addAll(getRuntimeInvisibleAnnotations(i));
|
||||
annotations.addAll(getRuntimeVisibleAnnotations(i));
|
||||
annotations = annotations.isEmpty() ? null : annotations;
|
||||
|
||||
if ((accessFlags & ClassConstants.ACC_STATIC) == 0) {
|
||||
addFieldToList(instanceList, name, b, accessFlags, annotations);
|
||||
@ -291,17 +292,24 @@ public final class ShrikeClass extends JVMClass<IClassLoader> {
|
||||
return result;
|
||||
}
|
||||
|
||||
private RuntimeInvisibleAnnotationsReader getRuntimeInvisibleAnnotationsReader(int fieldIndex) throws InvalidClassFileException {
|
||||
private AnnotationsReader getFieldAnnotationsReader(boolean runtimeInvisible, int fieldIndex) throws InvalidClassFileException {
|
||||
ClassReader.AttrIterator iter = new AttrIterator();
|
||||
reader.get().initFieldAttributeIterator(fieldIndex, iter);
|
||||
|
||||
// search for the desired attribute
|
||||
RuntimeInvisibleAnnotationsReader result = null;
|
||||
AnnotationsReader result = null;
|
||||
try {
|
||||
for (; iter.isValid(); iter.advance()) {
|
||||
if (iter.getName().toString().equals("RuntimeInvisibleAnnotations")) {
|
||||
result = new RuntimeInvisibleAnnotationsReader(iter);
|
||||
break;
|
||||
if (runtimeInvisible) {
|
||||
if (iter.getName().equals(RuntimeInvisibleAnnotationsReader.attrName)) {
|
||||
result = new RuntimeInvisibleAnnotationsReader(iter);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (iter.getName().equals(RuntimeVisibleAnnotationsReader.attrName)) {
|
||||
result = new RuntimeVisibleAnnotationsReader(iter);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (InvalidClassFileException e) {
|
||||
@ -314,7 +322,18 @@ public final class ShrikeClass extends JVMClass<IClassLoader> {
|
||||
* read the runtime-invisible annotations from the class file
|
||||
*/
|
||||
public Collection<Annotation> getRuntimeInvisibleAnnotations(int fieldIndex) throws InvalidClassFileException {
|
||||
RuntimeInvisibleAnnotationsReader r = getRuntimeInvisibleAnnotationsReader(fieldIndex);
|
||||
return getFieldAnnotations(fieldIndex, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* read the runtime-invisible annotations from the class file
|
||||
*/
|
||||
public Collection<Annotation> getRuntimeVisibleAnnotations(int fieldIndex) throws InvalidClassFileException {
|
||||
return getFieldAnnotations(fieldIndex, false);
|
||||
}
|
||||
|
||||
protected Collection<Annotation> getFieldAnnotations(int fieldIndex, boolean runtimeInvisible) throws InvalidClassFileException {
|
||||
AnnotationsReader r = getFieldAnnotationsReader(runtimeInvisible, fieldIndex);
|
||||
return Annotation.getAnnotationsFromReader(r, getClassLoader().getReference());
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user