codemining.languagetools.ParseType Java Examples

The following examples show how to use codemining.languagetools.ParseType. 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: AllScopeExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Multimap<Scope, String> getFromString(final String file,
		final ParseType parseType) {
	final Multimap<Scope, String> scopes = TreeMultimap.create();
	for (final IScopeExtractor extractor : allExtractors) {
		scopes.putAll(extractor.getFromString(file, parseType));
	}
	return scopes;
}
 
Example #2
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 #3
Source File: AllScopeExtractor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Multimap<Scope, String> getFromString(final String file,
		final ParseType parseType) {
	final Multimap<Scope, String> scopes = TreeMultimap.create();
	for (final IScopeExtractor extractor : allExtractors) {
		scopes.putAll(extractor.getFromString(file, parseType));
	}
	return scopes;
}
 
Example #4
Source File: JavaASTExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the AST by making the best effort to guess the type of the node.
 *
 * @throws Exception
 */
public final ASTNode getBestEffortAstNode(final char[] content)
		throws Exception {
	for (final ParseType parseType : ParseType.values()) {
		final ASTNode node = getASTNode(content, parseType);
		if (normalizeCode(node.toString().toCharArray()).equals(
				normalizeCode(content))) {
			return node;
		}
	}
	throw new Exception(
			"Code snippet could not be recognized as any of the known types");
}
 
Example #5
Source File: JavaApproximateTypeInferencerTest.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test() {
	JavaASTExtractor ex = new JavaASTExtractor(false);
	JavaApproximateTypeInferencer jati = new JavaApproximateTypeInferencer(
			ex.getAST(classContent, ParseType.COMPILATION_UNIT));
	jati.infer();
	final Map<String, String> vars = jati.getVariableTypes();
	assertEquals(vars.get("anInstance"), "my.pack.SomeName");
	assertEquals(vars.get("arrayOfInt"), "int[]");
	assertEquals(vars.get("aNumber"), "long");
	assertEquals(vars.get("singleObject"), "your.pack.Blah");
	assertEquals(vars.get("arrayOfObjects"), "your.pack.Blah[]");
	assertEquals(vars.get("listOfInt"), "java.util.List<java.lang.Integer>");
	assertEquals(
			vars.get("complexParamType"),
			"java.util.Map<your.pack.Blah,java.util.Map<my.pack.SomeNameInPkg,java.util.List<java.lang.Double>>>");
	assertEquals(vars.get("paraType"),
			"your.pack2.ParamType<your.pack.Blah>");
	assertEquals(vars.get("lowerBoundPa"),
			"your.pack2.ParamType<? extends your.pack.Blah>");
	assertEquals(vars.get("upperBoundPa"),
			"your.pack2.ParamType<? super your.pack.Blah>");
	assertEquals(vars.get("upperBoundPa2"),
			"your.pack2.ParamType<? super java.util.List<? super your.pack.Blah>>");
	assertEquals(vars.get("e"),
			"java.io.IOException | java.lang.ArithmeticException");

}
 
Example #6
Source File: AllScopeExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Multimap<Scope, String> getFromString(final String file,
		final ParseType parseType) {
	final Multimap<Scope, String> scopes = TreeMultimap.create();
	for (final IScopeExtractor extractor : allExtractors) {
		scopes.putAll(extractor.getFromString(file, parseType));
	}
	return scopes;
}
 
Example #7
Source File: JavaASTExtractor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the AST by making the best effort to guess the type of the node.
 *
 * @throws Exception
 */
public final ASTNode getBestEffortAstNode(final char[] content)
		throws Exception {
	for (final ParseType parseType : ParseType.values()) {
		final ASTNode node = getASTNode(content, parseType);
		if (normalizeCode(node.toString().toCharArray()).equals(
				normalizeCode(content))) {
			return node;
		}
	}
	throw new Exception(
			"Code snippet could not be recognized as any of the known types");
}
 
