codemining.java.codeutils.JavaASTExtractor Java Examples

The following examples show how to use codemining.java.codeutils.JavaASTExtractor. 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 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 #2
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 #3
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 #4
Source File: JunkVariableRenamer.java    From naturalize with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @param percentToRename
 * @param inputFile
 * @return
 * @throws Exception
 * @deprecated this is not fair for small files.
 */
@Deprecated
public String renameAllVarsInFile(final double percentToRename,
		final File inputFile) throws Exception {

	String file = FileUtils.readFileToString(inputFile);
	final Multimap<ASTNode, Variable> vars = VariableScopeExtractor
			.getVariableScopes(inputFile);
	final int cnt = (int) (vars.size() * percentToRename);

	for (int i = 0; i < cnt; i++) {
		file = renameSingleVariablesToJunk(file);
	}

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	return ex.getBestEffortAstNode(file).toString();
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #13
Source File: ScopedIdentifierRenaming.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String getFormattedRenamedCode(final String originalScopeCode,
		final String from, final String to, final String wholeFile) {
	final String code = getRenamedCode(originalScopeCode, from, to,
			wholeFile);
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	return ex.getASTNode(code, parseKindToUseOnOriginal).toString();
}
 
Example #14
Source File: ScopedIdentifierRenaming.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param originalScopeCode
 * @param wholeFile
 * @param varMapping
 * @return
 */
public String getRenamedCode(final String originalScopeCode,
		final String wholeFile, final Map<String, String> varMapping) {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final String originalCode = renameVariableInSnippet(
			ex.getASTNode(wholeFile, parseKindToUseOnOriginal).toString(),
			Collections.EMPTY_MAP);
	final String snippetToBeReplaced = renameVariableInSnippet(
			originalScopeCode, varMapping);

	final String code = originalCode.replace(
			renameVariableInSnippet(originalScopeCode,
					Collections.EMPTY_MAP), snippetToBeReplaced);
	return code;
}
 
Example #15
Source File: DynamicRangeEval.java    From naturalize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void pushStatsFor(final BaseIdentifierRenamings renamer,
		final MethodDeclaration method, final AbstractNGramLM lm,
		final List<String> allToks) throws IOException {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final Multimap<Scope, String> scopes = scopeExtractor
			.getFromNode(method);
	final ScopedIdentifierRenaming identifierRenamer = new ScopedIdentifierRenaming(
			scopeExtractor, ParseType.METHOD);
	final List<String> renamedVars = Lists.newArrayList(scopes.values());
	Collections.shuffle(renamedVars);

	checkArgument(!renamedVars.isEmpty());

	final SnippetSuggestions ssBefore = SnippetScorer.scoreSnippet(method,
			renamer, scopeExtractor, false, false);

	final Map<String, String> renamingPlan = Maps.newTreeMap();
	final String targetName = getRandomName(allToks, renamer.getLM()
			.getTokenizer());
	renamingPlan.put(renamedVars.get(0), targetName);

	final String renamedMethod = identifierRenamer.getRenamedCode(
			method.toString(), method.toString(), renamingPlan);
	final ASTNode renamedMethodNode = ex.getASTNode(renamedMethod,
			ParseType.METHOD);

	final SnippetSuggestions ssAfter = SnippetScorer.scoreSnippet(
			renamedMethodNode, renamer, scopeExtractor, false, false);

	scoreBest.add(new BeforeAfterScore<Double>(-ssBefore.suggestions
			.first().getConfidence(), -ssAfter.suggestions.first()
			.getConfidence()));

	if (DEBUG_OUTPUT) {
		printDebugOutput(method, lm, renamedVars, ssBefore, targetName,
				renamedMethodNode, ssAfter);
	}
}
 
Example #16
Source File: AbstractJavaNameBindingsExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ResolvedSourceCode getResolvedSourceCode(final String code) {
	final JavaASTExtractor ex = createExtractor();
	try {
		return getResolvedSourceCode(code,
				getNameBindings(ex.getBestEffortAstNode(code)),
				"UnkownSourceFile");
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example #17
Source File: JavaASTAnnotatedTokenizer.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public SortedMap<Integer, AstAnnotatedToken> getAnnotatedTokens(
		final char[] code) {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final ASTNode cu = ex.getASTNode(code, ParseType.COMPILATION_UNIT);

	final SortedMap<Integer, FullToken> baseTokens = baseTokenizer
			.fullTokenListWithPos(code);
	final TokenDecorator dec = new TokenDecorator(baseTokens);
	final SortedMap<Integer, AstAnnotatedToken> annotatedTokens = dec
			.getAnnotatedTokens(cu);
	return annotatedTokens;
}
 
Example #18
Source File: ScopedIdentifierRenaming.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String getFormattedRenamedCode(final String originalScopeCode,
		final String from, final String to, final String wholeFile) {
	final String code = getRenamedCode(originalScopeCode, from, to,
			wholeFile);
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	return ex.getASTNode(code, parseKindToUseOnOriginal).toString();
}
 
Example #19
Source File: JavaASTAnnotatedTokenizer.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@Override
public SortedMap<Integer, AstAnnotatedToken> getAnnotatedTokens(
		final char[] code) {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final ASTNode cu = ex.getASTNode(code, ParseType.COMPILATION_UNIT);

	final SortedMap<Integer, FullToken> baseTokens = baseTokenizer
			.fullTokenListWithPos(code);
	final TokenDecorator dec = new TokenDecorator(baseTokens);
	final SortedMap<Integer, AstAnnotatedToken> annotatedTokens = dec
			.getAnnotatedTokens(cu);
	return annotatedTokens;
}
 
Example #20
Source File: ScopedIdentifierRenaming.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param originalScopeCode
 * @param wholeFile
 * @param varMapping
 * @return
 */
public String getRenamedCode(final String originalScopeCode,
		final String wholeFile, final Map<String, String> varMapping) {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final String originalCode = renameVariableInSnippet(
			ex.getASTNode(wholeFile, parseKindToUseOnOriginal).toString(),
			Collections.EMPTY_MAP);
	final String snippetToBeReplaced = renameVariableInSnippet(
			originalScopeCode, varMapping);

	final String code = originalCode.replace(
			renameVariableInSnippet(originalScopeCode,
					Collections.EMPTY_MAP), snippetToBeReplaced);
	return code;
}
 
Example #21
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 char[] code) {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(code);

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	ASTNode cu;
	try {
		cu = ex.getBestEffortAstNode(code);
		return getTypedTokens(tokens, cu);
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}

}
 
Example #22
Source File: JavaASTAnnotatedTokenizer.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public SortedMap<Integer, AstAnnotatedToken> getAnnotatedTokens(
		final char[] code) {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	final ASTNode cu = ex.getASTNode(code, ParseType.COMPILATION_UNIT);

	final SortedMap<Integer, FullToken> baseTokens = baseTokenizer
			.fullTokenListWithPos(code);
	final TokenDecorator dec = new TokenDecorator(baseTokens);
	final SortedMap<Integer, AstAnnotatedToken> annotatedTokens = dec
			.getAnnotatedTokens(cu);
	return annotatedTokens;
}
 
Example #23
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 #24
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 char[] code) {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(code);

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	ASTNode cu;
	try {
		cu = ex.getBestEffortAstNode(code);
		return getTypedTokens(tokens, cu);
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}

}
 
Example #25
Source File: JavaTypeTokenizer.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public SortedMap<Integer, FullToken> fullTokenListWithPos(final char[] code) {
	final SortedMap<Integer, FullToken> tokens = baseTokenizer
			.fullTokenListWithPos(code);

	final JavaASTExtractor ex = new JavaASTExtractor(false);
	ASTNode cu;
	try {
		cu = ex.getBestEffortAstNode(code);
		return doApproximateTypeInference(tokens, cu);
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example #26
Source File: AbstractJavaNameBindingsExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public List<TokenNameBinding> getNameBindings(final String code) {
	final JavaASTExtractor ex = createExtractor();
	try {
		return getNameBindings(ex.getBestEffortAstNode(code), code);
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example #27
Source File: AbstractJavaNameBindingsExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ResolvedSourceCode getResolvedSourceCode(final File f)
		throws IOException {
	final JavaASTExtractor ex = createExtractor();
	return getResolvedSourceCode(FileUtils.readFileToString(f),
			getNameBindings(ex.getAST(f)), f.getAbsolutePath());
}
 
Example #28
Source File: AbstractJavaNameBindingsExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ResolvedSourceCode getResolvedSourceCode(final String code) {
	final JavaASTExtractor ex = createExtractor();
	try {
		return getResolvedSourceCode(code,
				getNameBindings(ex.getBestEffortAstNode(code)),
				"UnkownSourceFile");
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example #29
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 #30
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 String file) throws Exception {
	final JavaASTExtractor astExtractor = new JavaASTExtractor(false);
	final MethodRetriever m = new MethodRetriever();
	final ASTNode cu = astExtractor.getBestEffortAstNode(file);
	cu.accept(m);
	return m.methods;
}