Java Code Examples for codemining.java.codeutils.JavaASTExtractor#getAST()

The following examples show how to use codemining.java.codeutils.JavaASTExtractor#getAST() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavaMethodClassCounter.java    From tassal with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void countMethodsClasses(final File projectDir)
		throws IOException {

	System.out.println("\n===== Project " + projectDir);
	final MethodClassCountVisitor mccv = new MethodClassCountVisitor();
	final JavaASTExtractor astExtractor = new JavaASTExtractor(false);

	final List<File> files = (List<File>) FileUtils.listFiles(projectDir,
			new String[] { "java" }, true);

	int count = 0;
	for (final File file : files) {

		final CompilationUnit cu = astExtractor.getAST(file);
		cu.accept(mccv);

		if (count % 1000 == 0)
			System.out.println("At file " + count + " of " + files.size());
		count++;
	}

	System.out.println("Project " + projectDir);
	System.out.println("No. *.java files " + files.size());
	System.out.println("No. Methods: " + mccv.noMethods);
	System.out.println("No. Classes: " + mccv.noClasses);
}
 
Example 2
Source File: JavaTypeTokenizer.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public SortedMap<Integer, FullToken> tokenListWithPos(final File f)
		throws IOException {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(FileUtils.readFileToString(f)
					.toCharArray());

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	ASTNode cu;
	try {
		cu = ex.getAST(f);
		return doApproximateTypeInference(tokens, cu);
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example 3
Source File: JavaTypeTokenizer.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public List<String> tokenListFromCode(final File codeFile)
		throws IOException {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(FileUtils.readFileToString(codeFile)
					.toCharArray());

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final ASTNode cu = ex.getAST(codeFile);
	final JavaApproximateTypeInferencer tInf = new JavaApproximateTypeInferencer(
			cu);
	tInf.infer();
	final Map<Integer, String> types = tInf.getVariableTypesAtPosition();

	final List<String> typeTokenList = Lists.newArrayList();
	for (final Entry<Integer, FullToken> token : tokens.entrySet()) {
		final String type = types.get(token.getKey());
		if (type != null) {
			typeTokenList.add("var%" + type + "%");
		} else {
			typeTokenList.add(token.getValue().token);
		}
	}
	return typeTokenList;
}
 
Example 4
Source File: JavaMethodClassCounter.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void countMethodsClasses(final File projectDir)
		throws IOException {

	System.out.println("\n===== Project " + projectDir);
	final MethodClassCountVisitor mccv = new MethodClassCountVisitor();
	final JavaASTExtractor astExtractor = new JavaASTExtractor(false);

	final List<File> files = (List<File>) FileUtils.listFiles(projectDir,
			new String[] { "java" }, true);

	int count = 0;
	for (final File file : files) {

		final CompilationUnit cu = astExtractor.getAST(file);
		cu.accept(mccv);

		if (count % 1000 == 0)
			System.out.println("At file " + count + " of " + files.size());
		count++;
	}

	System.out.println("Project " + projectDir);
	System.out.println("No. *.java files " + files.size());
	System.out.println("No. Methods: " + mccv.noMethods);
	System.out.println("No. Classes: " + mccv.noClasses);
}
 
Example 5
Source File: JavaTypeTokenizer.java    From tassal with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public SortedMap<Integer, FullToken> tokenListWithPos(final File f)
		throws IOException {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(FileUtils.readFileToString(f)
					.toCharArray());

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	ASTNode cu;
	try {
		cu = ex.getAST(f);
		return doApproximateTypeInference(tokens, cu);
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example 6
Source File: JavaTypeTokenizer.java    From tassal with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public List<String> tokenListFromCode(final File codeFile)
		throws IOException {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(FileUtils.readFileToString(codeFile)
					.toCharArray());

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final ASTNode cu = ex.getAST(codeFile);
	final JavaApproximateTypeInferencer tInf = new JavaApproximateTypeInferencer(
			cu);
	tInf.infer();
	final Map<Integer, String> types = tInf.getVariableTypesAtPosition();

	final List<String> typeTokenList = Lists.newArrayList();
	for (final Entry<Integer, FullToken> token : tokens.entrySet()) {
		final String type = types.get(token.getKey());
		if (type != null) {
			typeTokenList.add("var%" + type + "%");
		} else {
			typeTokenList.add(token.getValue().token);
		}
	}
	return typeTokenList;
}
 
Example 7
Source File: JavaVariableNameTypeDistribution.java    From naturalize with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static JavaVariableNameTypeDistribution buildFromFiles(
		final Collection<File> files) {
	final JavaVariableNameTypeDistribution tp = new JavaVariableNameTypeDistribution();
	for (final File f : files) {
		try {
			final JavaASTExtractor ex = new JavaASTExtractor(false);
			final CompilationUnit cu = ex.getAST(f);
			final JavaApproximateTypeInferencer typeInf = new JavaApproximateTypeInferencer(
					cu);
			typeInf.infer();
			final Map<String, String> varTypes = typeInf.getVariableTypes();
			for (final Entry<String, String> variable : varTypes.entrySet()) {
				tp.typePrior.addElement(variable.getKey(),
						variable.getValue());
			}
		} catch (final IOException e) {
			LOGGER.warning(ExceptionUtils.getFullStackTrace(e));
		}
	}

	return tp;
}
 
Example 8
Source File: JavaTypeTokenizer.java    From api-mining with GNU General Public License v3.0 6 votes vote down vote up
@Override
public SortedMap<Integer, FullToken> tokenListWithPos(final File f)
		throws IOException {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(FileUtils.readFileToString(f)
					.toCharArray());

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	ASTNode cu;
	try {
		cu = ex.getAST(f);
		return doApproximateTypeInference(tokens, cu);
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example 9
Source File: JavaTypeTokenizer.java    From api-mining with GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<String> tokenListFromCode(final File codeFile)
		throws IOException {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(FileUtils.readFileToString(codeFile)
					.toCharArray());

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final ASTNode cu = ex.getAST(codeFile);
	final JavaApproximateTypeInferencer tInf = new JavaApproximateTypeInferencer(
			cu);
	tInf.infer();
	final Map<Integer, String> types = tInf.getVariableTypesAtPosition();

	final List<String> typeTokenList = Lists.newArrayList();
	for (final Entry<Integer, FullToken> token : tokens.entrySet()) {
		final String type = types.get(token.getKey());
		if (type != null) {
			typeTokenList.add("var%" + type + "%");
		} else {
			typeTokenList.add(token.getValue().token);
		}
	}
	return typeTokenList;
}
 
Example 10
Source File: JavaMethodClassCounter.java    From api-mining with GNU General Public License v3.0 6 votes vote down vote up
public static void countMethodsClasses(final File projectDir)
		throws IOException {

	System.out.println("\n===== Project " + projectDir);
	final MethodClassCountVisitor mccv = new MethodClassCountVisitor();
	final JavaASTExtractor astExtractor = new JavaASTExtractor(false);

	final List<File> files = (List<File>) FileUtils.listFiles(projectDir,
			new String[] { "java" }, true);

	int count = 0;
	for (final File file : files) {

		final CompilationUnit cu = astExtractor.getAST(file);
		cu.accept(mccv);

		if (count % 1000 == 0)
			System.out.println("At file " + count + " of " + files.size());
		count++;
	}

	System.out.println("Project " + projectDir);
	System.out.println("No. *.java files " + files.size());
	System.out.println("No. Methods: " + mccv.noMethods);
	System.out.println("No. Classes: " + mccv.noClasses);
}
 
Example 11
Source File: CodeUtils.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get AST for source file
 *
 * @author Jaroslav Fowkes
 */
public static CompilationUnit getAST(final File fin) {

	CompilationUnit cu = null;
	final JavaASTExtractor ext = new JavaASTExtractor(false, true);
	try {
		cu = ext.getAST(fin);
	} catch (final Exception exc) {
		System.out.println("=+=+=+=+= AST Parse " + exc);
	}
	return cu;
}
 
Example 12
Source File: MethodRetriever.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Map<String, MethodDeclaration> getMethodNodes(final File file)
		throws IOException {
	final JavaASTExtractor astExtractor = new JavaASTExtractor(false);
	final MethodRetriever m = new MethodRetriever();
	final CompilationUnit cu = astExtractor.getAST(file);
	cu.accept(m);
	return m.methods;
}
 
Example 13
Source File: MethodRetriever.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
public static Map<String, MethodDeclaration> getMethodNodes(final File file)
		throws IOException {
	final JavaASTExtractor astExtractor = new JavaASTExtractor(false);
	final MethodRetriever m = new MethodRetriever();
	final CompilationUnit cu = astExtractor.getAST(file);
	cu.accept(m);
	return m.methods;
}
 
Example 14
Source File: JavaTypeTokenizer.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<FullToken> getTokenListFromCode(final File codeFile)
		throws IOException {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(FileUtils.readFileToString(codeFile)
					.toCharArray());

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final ASTNode cu = ex.getAST(codeFile);
	return getTypedTokens(tokens, cu);
}
 
Example 15
Source File: ASTVisitors.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get AST for source file
 *
 * @author Jaroslav Fowkes
 */
public static CompilationUnit getAST(final File fin) {

	CompilationUnit cu = null;
	final JavaASTExtractor ext = new JavaASTExtractor(false, true);
	try {
		cu = ext.getAST(fin);
	} catch (final Exception exc) {
		System.out.println("=+=+=+=+= AST Parse " + exc);
	}
	return cu;
}
 
Example 16
Source File: MethodRetriever.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Map<String, MethodDeclaration> getMethodNodes(final File file)
		throws IOException {
	final JavaASTExtractor astExtractor = new JavaASTExtractor(false);
	final MethodRetriever m = new MethodRetriever();
	final CompilationUnit cu = astExtractor.getAST(file);
	cu.accept(m);
	return m.methods;
}
 
Example 17
Source File: JavaTypeTokenizer.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<FullToken> getTokenListFromCode(final File codeFile)
		throws IOException {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(FileUtils.readFileToString(codeFile)
					.toCharArray());

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final ASTNode cu = ex.getAST(codeFile);
	return getTypedTokens(tokens, cu);
}
 
Example 18
Source File: JavaTypeTokenizer.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<FullToken> getTokenListFromCode(final File codeFile)
		throws IOException {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(FileUtils.readFileToString(codeFile)
					.toCharArray());

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final ASTNode cu = ex.getAST(codeFile);
	return getTypedTokens(tokens, cu);
}