Example #8
Source File: JavaAstExtractorTest.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link codemining.java.codeutils.JavaASTExtractor#getBestEffortAst(java.lang.String)}
 * .
 * 
 * @throws IOException
 */
@Test
public void testGetASTString() {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	assertTrue(classContent.length() > 0);
	final ASTNode classCU = ex.getASTNode(classContent,
			ParseType.COMPILATION_UNIT);
	assertTrue(snippetMatchesAstTokens(classContent, classCU));

	assertTrue(methodContent.length() > 0);
	final ASTNode methodCU = ex.getASTNode(methodContent,
			ParseType.METHOD);
	assertTrue(snippetMatchesAstTokens(methodContent, methodCU));
}
 
Example #9
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 #10
Source File: JavascriptASTExtractorTest.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link codemining.java.codeutils.JavaASTExtractor#getBestEffortAst(java.lang.String)}
 * .
 * 
 * @throws IOException
 */
@Test
public void testGetASTString() {
	final JavascriptASTExtractor ex = new JavascriptASTExtractor(false);
	assertTrue(classContent.length() > 0);
	final ASTNode classCU = ex.getASTNode(classContent,
			ParseType.COMPILATION_UNIT);
	// assertTrue(snippetMatchesAstTokens(classContent, classCU));

	assertTrue(methodContent.length() > 0);
	final ASTNode methodCU = ex.getASTNode(methodContent, ParseType.METHOD);
	// assertTrue(snippetMatchesAstTokens(methodContent, methodCU));
}
 
Example #11
Source File: JavaApproximateTypeInferencerTest.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void test() {
	JavaASTExtractor ex = new JavaASTExtractor(false);
	JavaApproximateTypeInferencer jati = new JavaApproximateTypeInferencer(
			ex.getAST(classContent, ParseType.COMPILATION_UNIT));
	jati.infer();
	final Map<String, String> vars = jati.getVariableTypes();
	assertEquals(vars.get("anInstance"), "my.pack.SomeName");
	assertEquals(vars.get("arrayOfInt"), "int[]");
	assertEquals(vars.get("aNumber"), "long");
	assertEquals(vars.get("singleObject"), "your.pack.Blah");
	assertEquals(vars.get("arrayOfObjects"), "your.pack.Blah[]");
	assertEquals(vars.get("listOfInt"), "java.util.List<java.lang.Integer>");
	assertEquals(
			vars.get("complexParamType"),
			"java.util.Map<your.pack.Blah,java.util.Map<my.pack.SomeNameInPkg,java.util.List<java.lang.Double>>>");
	assertEquals(vars.get("paraType"),
			"your.pack2.ParamType<your.pack.Blah>");
	assertEquals(vars.get("lowerBoundPa"),
			"your.pack2.ParamType<? extends your.pack.Blah>");
	assertEquals(vars.get("upperBoundPa"),
			"your.pack2.ParamType<? super your.pack.Blah>");
	assertEquals(vars.get("upperBoundPa2"),
			"your.pack2.ParamType<? super java.util.List<? super your.pack.Blah>>");
	assertEquals(vars.get("e"),
			"java.io.IOException | java.lang.ArithmeticException");

}
 
