Java Code Examples for codemining.languagetools.bindings.TokenNameBinding

The following examples show how to use codemining.languagetools.bindings.TokenNameBinding. These examples are extracted from open source projects. 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
/**
 * Get the token bindings given the ASTNode bindings and the source code
 * positions.
 *
 * @param sourceCode
 * @param nodeBindings
 * @return
 */
public List<TokenNameBinding> getTokenBindings(
		final SortedMap<Integer, String> tokenPositions,
		final Set<Set<ASTNode>> nodeBindings) {
	final SortedMap<Integer, Integer> positionToIndex = getTokenIndexForPostion(tokenPositions);
	final List<String> tokens = Lists.newArrayList(tokenPositions.values());

	final List<TokenNameBinding> bindings = Lists.newArrayList();

	for (final Set<ASTNode> boundName : nodeBindings) {
		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.add(new TokenNameBinding(Sets.newTreeSet(boundPositions),
				tokens, getFeatures(boundName)));
	}

	return bindings;
}
 
Example 2
/**
 * Get the token bindings given the ASTNode bindings and the source code
 * positions.
 *
 * @param sourceCode
 * @param nodeBindings
 * @return
 */
public static List<TokenNameBinding> getTokenBindings(
		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 List<TokenNameBinding> bindings = Lists.newArrayList();

	for (final Set<ASTNode> boundName : nodeBindings) {
		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.add(new TokenNameBinding(Sets.newTreeSet(boundPositions),
				tokens, getFeatures(boundName)));
	}

	return bindings;
}
 
Example 3
@Test
public void testClassBindings() throws IOException {
	final JavaApproximateVariableBindingExtractor jabe = new JavaApproximateVariableBindingExtractor();
	final JavaExactVariableBindingsExtractor jbe = new JavaExactVariableBindingsExtractor();

	final List<TokenNameBinding> classVariableBindings = jabe
			.getNameBindings(classContent);
	final List<TokenNameBinding> classVariableBindingsExact = jbe
			.getNameBindings(classContent);

	BindingTester
			.checkAllBindings(classVariableBindings);
	assertEquals(classVariableBindings.size(), 5);

	final List<TokenNameBinding> classVariableBindings2 = jabe
			.getNameBindings(classContent2);
	final List<TokenNameBinding> classVariableBindings2Exact = jbe
			.getNameBindings(classContent2);

	assertEquals(classVariableBindings2.size(), 9);

	allAreContained(classVariableBindingsExact, classVariableBindings);
	allAreContained(classVariableBindings2Exact, classVariableBindings2);
}
 
Example 4
@Test
public void testClassLevelBindings() throws IOException {
	final JavaTypeDeclarationBindingExtractor jame = new JavaTypeDeclarationBindingExtractor();

	final List<TokenNameBinding> classTypeindings = jame
			.getNameBindings(classContent);

	BindingTester.checkAllBindings(classTypeindings);
	assertEquals(classTypeindings.size(), 1);

	final List<TokenNameBinding> classTypeBindings2 = jame
			.getNameBindings(classContent2);
	BindingTester.checkAllBindings(classTypeBindings2);

	assertEquals(classTypeBindings2.size(), 1);
}
 
Example 5
@Test
public void testClassLevelBindings() throws IOException {
	final JavaMethodInvocationBindingExtractor jame = new JavaMethodInvocationBindingExtractor();

	final List<TokenNameBinding> classMethodBindings = jame
			.getNameBindings(classContent);

	BindingTester.checkAllBindings(classMethodBindings);
	assertEquals(classMethodBindings.size(), 7);

	final List<TokenNameBinding> classMethodBindings2 = jame
			.getNameBindings(classContent2);
	BindingTester.checkAllBindings(classMethodBindings2);

	assertEquals(classMethodBindings2.size(), 6);
}
 
Example 6
@Test
public void testClassBindings() throws IOException {
	final JavascriptApproximateVariableBindingExtractor jabe = new JavascriptApproximateVariableBindingExtractor();
	final JavascriptExactVariableBindingsExtractor jbe = new JavascriptExactVariableBindingsExtractor();

	final List<TokenNameBinding> classVariableBindings = jabe
			.getNameBindings(classContent);
	final List<TokenNameBinding> classVariableBindingsExact = jbe
			.getNameBindings(classContent);

	BindingTester.checkAllBindings(classVariableBindings);
	assertEquals(classVariableBindings.size(), 3);

	final List<TokenNameBinding> classVariableBindings2 = jabe
			.getNameBindings(classContent2);
	final List<TokenNameBinding> classVariableBindings2Exact = jbe
			.getNameBindings(classContent2);

	assertEquals(classVariableBindings2.size(), 13);

	allAreContained(classVariableBindingsExact, classVariableBindings);
	allAreContained(classVariableBindings2Exact, classVariableBindings2);
}
 
