Usages of

com.ibm.wala.types.TypeName.findOrCreateClassName(?, ?)
private static IClass packageInfoFor(IClass clazz) { final TypeName name = clazz.getName(); final Atom packge = name.getPackage(); final TypeName packageInfoName = TypeName.findOrCreateClassName( packge.toString(), "package-info" ); return clazz.getClassLoader().lookupClass(packageInfoName); }
public class Annotations { public static final TypeName INTERNAL = TypeName.findOrCreateClassName("com/ibm/wala/annotations", "Internal"); public static final TypeName NONNULL = TypeName.findOrCreateClassName("com/ibm/wala/annotations", "NonNull"); /** * Does a particular method have a particular annotation? */ public static boolean hasAnnotation(IMethod m, TypeName type) { if (m instanceof ShrikeCTMethod) { Collection<Annotation> annotations = null; try { annotations = ((ShrikeCTMethod) m).getRuntimeInvisibleAnnotations(); } catch (InvalidClassFileException e) { e.printStackTrace(); Assertions.UNREACHABLE(); } for (Annotation a : annotations) { if (a.getType().getName().equals(type)) { return true; } } } return false; } /** * Does a particular class have a particular annotation? */ public static boolean hasAnnotation(IClass c, TypeName type) { if (c instanceof ShrikeClass) { Collection<Annotation> annotations = null; try { annotations = ((ShrikeClass) c).getRuntimeInvisibleAnnotations(); } catch (InvalidClassFileException e) { e.printStackTrace(); Assertions.UNREACHABLE(); } for (Annotation a : annotations) { if (a.getType().getName().equals(type)) { return true; } } } return false; } public static boolean hasAnnotation(IField field, TypeName type) { if (field instanceof FieldImpl) { FieldImpl f = (FieldImpl) field; Collection<Annotation> annotations = f.getAnnotations(); if (annotations != null) { for (Annotation a : annotations) { if (a.getType().getName().equals(type)) { return true; } } } } return false; } }
public class WalaClassLoader { private Collection<IClass> appClasses; private Collection<IClass> extClasses; private Collection<IClass> testClasses; private ClassHierarchy cha; private AnalysisScope scope; private static final String JUnitTestCase = "Ljunit/framework/TestCase"; private static final TypeName testAnnotation = TypeName.findOrCreateClassName("org/junit", "Test"); public WalaClassLoader( String walaScopeFile, String walaExclFile ) { load( walaScopeFile, walaExclFile ); } private void load( String walaScopeFile, String walaExclFile ) { try { scope = makeJ2SEAnalysisScope( walaScopeFile, walaExclFile ); cha = ClassHierarchy.make( scope ); // build list of application and external classes appClasses = new ArrayList<IClass>(); extClasses = new ArrayList<IClass>(); testClasses = new ArrayList<IClass>(); loadAllClasses( scope ); } catch ( IOException ioe ) { System.err.println( "Error creating analysis scope ..." ); ioe.printStackTrace(); throw new RuntimeException(); } catch ( ClassHierarchyException che ) { System.err.println( "Error creating class hierarchy ..." ); che.printStackTrace(); throw new RuntimeException(); } } private void loadAllClasses( AnalysisScope scope ) { for ( IClass cls : cha ) { if ( isApplicationClass( scope, cls ) ) { try { if ( isTestClass( cls ) ) { testClasses.add( cls ); } else { appClasses.add( cls ); } } catch ( ClassHierarchyException che ) { System.err.println( "Error loading: "+cls.getName().toString() ); che.printStackTrace(); } } else { extClasses.add( cls ); } } } private AnalysisScope makeJ2SEAnalysisScope( String scopeFile, String exclFile ) throws IOException { return com.ibm.wala.core.tests.callGraph.CallGraphTestUtil.makeJ2SEAnalysisScope( scopeFile, exclFile ); } private boolean isApplicationClass( final AnalysisScope scope, final IClass clazz ) { return scope.getApplicationLoader().equals( clazz.getClassLoader().getReference() ); } private boolean isTestClass( IClass clazz ) throws ClassHierarchyException { IClass superClass = clazz.getSuperclass(); while ( superClass != null ) { if ( superClass.getName().toString().equals( JUnitTestCase ) ) { return true; } superClass = superClass.getSuperclass(); } //does not have TestCase as a SuperClass for(IMethod method:clazz.getAllMethods()){ if(isTestMethod(method)){ return true; } } return false; } private boolean isTestMethod(IMethod method) { try { for (Annotation a : ((ShrikeCTMethod) method).getAnnotations(false)) { if (a.getType().getName().equals(testAnnotation)) { return true; } } } catch (InvalidClassFileException e) { e.printStackTrace(); } return false; } public Collection<IClass> getApplicationClasses() { return appClasses; } public Collection<IClass> getTestClasses() { return testClasses; } public Collection<IClass> getExternalClasses() { return extClasses; } public ClassHierarchy getClassHierarchy() { return cha; } public AnalysisScope getAnalysisScope() { return scope; } }
public class Annotations { public static final TypeName INTERNAL = TypeName.findOrCreateClassName("com/ibm/wala/annotations", "Internal"); public static final TypeName NONNULL = TypeName.findOrCreateClassName("com/ibm/wala/annotations", "NonNull"); /** * Does a particular method have a particular annotation? */ public static boolean hasAnnotation(IMethod m, TypeName type) { if (m instanceof ShrikeCTMethod) { Collection<Annotation> annotations = null; try { annotations = ((ShrikeCTMethod) m).getRuntimeInvisibleAnnotations(); } catch (InvalidClassFileException e) { e.printStackTrace(); Assertions.UNREACHABLE(); } catch (UnimplementedException e) { e.printStackTrace(); Assertions.UNREACHABLE(); } for (Annotation a : annotations) { if (a.getType().getName().equals(type)) { return true; } } } return false; } /** * Does a particular class have a particular annotation? */ public static boolean hasAnnotation(IClass c, TypeName type) { if (c instanceof ShrikeClass) { Collection<Annotation> annotations = null; try { annotations = ((ShrikeClass) c).getRuntimeInvisibleAnnotations(); } catch (InvalidClassFileException e) { e.printStackTrace(); Assertions.UNREACHABLE(); } catch (UnimplementedException e) { e.printStackTrace(); Assertions.UNREACHABLE(); } for (Annotation a : annotations) { if (a.getType().getName().equals(type)) { return true; } } } return false; } public static boolean hasAnnotation(IField field, TypeName type) { if (field instanceof FieldImpl) { FieldImpl f = (FieldImpl) field; Collection<Annotation> annotations = f.getAnnotations(); if (annotations != null) { for (Annotation a : annotations) { if (a.getType().getName().equals(type)) { return true; } } } } return false; } }
/** * The below code uses WALA to read the annotation. However, as of Dec 2011, WALA does not * support method annotation yet, although a late commit indicates the annotation support * has been added: * http://sourceforge.net/mailarchive/forum.php?thread_name=OFA74890B8.9E3755EA-ON87257982. * 006305F0-87257982.006315AE%40us.ibm.com&forum_name=wala-wala * * For the sake of reducing potential risk, the above Java reflection-based approach * is still being used. * */ public static Map<String, Collection<String>> getNativeCallRelations( ClassHierarchy cha) throws InvalidClassFileException, UnimplementedException { Map<String, Collection<String>> callRelations = new LinkedHashMap<String, Collection<String>>(); TypeName nativeAnnotation = TypeName.findOrCreateClassName( "edu/washington/cs/detector/util", "CallByNativeMethods"); for (IClass clz : cha) { Collection<IMethod> methods = clz.getDeclaredMethods(); for (IMethod method : methods) { if (method instanceof ShrikeCTMethod) { Annotation ann = getVisibleAnnotation(method, nativeAnnotation); if (ann != null) { System.out.println(" method: " + method); throw new RuntimeException("No idea in how to get annotation values!"); } } else { System.err.println("WARNING: see a non-ShrikeCTMethod: " + method + ", type: " + method.getClass()); } } } return callRelations; }
Usage snippet has been bookmarked! Review your bookmarks
Thank you! Review your likes