codemining.languagetools.bindings.ResolvedSourceCode Java Examples

The following examples show how to use codemining.languagetools.bindings.ResolvedSourceCode. 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: JavaBindingsToJson.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
protected SerializableResolvedSourceCode(final ResolvedSourceCode rsc) {
	codeTokens = rsc.codeTokens;
	boundVariables = Lists.newArrayList();
	boundVariableFeatures = Lists.newArrayList();
	for (final TokenNameBinding binding : rsc.getAllBindings()) {
		boundVariables.add(new ArrayList<Integer>(binding.nameIndexes));
		boundVariableFeatures.add(new ArrayList<String>(
				binding.features));
	}
	provenance = rsc.name;
}
 
Example #2
Source File: AbstractJavascriptNameBindingsExtractor.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 JavascriptASTExtractor ex = createExtractor();
	try {
		return getResolvedSourceCode(code,
				getNameBindings(ex.getCompilationUnitAstNode(code)));
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example #3
Source File: AbstractJavascriptNameBindingsExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public ResolvedSourceCode getResolvedSourceCode(final File f)
		throws IOException {
	final JavascriptASTExtractor ex = createExtractor();
	return getResolvedSourceCode(FileUtils.readFileToString(f),
			getNameBindings(ex.getAST(f)));
}
 
Example #4
Source File: AbstractJavascriptNameBindingsExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static ResolvedSourceCode getResolvedSourceCode(
		final String sourceCode, final Set<Set<ASTNode>> nodeBindings) {
	final JavascriptTokenizer tokenizer = new JavascriptTokenizer();
	final SortedMap<Integer, String> tokenPositions = tokenizer
			.tokenListWithPos(sourceCode.toCharArray());
	final SortedMap<Integer, Integer> positionToIndex = getTokenIndexForPostion(tokenPositions);
	final List<String> tokens = Lists.newArrayList(tokenPositions.values());

	final ArrayListMultimap<String, TokenNameBinding> bindings = ArrayListMultimap
			.create();

	for (final Set<ASTNode> boundName : nodeBindings) {
		if (boundName.isEmpty()) {
			continue;
		}
		final List<Integer> boundPositions = Lists.newArrayList();
		for (final ASTNode name : boundName) {
			// Convert position to token index and add
			final int tokenIdx = positionToIndex.get(name
					.getStartPosition());
			boundPositions.add(tokenIdx);
		}
		bindings.put(tokens.get(boundPositions.get(0)),
				new TokenNameBinding(Sets.newTreeSet(boundPositions),
						tokens, getFeatures(boundName)));
	}

	return new ResolvedSourceCode(tokens, bindings);
}
 
Example #5
Source File: AbstractJavaNameBindingsExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ResolvedSourceCode getResolvedSourceCode(final String sourceCode,
		final Set<Set<ASTNode>> nodeBindings, final String filename,
		final Predicate<ASTNode> includeNode) {
	final SortedMap<Integer, String> tokenPositions = tokenizer
			.tokenListWithPos(sourceCode.toCharArray());
	final SortedMap<Integer, Integer> positionToIndex = getTokenIndexForPostion(tokenPositions);
	final List<String> tokens = Lists.newArrayList(tokenPositions.values());

	final ArrayListMultimap<String, TokenNameBinding> bindings = ArrayListMultimap
			.create();

	for (final Set<ASTNode> boundName : nodeBindings) {
		if (boundName.isEmpty()
				|| boundName.stream().noneMatch(includeNode)) {
			continue;
		}
		final List<Integer> boundPositions = Lists.newArrayList();
		for (final ASTNode name : boundName) {
			// Convert position to token index and add
			final int tokenIdx = positionToIndex.get(name
					.getStartPosition());
			boundPositions.add(tokenIdx);
		}
		bindings.put(tokens.get(boundPositions.get(0)),
				new TokenNameBinding(Sets.newTreeSet(boundPositions),
						tokens, getFeatures(boundName)));
	}

	return new ResolvedSourceCode(filename, tokens, bindings);
}
 
Example #6
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 #7
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 File f)
		throws IOException {
	final JavaASTExtractor ex = createExtractor();
	return getResolvedSourceCode(FileUtils.readFileToString(f),
			getNameBindings(ex.getAST(f)), f.getAbsolutePath());
}
 
