/** * Lookup a class. * <em>Use this method instead of Repository.lookupClass().</em> * * @param classDescriptor * descriptor specifying the class to look up * @return the class * @throws ClassNotFoundException * if the class can't be found */ public JavaClass lookupClass(@Nonnull ClassDescriptor classDescriptor) throws ClassNotFoundException { return lookupClass(classDescriptor.toDottedClassName()); }
/** * This is equivalent to Repository.lookupClass() or this.lookupClass(), * except it uses the original Repository instead of the current one. * * This can be important because URLClassPathRepository objects are closed * after an analysis, so JavaClass objects obtained from them are no good on * subsequent runs. * * @param className * the name of the class * @return the JavaClass representing the class * @throws ClassNotFoundException */ public static JavaClass lookupSystemClass(@Nonnull String className) throws ClassNotFoundException { // TODO: eventually we should move to our own thread-safe repository // implementation requireNonNull (className, "className is null"); if (originalRepository == null) { throw new IllegalStateException("originalRepository is null"); } JavaClass clazz = originalRepository.findClass(className); if(clazz != null){ return clazz; } // XXX workaround for system classes missing on Java 9 // Not sure if we BCEL update, but this seem to work in simple cases return AnalysisContext.currentAnalysisContext().lookupClass(className); }
/** * Given two JavaClasses, try to estimate the probability that an reference * of type x is also an instance of type y. Will return 0 only if it is * impossible and 1 only if it is guaranteed. * * @param x * Known type of object * @param y * Type queried about * @return 0 - 1 value indicating probablility */ public static double deepInstanceOf(@DottedClassName String x, @DottedClassName String y) throws ClassNotFoundException { return deepInstanceOf(AnalysisContext.currentAnalysisContext().lookupClass(x), AnalysisContext.currentAnalysisContext() .lookupClass(y)); }
@Override public void observeClass(ClassDescriptor classDescriptor) { try { JavaClass javaClass = AnalysisContext.currentAnalysisContext().lookupClass(classDescriptor.toDottedClassName()); String sourceFileName = fileNameFor(javaClass.getPackageName(), javaClass.getSourceFileName()); sourceFileNameCache.put(javaClass.getClassName(), sourceFileName); } catch (ClassNotFoundException e) { // Ignore - should not happen } }
static SourceLineAnnotation getSourceAnnotationForClass(String className, String sourceFileName) { int lastLine = -1; int firstLine = Integer.MAX_VALUE; try { JavaClass targetClass = AnalysisContext.currentAnalysisContext().lookupClass(className); for (Method m : targetClass.getMethods()) { Code c = m.getCode(); if (c != null) { LineNumberTable table = c.getLineNumberTable(); if (table != null) { for (LineNumber line : table.getLineNumberTable()) { lastLine = Math.max(lastLine, line.getLineNumber()); firstLine = Math.min(firstLine, line.getLineNumber()); } } } } } catch (ClassNotFoundException e) { AnalysisContext.reportMissingClass(e); } if (firstLine < Integer.MAX_VALUE) { return new SourceLineAnnotation(className, sourceFileName, firstLine, lastLine, -1, -1); } return SourceLineAnnotation.createUnknown(className, sourceFileName); }
JavaClass javaLangObject = AnalysisContext.currentAnalysisContext().lookupClass("java.lang.Object"); JavaClassAndMethod classAndMethod = findMethod(javaLangObject, methodName, methodSig, INSTANCE_METHOD); if (classAndMethod != null) { JavaClass receiverClass = analysisContext.lookupClass(receiverClassName); ClassDescriptor receiverDesc = DescriptorFactory.createClassDescriptorFromDottedClassName(receiverClassName);
clazz = AnalysisContext.currentAnalysisContext().lookupClass(descriptor); } catch (ClassNotFoundException e) {
if (!dottedClassName.startsWith("junit")) { try { JavaClass targetClass = AnalysisContext.currentAnalysisContext().lookupClass(dottedClassName); if (!targetClass.getSuperclassName().startsWith("junit")) { break AssertInvokedFromRun;
static SourceLineAnnotation getSourceAnnotationForMethod(String className, String methodName, String methodSig) { JavaClassAndMethod targetMethod = null; Code code = null; try { JavaClass targetClass = AnalysisContext.currentAnalysisContext().lookupClass(className); targetMethod = Hierarchy.findMethod(targetClass, methodName, methodSig); if (targetMethod != null) { Method method = targetMethod.getMethod(); if (method != null) { code = method.getCode(); } } } catch (ClassNotFoundException e) { AnalysisContext.reportMissingClass(e); } SourceInfoMap sourceInfoMap = AnalysisContext.currentAnalysisContext().getSourceInfoMap(); SourceInfoMap.SourceLineRange range = sourceInfoMap.getMethodLine(className, methodName, methodSig); if (range != null) { return new SourceLineAnnotation(className, AnalysisContext.currentAnalysisContext().lookupSourceFile(className), range.getStart(), range.getEnd(), 0, code == null ? -1 : code.getLength()); } if (sourceInfoMap.fallBackToClassfile() && targetMethod != null) { return forEntireMethod(targetMethod.getJavaClass(), targetMethod.getMethod()); } // If we couldn't find the source lines, // create an unknown source line annotation referencing // the class and source file. return createUnknown(className); }
/** * Lookup a class. * <em>Use this method instead of Repository.lookupClass().</em> * * @param classDescriptor * descriptor specifying the class to look up * @return the class * @throws ClassNotFoundException * if the class can't be found */ public JavaClass lookupClass(@Nonnull ClassDescriptor classDescriptor) throws ClassNotFoundException { return lookupClass(classDescriptor.toDottedClassName()); }
/** * Given two JavaClasses, try to estimate the probability that an reference * of type x is also an instance of type y. Will return 0 only if it is * impossible and 1 only if it is guaranteed. * * @param x * Known type of object * @param y * Type queried about * @return 0 - 1 value indicating probablility */ public static double deepInstanceOf(@DottedClassName String x, @DottedClassName String y) throws ClassNotFoundException { return deepInstanceOf(AnalysisContext.currentAnalysisContext().lookupClass(x), AnalysisContext.currentAnalysisContext() .lookupClass(y)); }
@Override public void observeClass(ClassDescriptor classDescriptor) { try { JavaClass javaClass = AnalysisContext.currentAnalysisContext().lookupClass(classDescriptor.toDottedClassName()); String sourceFileName = fileNameFor(javaClass.getPackageName(), javaClass.getSourceFileName()); sourceFileNameCache.put(javaClass.getClassName(), sourceFileName); } catch (ClassNotFoundException e) { // Ignore - should not happen } }
static SourceLineAnnotation getSourceAnnotationForClass(String className, String sourceFileName) { int lastLine = -1; int firstLine = Integer.MAX_VALUE; try { JavaClass targetClass = AnalysisContext.currentAnalysisContext().lookupClass(className); for (Method m : targetClass.getMethods()) { Code c = m.getCode(); if (c != null) { LineNumberTable table = c.getLineNumberTable(); if (table != null) { for (LineNumber line : table.getLineNumberTable()) { lastLine = Math.max(lastLine, line.getLineNumber()); firstLine = Math.min(firstLine, line.getLineNumber()); } } } } } catch (ClassNotFoundException e) { AnalysisContext.reportMissingClass(e); } if (firstLine < Integer.MAX_VALUE) { return new SourceLineAnnotation(className, sourceFileName, firstLine, lastLine, -1, -1); } return SourceLineAnnotation.createUnknown(className, sourceFileName); }
JavaClass javaLangObject = AnalysisContext.currentAnalysisContext().lookupClass("java.lang.Object"); JavaClassAndMethod classAndMethod = findMethod(javaLangObject, methodName, methodSig, INSTANCE_METHOD); if (classAndMethod != null) { JavaClass receiverClass = analysisContext.lookupClass(receiverClassName); ClassDescriptor receiverDesc = DescriptorFactory.createClassDescriptorFromDottedClassName(receiverClassName);
if (!dottedClassName.startsWith("junit")) { try { JavaClass targetClass = AnalysisContext.currentAnalysisContext().lookupClass(dottedClassName); if (!targetClass.getSuperclassName().startsWith("junit")) { break AssertInvokedFromRun;
static SourceLineAnnotation getSourceAnnotationForMethod(String className, String methodName, String methodSig) { JavaClassAndMethod targetMethod = null; Code code = null; try { JavaClass targetClass = AnalysisContext.currentAnalysisContext().lookupClass(className); targetMethod = Hierarchy.findMethod(targetClass, methodName, methodSig); if (targetMethod != null) { Method method = targetMethod.getMethod(); if (method != null) { code = method.getCode(); } } } catch (ClassNotFoundException e) { AnalysisContext.reportMissingClass(e); } SourceInfoMap sourceInfoMap = AnalysisContext.currentAnalysisContext().getSourceInfoMap(); SourceInfoMap.SourceLineRange range = sourceInfoMap.getMethodLine(className, methodName, methodSig); if (range != null) { return new SourceLineAnnotation(className, AnalysisContext.currentAnalysisContext().lookupSourceFile(className), range.getStart(), range.getEnd(), 0, code == null ? -1 : code.getLength()); } if (sourceInfoMap.fallBackToClassfile() && targetMethod != null) { return forEntireMethod(targetMethod.getJavaClass(), targetMethod.getMethod()); } // If we couldn't find the source lines, // create an unknown source line annotation referencing // the class and source file. return createUnknown(className); }