public static ClassTypeResolver createClassTypeResolver( JavaSource javaSource, ClassLoader classLoader) { String packageName; Set<String> classImports = new HashSet<String>(); // Importer.getImports() returns both normal and static imports // You can see if an Import is static by calling hte // Import.isStatic() method List<Import> imports = javaSource.getImports(); if (imports != null) { for (Import currentImport : imports) { String importName = currentImport.getQualifiedName(); if (currentImport.isWildcard()) { importName = importName + ".*"; } classImports.add(importName); } } packageName = javaSource.getPackage(); // add current package too, if not added, the class type resolver don't resolve current package // classes. if (packageName != null && !"".equals(packageName)) { classImports.add(packageName + ".*"); } if (javaSource instanceof JavaClassSource) { JavaClassSource javaClassSource = (JavaClassSource) javaSource; // add current file inner types as import clauses to help the ClassTypeResolver to find // variables of inner types // It was detected that current ClassTypeResolver don't resolve inner classes well. // workaround for BZ https://bugzilla.redhat.com/show_bug.cgi?id=1172711 List<JavaSource<?>> innerTypes = javaClassSource.getNestedTypes(); if (innerTypes != null) { for (JavaSource<?> type : innerTypes) { classImports.add(packageName + "." + javaClassSource.getName() + "." + type.getName()); } } } return new ClassTypeResolver(classImports, classLoader); }
@Override public JavaResource saveTestJavaSource(final JavaSource<?> source) throws FileNotFoundException { return getTestJavaResource(source.getQualifiedName()).setContents(source); }
@Override public JavaResource getTestJavaResource(final JavaSource<?> javaClass) throws FileNotFoundException { String pkg = Strings.isNullOrEmpty(javaClass.getPackage()) ? "" : javaClass.getPackage() + "."; return getTestJavaResource(pkg + javaClass.getName()); }