Example #8
Source File: JavaBindingsToJson.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static ResolvedSourceCode getResolvedCode(final File f,
		final AbstractJavaNameBindingsExtractor extractor) {
	try {
		return extractor.getResolvedSourceCode(f);
	} catch (final Throwable t) {
		LOGGER.warning("Error for file " + f + ": "
				+ ExceptionUtils.getFullStackTrace(t));
	}
	return null;
}
 
Example #9
Source File: JavaBindingsToJson.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected SerializableResolvedSourceCode(final ResolvedSourceCode rsc) {
	codeTokens = rsc.codeTokens;
	boundVariables = Lists.newArrayList();
	boundVariableFeatures = Lists.newArrayList();
	for (final TokenNameBinding binding : rsc.getAllBindings()) {
		boundVariables.add(new ArrayList<Integer>(binding.nameIndexes));
		boundVariableFeatures.add(new ArrayList<String>(
				binding.features));
	}
	provenance = rsc.name;
}
 
Example #10
Source File: AbstractJavascriptNameBindingsExtractor.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 JavascriptASTExtractor ex = createExtractor();
	try {
		return getResolvedSourceCode(code,
				getNameBindings(ex.getCompilationUnitAstNode(code)));
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example #11
Source File: AbstractJavascriptNameBindingsExtractor.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 JavascriptASTExtractor ex = createExtractor();
	return getResolvedSourceCode(FileUtils.readFileToString(f),
			getNameBindings(ex.getAST(f)));
}
 
Example #12
Source File: AbstractJavascriptNameBindingsExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static ResolvedSourceCode getResolvedSourceCode(
		final String sourceCode, final Set<Set<ASTNode>> nodeBindings) {
	final JavascriptTokenizer tokenizer = new JavascriptTokenizer();
	final SortedMap<Integer, String> tokenPositions = tokenizer
			.tokenListWithPos(sourceCode.toCharArray());
	final SortedMap<Integer, Integer> positionToIndex = getTokenIndexForPostion(tokenPositions);
	final List<String> tokens = Lists.newArrayList(tokenPositions.values());

	final ArrayListMultimap<String, TokenNameBinding> bindings = ArrayListMultimap
			.create();

	for (final Set<ASTNode> boundName : nodeBindings) {
		if (boundName.isEmpty()) {
			continue;
		}
		final List<Integer> boundPositions = Lists.newArrayList();
		for (final ASTNode name : boundName) {
			// Convert position to token index and add
			final int tokenIdx = positionToIndex.get(name
					.getStartPosition());
			boundPositions.add(tokenIdx);
		}
		bindings.put(tokens.get(boundPositions.get(0)),
				new TokenNameBinding(Sets.newTreeSet(boundPositions),
						tokens, getFeatures(boundName)));
	}

	return new ResolvedSourceCode(tokens, bindings);
}
 
Example #13
Source File: AbstractJavaNameBindingsExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ResolvedSourceCode getResolvedSourceCode(final String sourceCode,
		final Set<Set<ASTNode>> nodeBindings, final String filename,
		final Predicate<ASTNode> includeNode) {
	final SortedMap<Integer, String> tokenPositions = tokenizer
			.tokenListWithPos(sourceCode.toCharArray());
	final SortedMap<Integer, Integer> positionToIndex = getTokenIndexForPostion(tokenPositions);
	final List<String> tokens = Lists.newArrayList(tokenPositions.values());

	final ArrayListMultimap<String, TokenNameBinding> bindings = ArrayListMultimap
			.create();

	for (final Set<ASTNode> boundName : nodeBindings) {
		if (boundName.isEmpty()
				|| boundName.stream().noneMatch(includeNode)) {
			continue;
		}
		final List<Integer> boundPositions = Lists.newArrayList();
		for (final ASTNode name : boundName) {
			// Convert position to token index and add
			final int tokenIdx = positionToIndex.get(name
					.getStartPosition());
			boundPositions.add(tokenIdx);
		}
		bindings.put(tokens.get(boundPositions.get(0)),
				new TokenNameBinding(Sets.newTreeSet(boundPositions),
						tokens, getFeatures(boundName)));
	}

	return new ResolvedSourceCode(filename, tokens, bindings);
}
 