Example #12
Source File: JavaAstExtractorTest.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Test method for
 * {@link codemining.java.codeutils.JavaASTExtractor#getBestEffortAst(java.lang.String)}
 * .
 * 
 * @throws IOException
 */
@Test
public void testGetASTString() {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	assertTrue(classContent.length() > 0);
	final ASTNode classCU = ex.getASTNode(classContent,
			ParseType.COMPILATION_UNIT);
	assertTrue(snippetMatchesAstTokens(classContent, classCU));

	assertTrue(methodContent.length() > 0);
	final ASTNode methodCU = ex.getASTNode(methodContent,
			ParseType.METHOD);
	assertTrue(snippetMatchesAstTokens(methodContent, methodCU));
}
 
Example #13
Source File: JavascriptASTExtractorTest.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Test method for
 * {@link codemining.java.codeutils.JavaASTExtractor#getBestEffortAst(java.lang.String)}
 * .
 * 
 * @throws IOException
 */
@Test
public void testGetASTString() {
	final JavascriptASTExtractor ex = new JavascriptASTExtractor(false);
	assertTrue(classContent.length() > 0);
	final ASTNode classCU = ex.getASTNode(classContent,
			ParseType.COMPILATION_UNIT);
	// assertTrue(snippetMatchesAstTokens(classContent, classCU));

	assertTrue(methodContent.length() > 0);
	final ASTNode methodCU = ex.getASTNode(methodContent, ParseType.METHOD);
	// assertTrue(snippetMatchesAstTokens(methodContent, methodCU));
}
 
Example #14
Source File: JavascriptASTExtractorTest.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link codemining.java.codeutils.JavaASTExtractor#getBestEffortAst(java.lang.String)}
 * .
 * 
 * @throws IOException
 */
@Test
public void testGetASTString() {
	final JavascriptASTExtractor ex = new JavascriptASTExtractor(false);
	assertTrue(classContent.length() > 0);
	final ASTNode classCU = ex.getASTNode(classContent,
			ParseType.COMPILATION_UNIT);
	// assertTrue(snippetMatchesAstTokens(classContent, classCU));

	assertTrue(methodContent.length() > 0);
	final ASTNode methodCU = ex.getASTNode(methodContent, ParseType.METHOD);
	// assertTrue(snippetMatchesAstTokens(methodContent, methodCU));
}
 
Example #15
Source File: JavaAstExtractorTest.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test method for
 * {@link codemining.java.codeutils.JavaASTExtractor#getBestEffortAst(java.lang.String)}
 * .
 * 
 * @throws IOException
 */
@Test
public void testGetASTString() {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	assertTrue(classContent.length() > 0);
	final ASTNode classCU = ex.getASTNode(classContent,
			ParseType.COMPILATION_UNIT);
	assertTrue(snippetMatchesAstTokens(classContent, classCU));

	assertTrue(methodContent.length() > 0);
	final ASTNode methodCU = ex.getASTNode(methodContent,
			ParseType.METHOD);
	assertTrue(snippetMatchesAstTokens(methodContent, methodCU));
}
 
Example #16
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 #17
Source File: PerturbationEvaluator.java    From naturalize with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public PerturbationEvaluator(final File directory,
		final ITokenizer tokenizer, final IScopeExtractor scopeExtractor,
		final String renamerClass) {
	allFiles = FileUtils.listFiles(directory, tokenizer.getFileFilter(),
			DirectoryFileFilter.DIRECTORY);
	this.tokenizer = tokenizer;
	this.scopeExtractor = scopeExtractor;
	this.renamerClass = renamerClass;
	varRenamer = new ScopedIdentifierRenaming(scopeExtractor,
			ParseType.COMPILATION_UNIT);
}
 
Example #18
Source File: JavaApproximateTypeInferencerTest.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void test() {
	JavaASTExtractor ex = new JavaASTExtractor(false);
	JavaApproximateTypeInferencer jati = new JavaApproximateTypeInferencer(
			ex.getAST(classContent, ParseType.COMPILATION_UNIT));
	jati.infer();
	final Map<String, String> vars = jati.getVariableTypes();
	assertEquals(vars.get("anInstance"), "my.pack.SomeName");
	assertEquals(vars.get("arrayOfInt"), "int[]");
	assertEquals(vars.get("aNumber"), "long");
	assertEquals(vars.get("singleObject"), "your.pack.Blah");
	assertEquals(vars.get("arrayOfObjects"), "your.pack.Blah[]");
	assertEquals(vars.get("listOfInt"), "java.util.List<java.lang.Integer>");
	assertEquals(
			vars.get("complexParamType"),
			"java.util.Map<your.pack.Blah,java.util.Map<my.pack.SomeNameInPkg,java.util.List<java.lang.Double>>>");
	assertEquals(vars.get("paraType"),
			"your.pack2.ParamType<your.pack.Blah>");
	assertEquals(vars.get("lowerBoundPa"),
			"your.pack2.ParamType<? extends your.pack.Blah>");
	assertEquals(vars.get("upperBoundPa"),
			"your.pack2.ParamType<? super your.pack.Blah>");
	assertEquals(vars.get("upperBoundPa2"),
			"your.pack2.ParamType<? super java.util.List<? super your.pack.Blah>>");
	assertEquals(vars.get("e"),
			"java.io.IOException | java.lang.ArithmeticException");

}
 
Example #19
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 #20
Source File: JavaASTExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Get the AST by making the best effort to guess the type of the node.
 *
 * @throws Exception
 */
public final ASTNode getBestEffortAstNode(final char[] content)
		throws Exception {
	for (final ParseType parseType : ParseType.values()) {
		final ASTNode node = getASTNode(content, parseType);
		if (normalizeCode(node.toString().toCharArray()).equals(
				normalizeCode(content))) {
			return node;
		}
	}
	throw new Exception(
			"Code snippet could not be recognized as any of the known types");
}
 
Example #21
Source File: JavascriptASTExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Return an ASTNode given the content
 * 
 * @param content
 * @return
 */
public final ASTNode getASTNode(final char[] content,
		final ParseType parseType) {
	final ASTParser parser = ASTParser.newParser(AST.JLS3);
	final int astKind;
	switch (parseType) {
	case CLASS_BODY:
	case METHOD:
		astKind = ASTParser.K_CLASS_BODY_DECLARATIONS;
		break;
	case COMPILATION_UNIT:
		astKind = ASTParser.K_COMPILATION_UNIT;
		break;
	case EXPRESSION:
		astKind = ASTParser.K_EXPRESSION;
		break;
	case STATEMENTS:
		astKind = ASTParser.K_STATEMENTS;
		break;
	default:
		astKind = ASTParser.K_COMPILATION_UNIT;
	}
	parser.setKind(astKind);

	final Map<String, String> options = new Hashtable<String, String>();
	options.put(JavaScriptCore.COMPILER_CODEGEN_TARGET_PLATFORM,
			JavaScriptCore.VERSION_1_7);
	options.put(JavaScriptCore.COMPILER_SOURCE, JavaScriptCore.VERSION_1_7);
	if (useJavadocs) {
		options.put(JavaScriptCore.COMPILER_DOC_COMMENT_SUPPORT,
				JavaScriptCore.ENABLED);
	}
	parser.setCompilerOptions(options);
	parser.setSource(content); // set source
	parser.setResolveBindings(useBindings);
	parser.setBindingsRecovery(useBindings);

	parser.setStatementsRecovery(true);

	if (parseType != ParseType.METHOD) {
		return parser.createAST(null);
	} else {
		final ASTNode cu = parser.createAST(null);
		return getFirstFunctionDeclaration(cu);
	}
}
 
Example #22
Source File: MethodScopeExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static Multimap<Scope, String> getScopeSnippets(final String code,
		final boolean methodAsRoots, final ParseType parseType) {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	return getScopeSnippets(ex.getAST(code, parseType), methodAsRoots);
}
 
Example #23
Source File: ScopedIdentifierRenaming.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ScopedIdentifierRenaming(final IScopeExtractor scopeExtractor,
		final ParseType parseType) {
	this.scopeExtractor = scopeExtractor;
	parseKindToUseOnOriginal = parseType;
}
 
Example #24
Source File: TypenameScopeExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Multimap<Scope, String> getFromString(final String file,
		final ParseType parseType) {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	return getClassnames(ex.getAST(file, parseType));
}
 
Example #25
Source File: VariableScopeExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Multimap<Scope, String> getFromString(final String code,
		final ParseType parseType) {
	return getScopeSnippets(code, parseType);
}
 
Example #26
Source File: MethodScopeExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public final Multimap<Scope, String> getFromString(final String code,
		final ParseType parseType) {
	return getScopeSnippets(code, methodAsRoots, parseType);
}
 
Example #27
Source File: JavascriptASTExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Return an ASTNode given the content
 * 
 * @param content
 * @return
 */
public final ASTNode getASTNode(final char[] content,
		final ParseType parseType) {
	final ASTParser parser = ASTParser.newParser(AST.JLS3);
	final int astKind;
	switch (parseType) {
	case CLASS_BODY:
	case METHOD:
		astKind = ASTParser.K_CLASS_BODY_DECLARATIONS;
		break;
	case COMPILATION_UNIT:
		astKind = ASTParser.K_COMPILATION_UNIT;
		break;
	case EXPRESSION:
		astKind = ASTParser.K_EXPRESSION;
		break;
	case STATEMENTS:
		astKind = ASTParser.K_STATEMENTS;
		break;
	default:
		astKind = ASTParser.K_COMPILATION_UNIT;
	}
	parser.setKind(astKind);

	final Map<String, String> options = new Hashtable<String, String>();
	options.put(JavaScriptCore.COMPILER_CODEGEN_TARGET_PLATFORM,
			JavaScriptCore.VERSION_1_7);
	options.put(JavaScriptCore.COMPILER_SOURCE, JavaScriptCore.VERSION_1_7);
	if (useJavadocs) {
		options.put(JavaScriptCore.COMPILER_DOC_COMMENT_SUPPORT,
				JavaScriptCore.ENABLED);
	}
	parser.setCompilerOptions(options);
	parser.setSource(content); // set source
	parser.setResolveBindings(useBindings);
	parser.setBindingsRecovery(useBindings);

	parser.setStatementsRecovery(true);

	if (parseType != ParseType.METHOD) {
		return parser.createAST(null);
	} else {
		final ASTNode cu = parser.createAST(null);
		return getFirstFunctionDeclaration(cu);
	}
}
 
Example #28
Source File: JavaASTExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Return an ASTNode given the content
 *
 * @param content
 * @return
 */
public final ASTNode getASTNode(final char[] content,
		final ParseType parseType) {
	final ASTParser parser = ASTParser.newParser(AST.JLS8);
	final int astKind;
	switch (parseType) {
	case CLASS_BODY:
	case METHOD:
		astKind = ASTParser.K_CLASS_BODY_DECLARATIONS;
		break;
	case COMPILATION_UNIT:
		astKind = ASTParser.K_COMPILATION_UNIT;
		break;
	case EXPRESSION:
		astKind = ASTParser.K_EXPRESSION;
		break;
	case STATEMENTS:
		astKind = ASTParser.K_STATEMENTS;
		break;
	default:
		astKind = ASTParser.K_COMPILATION_UNIT;
	}
	parser.setKind(astKind);

	final Map<String, String> options = new Hashtable<String, String>();
	options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
			JavaCore.VERSION_1_8);
	options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_8);
	if (useJavadocs) {
		options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED);
	}
	parser.setCompilerOptions(options);
	parser.setSource(content); // set source
	parser.setResolveBindings(useBindings);
	parser.setBindingsRecovery(useBindings);

	parser.setStatementsRecovery(true);

	if (parseType != ParseType.METHOD) {
		return parser.createAST(null);
	} else {
		final ASTNode cu = parser.createAST(null);
		return getFirstMethodDeclaration(cu);
	}
}
 
Example #29
Source File: TypenameScopeExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Multimap<Scope, String> getFromString(final String file,
		final ParseType parseType) {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	return getClassnames(ex.getAST(file, parseType));
}
 
Example #30
Source File: MethodScopeExtractor.java    From api-mining with GNU General Public License v3.0 4 votes vote down vote up
public static Multimap<Scope, String> getScopeSnippets(final String code,
		final boolean methodAsRoots, final ParseType parseType) {
	final JavaASTExtractor ex = new JavaASTExtractor(false);
	return getScopeSnippets(ex.getAST(code, parseType), methodAsRoots);
}