Example 7
/**
 * Get the token bindings given the ASTNode bindings and the source code
 * positions.
 *
 * @param sourceCode
 * @param nodeBindings
 * @return
 */
public List<TokenNameBinding> getTokenBindings(
		final SortedMap<Integer, String> tokenPositions,
		final Set<Set<ASTNode>> nodeBindings) {
	final SortedMap<Integer, Integer> positionToIndex = getTokenIndexForPostion(tokenPositions);
	final List<String> tokens = Lists.newArrayList(tokenPositions.values());

	final List<TokenNameBinding> bindings = Lists.newArrayList();

	for (final Set<ASTNode> boundName : nodeBindings) {
		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.add(new TokenNameBinding(Sets.newTreeSet(boundPositions),
				tokens, getFeatures(boundName)));
	}

	return bindings;
}
 
Example 8
/**
 * Get the token bindings given the ASTNode bindings and the source code
 * positions.
 *
 * @param sourceCode
 * @param nodeBindings
 * @return
 */
public static List<TokenNameBinding> getTokenBindings(
		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 List<TokenNameBinding> bindings = Lists.newArrayList();

	for (final Set<ASTNode> boundName : nodeBindings) {
		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.add(new TokenNameBinding(Sets.newTreeSet(boundPositions),
				tokens, getFeatures(boundName)));
	}

	return bindings;
}
 
Example 9
@Test
public void testClassBindings() throws IOException {
	final JavaApproximateVariableBindingExtractor jabe = new JavaApproximateVariableBindingExtractor();
	final JavaExactVariableBindingsExtractor jbe = new JavaExactVariableBindingsExtractor();

	final List<TokenNameBinding> classVariableBindings = jabe
			.getNameBindings(classContent);
	final List<TokenNameBinding> classVariableBindingsExact = jbe
			.getNameBindings(classContent);

	BindingTester
			.checkAllBindings(classVariableBindings);
	assertEquals(classVariableBindings.size(), 5);

	final List<TokenNameBinding> classVariableBindings2 = jabe
			.getNameBindings(classContent2);
	final List<TokenNameBinding> classVariableBindings2Exact = jbe
			.getNameBindings(classContent2);

	assertEquals(classVariableBindings2.size(), 9);

	allAreContained(classVariableBindingsExact, classVariableBindings);
	allAreContained(classVariableBindings2Exact, classVariableBindings2);
}
 
Example 10
@Test
public void testClassLevelBindings() throws IOException {
	final JavaTypeDeclarationBindingExtractor jame = new JavaTypeDeclarationBindingExtractor();

	final List<TokenNameBinding> classTypeindings = jame
			.getNameBindings(classContent);

	BindingTester.checkAllBindings(classTypeindings);
	assertEquals(classTypeindings.size(), 1);

	final List<TokenNameBinding> classTypeBindings2 = jame
			.getNameBindings(classContent2);
	BindingTester.checkAllBindings(classTypeBindings2);

	assertEquals(classTypeBindings2.size(), 1);
}
 
Example 11
@Test
public void testClassLevelBindings() throws IOException {
	final JavaMethodInvocationBindingExtractor jame = new JavaMethodInvocationBindingExtractor();

	final List<TokenNameBinding> classMethodBindings = jame
			.getNameBindings(classContent);

	BindingTester.checkAllBindings(classMethodBindings);
	assertEquals(classMethodBindings.size(), 7);

	final List<TokenNameBinding> classMethodBindings2 = jame
			.getNameBindings(classContent2);
	BindingTester.checkAllBindings(classMethodBindings2);

	assertEquals(classMethodBindings2.size(), 6);
}
 
