private JavaClass _getJavaClass(String fileName, Reader reader) throws Exception { int pos = fileName.indexOf("src/"); if (pos == -1) { pos = fileName.indexOf("test/"); } if (pos == -1) { throw new RuntimeException(fileName); } pos = fileName.indexOf("/", pos); String srcFile = fileName.substring(pos + 1, fileName.length()); String className = StringUtil.replace(srcFile.substring(0, srcFile.length() - 5), "/", "."); JavaDocBuilder builder = new JavaDocBuilder(); if (reader == null) { File file = new File(fileName); if (!file.exists()) { return null; } builder.addSource(file); } else { builder.addSource(reader); } return builder.getClassByName(className); }
private JavaClass _getJavaClass(String parentDir, String srcFile) throws IOException { String className = StringUtil.replace(srcFile.substring(0, srcFile.length() - 5), "/", "."); JavaDocBuilder builder = new JavaDocBuilder(); builder.addSource(new File(parentDir + "/" + srcFile)); return builder.getClassByName(className); }
@Override protected void doWithResourceDocument(ResourceDocument document, Class<?> classOfResource) { // Check if the class is present in the builder. if (javaDocBuilder.getClassByName(classOfResource.getName()) == null) throw new IllegalArgumentException( "Class " + classOfResource + " was not found within the specified source files."); JavaClass javaClass = javaDocBuilder.getClassByName(classOfResource.getName()); document.setDescription(javaClass.getComment()); document.setTitle(titleOrNull(javaClass)); }
@BeforeClass public static void generateClasses() throws ClassNotFoundException, IOException { File outputDirectory = generate("/schema/title/title.json", "com.example"); File generatedJavaFile = new File(outputDirectory, "com/example/Title.java"); compile(outputDirectory); JavaDocBuilder javaDocBuilder = new JavaDocBuilder(); javaDocBuilder.addSource(generatedJavaFile); classWithTitle = javaDocBuilder.getClassByName("com.example.Title"); }
/** * Produces the {@link JavaDocBuilder} used to analyze classes and source files. * * @param parent parent classloader * @return The {@link JavaDocBuilder} used to analyze classes and source files. * @throws MojoExecutionException If we fail to produce the {@link JavaDocBuilder}. (Fail early.) */ private JavaDocBuilder createJavaDocBuilder(ClassLoader parent) throws MojoExecutionException { JavaDocBuilder builder = new JavaDocBuilder(); builder.addSourceTree(new File(project.getBuild().getSourceDirectory())); // Quick and dirty hack for adding Classloaders with the definitions of // classes the sources depend upon. try { URLClassLoader loader = getProjectClassLoader(parent); builder.getClassLibrary().addClassLoader(loader); } catch (InvalidDependencyVersionException idve) { idve.printStackTrace(); } catch (MalformedURLException e) { throw new MojoExecutionException("Malformed artifact URLs.", e); } return builder; }
/** * Get ITestNGMethod author(s) string, or class author(s) if no method author is present. Default * return value is "unknown". * * @param className * @param method * @return * @author hzjingcheng */ private String getAuthors(String className, ITestNGMethod method) { JavaClass cls = builder.getClassByName(className); DocletTag[] authors = cls.getTagsByName("author"); // get class authors as default author name String allAuthors = ""; if (authors.length == 0) { allAuthors = "unknown"; } else { for (DocletTag author : authors) { allAuthors += author.getValue() + " "; } } // get method author name JavaMethod[] mtds = cls.getMethods(); for (JavaMethod mtd : mtds) { if (mtd.getName().equals(method.getMethodName())) { authors = mtd.getTagsByName("author"); if (authors.length != 0) { allAuthors = ""; for (DocletTag author : authors) { allAuthors += author.getValue() + " "; } } break; } } return allAuthors.trim(); }
/** * Parses a specific class. * * @todo QDox seems to have a problem retrieving inner classes => null * @param className the name of the class to compile * @return true if class was found A false otherwise */ public boolean parse(final String className) { m_class = m_builder.getClassByName(className); if (m_class == null) { return false; } m_className = m_class.getFullyQualifiedName(); return true; }
private JavaClass _getJavaClass(String fileName, Reader reader) throws Exception { String className = _getClassName(fileName); JavaDocBuilder javadocBuilder = new JavaDocBuilder(); if (reader == null) { File file = new File(fileName); if (!file.exists()) { return null; } javadocBuilder.addSource(file); } else { javadocBuilder.addSource(reader); } return javadocBuilder.getClassByName(className); }
/** * Returns all classes. * * @return a collections with all classes */ public String[] getAllClassNames() { Collection classes = m_builder.getClassLibrary().all(); Collection classNames = new ArrayList(); String className = null; for (Iterator it = classes.iterator(); it.hasNext(); ) { className = (String) it.next(); if ("java.lang.Object".equals(className)) continue; classNames.add(className); } return (String[]) classNames.toArray(new String[] {}); }
/** Creates summary of the run */ public void generateReport(List<XmlSuite> xml, List<ISuite> suites, String outdir) { try { m_out = createWriter(outdir); } catch (IOException e) { L.error("output file", e); return; } ConfigReader cr = ConfigReader.getInstance(); builder.setEncoding(cr.getSrouceCodeEncoding()); builder.addSourceTree(new File(cr.getSourceCodeDir())); startHtml(m_out); generateSuiteSummaryReport(suites); testIds.clear(); generateMethodSummaryReport(suites); testIds.clear(); generateMethodDetailReport(suites); testIds.clear(); endHtml(m_out); m_out.flush(); m_out.close(); }
private JavaMethod getJavaMethod(Method method) { Class<?> declaringClass = method.getDeclaringClass(); final JavaClass doc = javaDocBuilder.getClassByName(declaringClass.getName()); return doc.getMethodBySignature(method.getName(), convertMethodParameterTypes(method)); }
public void processSources( File[] sourceDirectories, File outputFile, boolean containerDescriptor, ComponentDescriptor[] roleDefaults) throws ComponentDescriptorCreatorException { // ---------------------------------------------------------------------- // Check and register all directories to scan // ---------------------------------------------------------------------- JavaSource[] javaSources; JavaDocBuilder builder = new JavaDocBuilder(); getLogger().debug("Source directories: "); for (int it = 0; it < sourceDirectories.length; it++) { File sourceDirectory = sourceDirectories[it]; if (!sourceDirectory.isDirectory()) { getLogger() .debug( "Specified source directory isn't a directory: " + "'" + sourceDirectory.getAbsolutePath() + "'."); } getLogger().debug(" - " + sourceDirectory.getAbsolutePath()); builder.addSourceTree(sourceDirectory); } // ---------------------------------------------------------------------- // Scan the sources // ---------------------------------------------------------------------- javaSources = builder.getSources(); Map defaultsByRole = new HashMap(); if (roleDefaults != null) { for (int i = 0; i < roleDefaults.length; i++) { // TODO: fail if role is null defaultsByRole.put(roleDefaults[i].getRole(), roleDefaults[i]); } } List componentDescriptors = new ArrayList(); for (int i = 0; i < javaSources.length; i++) { JavaClass javaClass = getJavaClass(javaSources[i]); ComponentDescriptor componentDescriptor = gleaner.glean(builder, javaClass); if (componentDescriptor != null && !javaClass.isAbstract()) { // TODO: better merge, perhaps pass it into glean as the starting point instead if (defaultsByRole.containsKey(componentDescriptor.getRole())) { ComponentDescriptor desc = (ComponentDescriptor) defaultsByRole.get(componentDescriptor.getRole()); if (componentDescriptor.getInstantiationStrategy() == null) { componentDescriptor.setInstantiationStrategy(desc.getInstantiationStrategy()); } } componentDescriptors.add(componentDescriptor); } } ComponentSetDescriptor componentSetDescriptor = new ComponentSetDescriptor(); componentSetDescriptor.setComponents(componentDescriptors); // ---------------------------------------------------------------------- // Convert the Maven dependencies to Plexus component dependencies // ---------------------------------------------------------------------- // List componentDependencies = convertDependencies( mavenProject.getDependencies() ); // // componentSetDescriptor.setDependencies( componentDependencies ); // TODO: for now componentSetDescriptor.setDependencies(Collections.EMPTY_LIST); // ---------------------------------------------------------------------- // // ---------------------------------------------------------------------- validateConfiguration(componentSetDescriptor); // ---------------------------------------------------------------------- // Write out the component descriptor // ---------------------------------------------------------------------- if (componentDescriptors.size() == 0 && componentSetDescriptor.getDependencies().size() == 0) { getLogger().debug("No components or dependencies found, not writing components.xml"); return; } File parentFile = outputFile.getParentFile(); if (!parentFile.exists()) { if (!parentFile.mkdirs()) { throw new ComponentDescriptorCreatorException( "Could not make parent directory: '" + parentFile.getAbsolutePath() + "'."); } } try { FileWriter writer = new FileWriter(outputFile); XMLWriter w = new PrettyPrintXMLWriter(writer); w.startElement(containerDescriptor ? "plexus" : "component-set"); writeComponents(w, componentDescriptors); writeDependencies(w, componentSetDescriptor); w.endElement(); writer.write(LS); writer.close(); } catch (PlexusConfigurationException e) { throw new ComponentDescriptorCreatorException( "Internal error while writing out the configuration", e); } catch (IOException e) { throw new ComponentDescriptorCreatorException( "Error while writing the component descriptor to: " + "'" + outputFile.getAbsolutePath() + "'.", e); } }
/** * Get comment string of Java class. * * @param className * @return * @author hzjingcheng */ @SuppressWarnings("unused") private String getClassComment(String className) { JavaClass cls = builder.getClassByName(className); return cls.getComment(); }
/** * Adds a source tree to the builder. * * @param srcDir the source tree to getJavaMethods */ public QDoxParser(final String srcDir) { m_builder.addSourceTree(new File(srcDir)); }