Java Code Examples for org.eclipse.jdt.core.dom.ASTParser#K_COMPILATION_UNIT

The following examples show how to use org.eclipse.jdt.core.dom.ASTParser#K_COMPILATION_UNIT . 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: GoogleJavaFormatter.java    From google-java-format with Apache License 2.0 5 votes vote down vote up
/** Runs the Google Java formatter on the given source, with only the given ranges specified. */
private TextEdit formatInternal(int kind, String source, IRegion[] regions, int initialIndent) {
  try {
    boolean includeComments =
        (kind & CodeFormatter.F_INCLUDE_COMMENTS) == CodeFormatter.F_INCLUDE_COMMENTS;
    kind &= ~CodeFormatter.F_INCLUDE_COMMENTS;
    SnippetKind snippetKind;
    switch (kind) {
      case ASTParser.K_EXPRESSION:
        snippetKind = SnippetKind.EXPRESSION;
        break;
      case ASTParser.K_STATEMENTS:
        snippetKind = SnippetKind.STATEMENTS;
        break;
      case ASTParser.K_CLASS_BODY_DECLARATIONS:
        snippetKind = SnippetKind.CLASS_BODY_DECLARATIONS;
        break;
      case ASTParser.K_COMPILATION_UNIT:
        snippetKind = SnippetKind.COMPILATION_UNIT;
        break;
      default:
        throw new IllegalArgumentException(String.format("Unknown snippet kind: %d", kind));
    }
    List<Replacement> replacements =
        new SnippetFormatter()
            .format(
                snippetKind, source, rangesFromRegions(regions), initialIndent, includeComments);
    if (idempotent(source, regions, replacements)) {
      // Do not create edits if there's no diff.
      return null;
    }
    // Convert replacements to text edits.
    return editFromReplacements(replacements);
  } catch (IllegalArgumentException | FormatterException exception) {
    // Do not format on errors.
    return null;
  }
}
 
Example 2
Source File: JavaCodeAnalyzer.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
public JavaCodeAnalyzer.JavaParseResult<? extends ASTNode> determinateJavaType(final String javaCode) {
  ASTParser parser = this.parserFactory.createDefaultJavaParser(this.parserFactory.minParserApiLevel);
  parser.setSource(javaCode.toCharArray());
  parser.setStatementsRecovery(true);
  ASTNode _createAST = parser.createAST(null);
  CompilationUnit unit = ((CompilationUnit) _createAST);
  int _size = unit.types().size();
  boolean _greaterThan = (_size > 0);
  if (_greaterThan) {
    List<CompilationUnit> _singletonList = Collections.<CompilationUnit>singletonList(unit);
    return new JavaCodeAnalyzer.JavaParseResult<CompilationUnit>(javaCode, ASTParser.K_COMPILATION_UNIT, _singletonList);
  }
  parser.setSource(javaCode.toCharArray());
  parser.setStatementsRecovery(false);
  parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
  ASTNode root = parser.createAST(null);
  if ((root instanceof TypeDeclaration)) {
    List<BodyDeclaration> bodyDeclarations = ((TypeDeclaration)root).bodyDeclarations();
    int _size_1 = bodyDeclarations.size();
    boolean _greaterThan_1 = (_size_1 > 0);
    if (_greaterThan_1) {
      return new JavaCodeAnalyzer.JavaParseResult<BodyDeclaration>(javaCode, ASTParser.K_CLASS_BODY_DECLARATIONS, bodyDeclarations);
    }
  }
  parser.setSource(javaCode.toCharArray());
  parser.setStatementsRecovery(false);
  parser.setKind(ASTParser.K_STATEMENTS);
  root = parser.createAST(null);
  if ((root instanceof Block)) {
    List<Statement> statements = ((Block)root).statements();
    int _size_2 = statements.size();
    boolean _greaterThan_2 = (_size_2 > 0);
    if (_greaterThan_2) {
      return new JavaCodeAnalyzer.JavaParseResult<Statement>(javaCode, ASTParser.K_STATEMENTS, statements);
    }
  }
  parser.setSource(javaCode.toCharArray());
  parser.setStatementsRecovery(false);
  parser.setKind(ASTParser.K_EXPRESSION);
  root = parser.createAST(null);
  if ((root instanceof Expression)) {
    List<Expression> _singletonList_1 = Collections.<Expression>singletonList(((Expression)root));
    return new JavaCodeAnalyzer.JavaParseResult<Expression>(javaCode, ASTParser.K_EXPRESSION, _singletonList_1);
  }
  return null;
}
 
Example 3
Source File: JavaASTParser.java    From KodeBeagle with Apache License 2.0 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 HashMap<>();
    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);
    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 4
Source File: JavaASTExtractor.java    From api-mining with GNU General Public License v3.0 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 5
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 6
Source File: JavaASTExtractor.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.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);
	}
}