use of org.eclipse.jdt.core.dom.TypeDeclaration in project xtext-xtend by eclipse.
the class JavaCodeAnalyzer method determinateJavaType.
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;
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project eclipse-cs by checkstyle.
the class FinalClassQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(TypeDeclaration node) {
// recalculate start position because optional javadoc is mixed
// into the original start position
int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
if (containsPosition(lineInfo, pos)) {
if (!Modifier.isFinal(node.getModifiers())) {
Modifier finalModifier = node.getAST().newModifier(ModifierKeyword.FINAL_KEYWORD);
node.modifiers().add(finalModifier);
// reorder modifiers into their correct order
List<?> reorderedModifiers = ModifierOrderQuickfix.reOrderModifiers(node.modifiers());
node.modifiers().clear();
node.modifiers().addAll(reorderedModifiers);
}
}
return true;
}
};
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project eclipse-cs by checkstyle.
the class RedundantModifierQuickfix method handleGetCorrectingASTVisitor.
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) {
return new ASTVisitor() {
@SuppressWarnings("unchecked")
@Override
public boolean visit(MethodDeclaration node) {
if (containsPosition(node, markerStartOffset)) {
List<ModifierKeyword> redundantKeyWords = Collections.emptyList();
if (node.getParent() instanceof TypeDeclaration) {
TypeDeclaration type = (TypeDeclaration) node.getParent();
if (type.isInterface()) {
redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD });
} else if (Modifier.isFinal(type.getModifiers())) {
redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.FINAL_KEYWORD });
}
}
deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
}
return true;
}
@SuppressWarnings("unchecked")
@Override
public boolean visit(FieldDeclaration node) {
// recalculate start position because optional javadoc is mixed
// into the original start position
int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
if (containsPosition(lineInfo, pos)) {
List<ModifierKeyword> redundantKeyWords = Collections.emptyList();
if (node.getParent() instanceof TypeDeclaration) {
TypeDeclaration type = (TypeDeclaration) node.getParent();
if (type.isInterface()) {
redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
}
} else if (node.getParent() instanceof AnnotationTypeDeclaration) {
redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
}
deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
}
return true;
}
@SuppressWarnings("unchecked")
@Override
public boolean visit(AnnotationTypeMemberDeclaration node) {
// recalculate start position because optional javadoc is mixed
// into the original start position
int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0);
if (containsPosition(lineInfo, pos)) {
if (node.getParent() instanceof AnnotationTypeDeclaration) {
List<ModifierKeyword> redundantKeyWords = Arrays.asList(new ModifierKeyword[] { ModifierKeyword.PUBLIC_KEYWORD, ModifierKeyword.ABSTRACT_KEYWORD, ModifierKeyword.FINAL_KEYWORD, ModifierKeyword.STATIC_KEYWORD });
deleteRedundantModifiers(node.modifiers(), redundantKeyWords);
}
}
return true;
}
private void deleteRedundantModifiers(List<ASTNode> modifiers, List<ModifierKeyword> redundantModifierKeywords) {
Iterator<ASTNode> it = modifiers.iterator();
while (it.hasNext()) {
ASTNode node = it.next();
if (node instanceof Modifier) {
Modifier modifier = (Modifier) node;
if (redundantModifierKeywords.contains(modifier.getKeyword())) {
it.remove();
}
}
}
}
};
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project whole by wholeplatform.
the class JDTUtilsTest method testParseInitializer.
@Test
public void testParseInitializer() {
try {
ASTNode astNode = JDTUtils.parse(JAVA_INITIALIZER);
assertTrue(astNode instanceof TypeDeclaration);
} catch (IllegalArgumentException e) {
fail();
}
}
use of org.eclipse.jdt.core.dom.TypeDeclaration in project whole by wholeplatform.
the class JDTUtilsTest method testParseClassBodyDeclarations.
@Test
public void testParseClassBodyDeclarations() {
try {
ASTNode astNode = JDTUtils.parse(JAVA_CLASS_BODY_DECLARATIONS);
assertTrue(astNode instanceof TypeDeclaration);
} catch (IllegalArgumentException e) {
fail();
}
}
Aggregations