Example 12
@Test
public void testClassBindings() throws IOException {
	final JavascriptApproximateVariableBindingExtractor jabe = new JavascriptApproximateVariableBindingExtractor();
	final JavascriptExactVariableBindingsExtractor jbe = new JavascriptExactVariableBindingsExtractor();

	final List<TokenNameBinding> classVariableBindings = jabe
			.getNameBindings(classContent);
	final List<TokenNameBinding> classVariableBindingsExact = jbe
			.getNameBindings(classContent);

	BindingTester.checkAllBindings(classVariableBindings);
	assertEquals(classVariableBindings.size(), 3);

	final List<TokenNameBinding> classVariableBindings2 = jabe
			.getNameBindings(classContent2);
	final List<TokenNameBinding> classVariableBindings2Exact = jbe
			.getNameBindings(classContent2);

	assertEquals(classVariableBindings2.size(), 13);

	allAreContained(classVariableBindingsExact, classVariableBindings);
	allAreContained(classVariableBindings2Exact, classVariableBindings2);
}
 
Example 13
/**
 * Get the token bindings given the ASTNode bindings and the source code
 * positions.
 *
 * @param sourceCode
 * @param nodeBindings
 * @return
 */
public List<TokenNameBinding> getTokenBindings(
		final SortedMap<Integer, String> tokenPositions,
		final Set<Set<ASTNode>> nodeBindings) {
	final SortedMap<Integer, Integer> positionToIndex = getTokenIndexForPostion(tokenPositions);
	final List<String> tokens = Lists.newArrayList(tokenPositions.values());

	final List<TokenNameBinding> bindings = Lists.newArrayList();

	for (final Set<ASTNode> boundName : nodeBindings) {
		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.add(new TokenNameBinding(Sets.newTreeSet(boundPositions),
				tokens, getFeatures(boundName)));
	}

	return bindings;
}
 
Example 14
/**
 * Get the token bindings given the ASTNode bindings and the source code
 * positions.
 *
 * @param sourceCode
 * @param nodeBindings
 * @return
 */
public static List<TokenNameBinding> getTokenBindings(
		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 List<TokenNameBinding> bindings = Lists.newArrayList();

	for (final Set<ASTNode> boundName : nodeBindings) {
		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.add(new TokenNameBinding(Sets.newTreeSet(boundPositions),
				tokens, getFeatures(boundName)));
	}

	return bindings;
}
 
Example 15
@Test
public void testClassBindings() throws IOException {
	final JavaApproximateVariableBindingExtractor jabe = new JavaApproximateVariableBindingExtractor();
	final JavaExactVariableBindingsExtractor jbe = new JavaExactVariableBindingsExtractor();

	final List<TokenNameBinding> classVariableBindings = jabe
			.getNameBindings(classContent);
	final List<TokenNameBinding> classVariableBindingsExact = jbe
			.getNameBindings(classContent);

	BindingTester
			.checkAllBindings(classVariableBindings);
	assertEquals(classVariableBindings.size(), 5);

	final List<TokenNameBinding> classVariableBindings2 = jabe
			.getNameBindings(classContent2);
	final List<TokenNameBinding> classVariableBindings2Exact = jbe
			.getNameBindings(classContent2);

	assertEquals(classVariableBindings2.size(), 9);

	allAreContained(classVariableBindingsExact, classVariableBindings);
	allAreContained(classVariableBindings2Exact, classVariableBindings2);
}
 
Example 16
@Test
public void testClassLevelBindings() throws IOException {
	final JavaTypeDeclarationBindingExtractor jame = new JavaTypeDeclarationBindingExtractor();

	final List<TokenNameBinding> classTypeindings = jame
			.getNameBindings(classContent);

	BindingTester.checkAllBindings(classTypeindings);
	assertEquals(classTypeindings.size(), 1);

	final List<TokenNameBinding> classTypeBindings2 = jame
			.getNameBindings(classContent2);
	BindingTester.checkAllBindings(classTypeBindings2);

	assertEquals(classTypeBindings2.size(), 1);
}
 
Example 17
@Test
public void testClassLevelBindings() throws IOException {
	final JavaMethodInvocationBindingExtractor jame = new JavaMethodInvocationBindingExtractor();

	final List<TokenNameBinding> classMethodBindings = jame
			.getNameBindings(classContent);

	BindingTester.checkAllBindings(classMethodBindings);
	assertEquals(classMethodBindings.size(), 7);

	final List<TokenNameBinding> classMethodBindings2 = jame
			.getNameBindings(classContent2);
	BindingTester.checkAllBindings(classMethodBindings2);

	assertEquals(classMethodBindings2.size(), 6);
}
 