Example #14
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 #15
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 #16
Source File: JavaBindingsToJson.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static ResolvedSourceCode getResolvedCode(final File f,
		final AbstractJavaNameBindingsExtractor extractor) {
	try {
		return extractor.getResolvedSourceCode(f);
	} catch (final Throwable t) {
		LOGGER.warning("Error for file " + f + ": "
				+ ExceptionUtils.getFullStackTrace(t));
	}
	return null;
}
 
Example #17
Source File: JavaBindingsToJson.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected SerializableResolvedSourceCode(final ResolvedSourceCode rsc) {
	codeTokens = rsc.codeTokens;
	boundVariables = Lists.newArrayList();
	boundVariableFeatures = Lists.newArrayList();
	for (final TokenNameBinding binding : rsc.getAllBindings()) {
		boundVariables.add(new ArrayList<Integer>(binding.nameIndexes));
		boundVariableFeatures.add(new ArrayList<String>(
				binding.features));
	}
	provenance = rsc.name;
}
 
Example #18
Source File: JavaBindingsToJson.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
public static ResolvedSourceCode getResolvedCode(final File f,
		final AbstractJavaNameBindingsExtractor extractor) {
	try {
		return extractor.getResolvedSourceCode(f);
	} catch (final Throwable t) {
		LOGGER.warning("Error for file " + f + ": "
				+ ExceptionUtils.getFullStackTrace(t));
	}
	return null;
}
 
Example #19
Source File: AbstractJavaNameBindingsExtractor.java    From api-mining with GNU General Public License v3.0 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 #20
Source File: RenamingDatasetExtractor.java    From naturalize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void visitCommitFiles(final RevCommit commit) {
	System.out.println("Visiting " + commit + " for renamings");
	final Collection<Renaming> renamingsAtThisPoint = renamings
			.get(commit.name());
	for (final Renaming renaming : renamingsAtThisPoint) {
		// Get the file
		final File currentFile = new File(
				repositoryDir.getAbsolutePath() + renaming.filename);
		checkArgument(currentFile.exists());

		try {
			ResolvedSourceCode resolvedCode = variableExtractor
					.getResolvedSourceCode(currentFile,
							n -> matchRenaming(renaming, n));
			if (!resolvedCode.getAllBindings().isEmpty()) {
				renamedVariablesDatapoints
						.add(RenamedSerializableResolvedSourceCode
								.fromResolvedSourceCode(
										resolvedCode,
										Lists.newArrayList(renaming.nameBefore)));
			} else {
				resolvedCode = methodDeclExtractor
						.getResolvedSourceCode(currentFile,
								n -> matchRenaming(renaming, n));
				if (!resolvedCode.getAllBindings().isEmpty()) {
					renamedMethodDeclarationsDatapoints
							.add(RenamedSerializableResolvedSourceCode.fromResolvedSourceCode(
									resolvedCode,
									Lists.newArrayList(renaming.nameBefore)));
				}
			}
		} catch (final IOException e) {
			// File always exists, since we checked above.
			throw new IllegalStateException(e);
		}
	}
}
 
Example #21
Source File: AbstractJavascriptNameBindingsExtractor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ResolvedSourceCode getResolvedSourceCode(final String code) {
	final JavascriptASTExtractor ex = createExtractor();
	try {
		return getResolvedSourceCode(code,
				getNameBindings(ex.getCompilationUnitAstNode(code)));
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example #22
Source File: AbstractJavascriptNameBindingsExtractor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ResolvedSourceCode getResolvedSourceCode(final File f)
		throws IOException {
	final JavascriptASTExtractor ex = createExtractor();
	return getResolvedSourceCode(FileUtils.readFileToString(f),
			getNameBindings(ex.getAST(f)));
}
 
Example #23
Source File: AbstractJavaNameBindingsExtractor.java    From api-mining with GNU General Public License v3.0 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 #24
Source File: AbstractJavascriptNameBindingsExtractor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
public static ResolvedSourceCode getResolvedSourceCode(
		final String sourceCode, final Set<Set<ASTNode>> nodeBindings) {
	final JavascriptTokenizer tokenizer = new JavascriptTokenizer();
	final SortedMap<Integer, String> tokenPositions = tokenizer
			.tokenListWithPos(sourceCode.toCharArray());
	final SortedMap<Integer, Integer> positionToIndex = getTokenIndexForPostion(tokenPositions);
	final List<String> tokens = Lists.newArrayList(tokenPositions.values());

	final ArrayListMultimap<String, TokenNameBinding> bindings = ArrayListMultimap
			.create();

	for (final Set<ASTNode> boundName : nodeBindings) {
		if (boundName.isEmpty()) {
			continue;
		}
		final List<Integer> boundPositions = Lists.newArrayList();
		for (final ASTNode name : boundName) {
			// Convert position to token index and add
			final int tokenIdx = positionToIndex.get(name
					.getStartPosition());
			boundPositions.add(tokenIdx);
		}
		bindings.put(tokens.get(boundPositions.get(0)),
				new TokenNameBinding(Sets.newTreeSet(boundPositions),
						tokens, getFeatures(boundName)));
	}

	return new ResolvedSourceCode(tokens, bindings);
}
 
Example #25
Source File: AbstractJavaNameBindingsExtractor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
public ResolvedSourceCode getResolvedSourceCode(final String sourceCode,
		final Set<Set<ASTNode>> nodeBindings, final String filename,
		final Predicate<ASTNode> includeNode) {
	final SortedMap<Integer, String> tokenPositions = tokenizer
			.tokenListWithPos(sourceCode.toCharArray());
	final SortedMap<Integer, Integer> positionToIndex = getTokenIndexForPostion(tokenPositions);
	final List<String> tokens = Lists.newArrayList(tokenPositions.values());

	final ArrayListMultimap<String, TokenNameBinding> bindings = ArrayListMultimap
			.create();

	for (final Set<ASTNode> boundName : nodeBindings) {
		if (boundName.isEmpty()
				|| boundName.stream().noneMatch(includeNode)) {
			continue;
		}
		final List<Integer> boundPositions = Lists.newArrayList();
		for (final ASTNode name : boundName) {
			// Convert position to token index and add
			final int tokenIdx = positionToIndex.get(name
					.getStartPosition());
			boundPositions.add(tokenIdx);
		}
		bindings.put(tokens.get(boundPositions.get(0)),
				new TokenNameBinding(Sets.newTreeSet(boundPositions),
						tokens, getFeatures(boundName)));
	}

	return new ResolvedSourceCode(filename, tokens, bindings);
}
 
Example #26
Source File: JavaBindingsToJson.java    From api-mining with GNU General Public License v3.0 4 votes vote down vote up
public static SerializableResolvedSourceCode fromResolvedSourceCode(
		final ResolvedSourceCode rsc) {
	return new SerializableResolvedSourceCode(rsc);
}
 
Example #27
Source File: AbstractJavaNameBindingsExtractor.java    From api-mining with GNU General Public License v3.0 4 votes vote down vote up
public ResolvedSourceCode getResolvedSourceCode(final File f,
		final Predicate<ASTNode> includeNode) throws IOException {
	final JavaASTExtractor ex = createExtractor();
	return getResolvedSourceCode(FileUtils.readFileToString(f),
			getNameBindings(ex.getAST(f)), f.getAbsolutePath(), includeNode);
}
 
Example #28
Source File: AbstractJavaNameBindingsExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ResolvedSourceCode getResolvedSourceCode(final String sourceCode,
		final Set<Set<ASTNode>> nodeBindings, final String filename) {
	return getResolvedSourceCode(sourceCode, nodeBindings, filename,
			node -> true);
}
 
Example #29
Source File: AbstractJavaNameBindingsExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ResolvedSourceCode getResolvedSourceCode(final File f,
		final Predicate<ASTNode> includeNode) throws IOException {
	final JavaASTExtractor ex = createExtractor();
	return getResolvedSourceCode(FileUtils.readFileToString(f),
			getNameBindings(ex.getAST(f)), f.getAbsolutePath(), includeNode);
}
 
Example #30
Source File: AbstractJavaNameBindingsExtractor.java    From api-mining with GNU General Public License v3.0 4 votes vote down vote up
public ResolvedSourceCode getResolvedSourceCode(final String sourceCode,
		final Set<Set<ASTNode>> nodeBindings, final String filename) {
	return getResolvedSourceCode(sourceCode, nodeBindings, filename,
			node -> true);
}