Example 18
@Test
public void testClassBindings() throws IOException {
	final JavascriptApproximateVariableBindingExtractor jabe = new JavascriptApproximateVariableBindingExtractor();
	final JavascriptExactVariableBindingsExtractor jbe = new JavascriptExactVariableBindingsExtractor();

	final List<TokenNameBinding> classVariableBindings = jabe
			.getNameBindings(classContent);
	final List<TokenNameBinding> classVariableBindingsExact = jbe
			.getNameBindings(classContent);

	BindingTester.checkAllBindings(classVariableBindings);
	assertEquals(classVariableBindings.size(), 3);

	final List<TokenNameBinding> classVariableBindings2 = jabe
			.getNameBindings(classContent2);
	final List<TokenNameBinding> classVariableBindings2Exact = jbe
			.getNameBindings(classContent2);

	assertEquals(classVariableBindings2.size(), 13);

	allAreContained(classVariableBindingsExact, classVariableBindings);
	allAreContained(classVariableBindings2Exact, classVariableBindings2);
}
 
Example 19
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 20
public final List<TokenNameBinding> getNameBindings(final ASTNode node,
		final File file) throws IOException {
	final Set<Set<ASTNode>> nodeBindings = getNameBindings(node);
	final SortedMap<Integer, String> tokenPositions = Maps.transformValues(
			tokenizer.tokenListWithPos(file),
			FullToken.TOKEN_NAME_CONVERTER);
	return getTokenBindings(tokenPositions, nodeBindings);
}
 
Example 21
@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 22
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 23
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 24
@Override
public List<TokenNameBinding> getNameBindings(final String code) {
	final JavascriptASTExtractor ex = createExtractor();
	try {
		return getNameBindings(ex.getCompilationUnitAstNode(code), code);
	} catch (final Exception e) {
		throw new IllegalArgumentException(e);
	}
}
 
Example 25
Source Project: api-mining   Source File: BindingTester.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void checkAllBindings(final List<TokenNameBinding> bindings) {
	final Set<Integer> indexes = Sets.newHashSet();
	for (final TokenNameBinding binding : bindings) {
		BindingTester.checkBinding(binding);
		assertFalse("Indexes appear only once",
				indexes.removeAll(binding.nameIndexes));
		indexes.addAll(binding.nameIndexes);
	}
}
 
Example 26
Source Project: api-mining   Source File: BindingTester.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static void checkBinding(final TokenNameBinding binding) {
	final String tokenName = binding.sourceCodeTokens
			.get(binding.nameIndexes.iterator().next());
	for (final int idx : binding.nameIndexes) {
		assertEquals(tokenName, binding.sourceCodeTokens.get(idx));
	}
}
 
Example 27
@Test
public void testMethodBinding() {
	final JavaApproximateVariableBindingExtractor jabe = new JavaApproximateVariableBindingExtractor();
	final List<TokenNameBinding> methodVariableBindings = jabe
			.getNameBindings(methodContent);
	BindingTester
			.checkAllBindings(methodVariableBindings);
	assertEquals(methodVariableBindings.size(), 3);

}
 
Example 28
@Test
public void testClassBindings() throws IOException {
	final JavaExactVariableBindingsExtractor jbe = new JavaExactVariableBindingsExtractor();
	final List<TokenNameBinding> classVariableBindings = jbe
			.getNameBindings(classContent);
	BindingTester.checkAllBindings(classVariableBindings);
	assertEquals(classVariableBindings.size(), 5);

	final List<TokenNameBinding> classVariableBindings2 = jbe
			.getNameBindings(classContent2);

	assertEquals(classVariableBindings2.size(), 9);
}
 
Example 29
@Test
public void testMethodBinding() {
	final JavascriptApproximateVariableBindingExtractor jabe = new JavascriptApproximateVariableBindingExtractor();
	final List<TokenNameBinding> methodVariableBindings = jabe
			.getNameBindings(methodContent);
	BindingTester.checkAllBindings(methodVariableBindings);
	assertEquals(methodVariableBindings.size(), 1);

}
 
Example 30
@Test
public void testClassBindings() throws IOException {
	final JavascriptExactVariableBindingsExtractor jbe = new JavascriptExactVariableBindingsExtractor();
	final List<TokenNameBinding> classVariableBindings = jbe
			.getNameBindings(classContent);
	BindingTester.checkAllBindings(classVariableBindings);
	// assertEquals(classVariableBindings.size(), 1);

	final List<TokenNameBinding> classVariableBindings2 = jbe
			.getNameBindings(classContent2);

	// assertEquals(classVariableBindings2.size(), 3);
}