org.eclipse.jdt.core.dom.Comment Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.Comment. 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: CamelJavaFileParser.java    From eip-designer with Apache License 2.0 6 votes vote down vote up
/**
 * Parse the routeFile given while building the instance and fill the model.
 * @param model The EIP Model to fill with parsed elements from routeFile.
 * @throws InvalidArgumentException if given file is not a valid Spring integration file
 */
public void parseAndFillModel(EIPModel model) throws Exception {
   // Read source content.
   routeSource = parseRouteClass();
   
   // Parse and get the compilation unit.
   ASTParser parser = ASTParser.newParser(AST.JLS8);
   parser.setKind(ASTParser.K_COMPILATION_UNIT);
   parser.setSource(routeSource.toCharArray());
   parser.setResolveBindings(false);
   routeCU = (CompilationUnit) parser.createAST(null);
   
   // Visit and build a comment map before parsing.
   for (Object comment : routeCU.getCommentList()) {
      ((Comment) comment).accept(this);
   }
   
   // Initialize and build a route.
   route = EipFactory.eINSTANCE.createRoute();
   model.getOwnedRoutes().add(route);
   
   // 
   routeCU.accept(this);
}
 
Example #2
Source File: JavadocLineBreakPreparator.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private List<Comment> getComments(ASTNode astRoot) {
	if (astRoot.getRoot() instanceof CompilationUnit) {
		CompilationUnit compilationUnit = (CompilationUnit) astRoot.getRoot();
		return compilationUnit.getCommentList();
	}
	return Collections.emptyList();
}
 
Example #3
Source File: ASTReader.java    From JDeodorant with MIT License 5 votes vote down vote up
private List<CommentObject> processComments(IFile iFile, IDocument iDocument,
		AbstractTypeDeclaration typeDeclaration, List<Comment> comments) {
	List<CommentObject> commentList = new ArrayList<CommentObject>();
	int typeDeclarationStartPosition = typeDeclaration.getStartPosition();
	int typeDeclarationEndPosition = typeDeclarationStartPosition + typeDeclaration.getLength();
	for(Comment comment : comments) {
		int commentStartPosition = comment.getStartPosition();
		int commentEndPosition = commentStartPosition + comment.getLength();
		int commentStartLine = 0;
		int commentEndLine = 0;
		String text = null;
		try {
			commentStartLine = iDocument.getLineOfOffset(commentStartPosition);
			commentEndLine = iDocument.getLineOfOffset(commentEndPosition);
			text = iDocument.get(commentStartPosition, comment.getLength());
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		CommentType type = null;
		if(comment.isLineComment()) {
			type = CommentType.LINE;
		}
		else if(comment.isBlockComment()) {
			type = CommentType.BLOCK;
		}
		else if(comment.isDocComment()) {
			type = CommentType.JAVADOC;
		}
		CommentObject commentObject = new CommentObject(text, type, commentStartLine, commentEndLine);
		commentObject.setComment(comment);
		String fileExtension = iFile.getFileExtension() != null ? "." + iFile.getFileExtension() : "";
		if(typeDeclarationStartPosition <= commentStartPosition && typeDeclarationEndPosition >= commentEndPosition) {
			commentList.add(commentObject);
		}
		else if(iFile.getName().equals(typeDeclaration.getName().getIdentifier() + fileExtension)) {
			commentList.add(commentObject);
		}
	}
	return commentList;
}
 
Example #4
Source File: CamelJavaFileParser.java    From eip-designer with Apache License 2.0 5 votes vote down vote up
/** Extract comment content from source. */
private String getCommentContent(Comment comment) {
   int start = comment.getStartPosition();
   int end = start + comment.getLength();
   String content = routeSource.substring(start, end);
   if (content.startsWith("//")) {
      content = content.substring(2).trim();
   }
   return content;
}
 
Example #5
Source File: UglyMathCommentsExtractor.java    From compiler with Apache License 2.0 5 votes vote down vote up
private final int getNodeFirstLeadingCommentIndex(final ASTNode node) {
    if (node instanceof PackageDeclaration) {
        if (commentsVisited.length > 0) {
            final Comment comment = (Comment) comments.get(0);
            if (comment.getStartPosition() + comment.getLength() <= ((PackageDeclaration) node).getName()
                    .getStartPosition()) {
                return 0;
            }
        }
        return -1;
    } else {
        return cu.firstLeadingCommentIndex(node);
    }
}
 
Example #6
Source File: UglyMathCommentsExtractor.java    From compiler with Apache License 2.0 5 votes vote down vote up
private final CommentItem extractCommentItem(final int index) {
    if (commentsVisited[index]) {
        return null;
    } else {
        final Comment comment = (Comment) comments.get(index);
        if (comment.isDocComment()) {
            /* Interpret DocComment as Line or Block */
            comment.delete();
        }
        final int start = comment.getStartPosition();
        final int end = start + comment.getLength();
        final String value = this.src.substring(start, end);
        return new CommentItem(comment, value);
    }
}
 
Example #7
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
private String commentContent(final Comment comment) {
  int _startPosition = comment.getStartPosition();
  int _startPosition_1 = comment.getStartPosition();
  int _length = comment.getLength();
  int _plus = (_startPosition_1 + _length);
  return this.javaSources.substring(_startPosition, _plus);
}
 
Example #8
Source File: SelectionRangeHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Finds the comment that contains the specified position
 *
 * @param comments
 * @param offset
 * @return
 */
public ASTNode containingComment(List<Comment> comments, int offset) {
	for (Comment comment : comments) {
		ASTNode result = NodeFinder.perform(comment, offset, 0);
		if (result != null) {
			return result;
		}
	}

	return null;
}
 
Example #9
Source File: CopyrightManager.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Gets compilation unit's source
 *
 * @param unit
 *            affected compilation unit
 * @param comment
 *            comment to be replaced; set null if comment is not present
 * @return new compilation unit's source
 */
private String getNewUnitSource(final ICompilationUnit unit, final Comment comment) {
	String source = null;
	try {
		source = unit.getSource();
		if (comment != null) {
			final int endOfComment = comment.getLength() + comment.getStartPosition();
			source = source.replace(source.substring(0, endOfComment), getCopyrightText());
		}
	} catch (final JavaModelException e) {
		ConsoleUtils.printError(e.getMessage());
	}
	return source;
}
 
Example #10
Source File: CopyrightManager.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether {@link CompilationUnit} has copyright header
 *
 * @param compilationUnit
 *            checked compilation unit
 * @return true if {@link CompilationUnit} has copyright header
 */
public boolean hasCopyrightsComment(final CompilationUnit compilationUnit) {
	final List<Comment> comments = getCommentList(compilationUnit);
	boolean hasCopyrights = false;
	if (!comments.isEmpty()) {
		final PackageDeclaration packageNode = compilationUnit.getPackage();
		final boolean commentBeforePackage = comments.get(0).getStartPosition() <= packageNode.getStartPosition();
		final boolean hasJavaDoc = packageNode.getJavadoc() != null;
		hasCopyrights = commentBeforePackage || hasJavaDoc;
	}
	return hasCopyrights;
}
 
Example #11
Source File: CopyrightManager.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces copyright header to the compilation unit
 *
 * @param compilationUnit
 *            compilation unit affected
 * @return compilation unit change
 */
public CompilationUnitChange replaceCopyrightsHeader(final CompilationUnit compilationUnit) {
	final ICompilationUnit unit = (ICompilationUnit) compilationUnit.getJavaElement();
	change = new CompilationUnitChange(OVERRIDE_COPYRIGHT, unit);
	rewriter = ASTRewrite.create(compilationUnit.getAST());
	final List<Comment> comments = getCommentList(compilationUnit);
	Comment copyrightComment = null;
	if (!comments.isEmpty()) {
		copyrightComment = comments.get(0);
	}
	rewriteCompilationUnit(unit, getNewUnitSource(unit, copyrightComment));
	return change;
}
 
Example #12
Source File: CopyrightManager.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Adds copyright header to the compilation unit
 *
 * @param compilationUnit
 *            compilation unit affected
 * @return compilation unit change
 */
public CompilationUnitChange addCopyrightsHeader(final CompilationUnit compilationUnit) {
	final ICompilationUnit unit = (ICompilationUnit) compilationUnit.getJavaElement();
	change = new CompilationUnitChange(ADD_COPYRIGHT, unit);
	rewriter = ASTRewrite.create(compilationUnit.getAST());
	final ListRewrite listRewrite = rewriter.getListRewrite(compilationUnit.getPackage(),
			PackageDeclaration.ANNOTATIONS_PROPERTY);
	final Comment placeHolder = (Comment) rewriter.createStringPlaceholder(getCopyrightText() + NEW_LINE_SEPARATOR,
			ASTNode.BLOCK_COMMENT);
	listRewrite.insertFirst(placeHolder, null);
	rewriteCompilationUnit(unit, getNewUnitSource(unit, null));
	return change;
}
 
Example #13
Source File: JavadocLineBreakPreparator.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(int kind, TokenManager tokenManager, ASTNode astRoot) {
	if ((kind & CodeFormatter.F_INCLUDE_COMMENTS) != 0) {
		ASTVisitor visitor = new Vistor(tokenManager);
		for (Comment comment : getComments(astRoot)) {
			comment.accept(visitor);
		}
	}
}
 
Example #14
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
private boolean notAssigned(final Comment comment) {
  boolean _contains = this.assignedComments.contains(comment);
  return (!_contains);
}
 
Example #15
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean visit(final TypeDeclaration it) {
  boolean _isDummyType = this._aSTFlattenerUtils.isDummyType(it);
  if (_isDummyType) {
    this.visitAll(it.bodyDeclarations(), this.nl());
    return false;
  }
  boolean _isNotSupportedInnerType = this._aSTFlattenerUtils.isNotSupportedInnerType(it);
  if (_isNotSupportedInnerType) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("/* FIXME Non-static inner classes are not supported.*/");
    this.appendToBuffer(_builder.toString());
    this.addProblem(it, "Non-static inner classes are not supported.");
  }
  Javadoc _javadoc = it.getJavadoc();
  boolean _tripleNotEquals = (_javadoc != null);
  if (_tripleNotEquals) {
    it.getJavadoc().accept(this);
  }
  this.appendModifiers(it, it.modifiers());
  boolean _isInterface = it.isInterface();
  if (_isInterface) {
    this.appendToBuffer("interface ");
  } else {
    boolean _isPackageVisibility = this._aSTFlattenerUtils.isPackageVisibility(Iterables.<Modifier>filter(it.modifiers(), Modifier.class));
    if (_isPackageVisibility) {
      this.appendToBuffer("package ");
    }
    this.appendToBuffer("class ");
  }
  it.getName().accept(this);
  boolean _isEmpty = it.typeParameters().isEmpty();
  boolean _not = (!_isEmpty);
  if (_not) {
    this.appendTypeParameters(it.typeParameters());
  }
  this.appendSpaceToBuffer();
  Type _superclassType = it.getSuperclassType();
  boolean _tripleNotEquals_1 = (_superclassType != null);
  if (_tripleNotEquals_1) {
    this.appendToBuffer("extends ");
    it.getSuperclassType().accept(this);
    this.appendSpaceToBuffer();
  }
  boolean _isEmpty_1 = it.superInterfaceTypes().isEmpty();
  boolean _not_1 = (!_isEmpty_1);
  if (_not_1) {
    boolean _isInterface_1 = it.isInterface();
    if (_isInterface_1) {
      this.appendToBuffer("extends ");
    } else {
      this.appendToBuffer("implements ");
    }
    this.visitAllSeparatedByComma(it.superInterfaceTypes());
  }
  this.appendToBuffer("{");
  this.increaseIndent();
  BodyDeclaration prev = null;
  List _bodyDeclarations = it.bodyDeclarations();
  for (final BodyDeclaration body : ((Iterable<BodyDeclaration>) _bodyDeclarations)) {
    {
      if ((prev instanceof EnumConstantDeclaration)) {
        if ((body instanceof EnumConstantDeclaration)) {
          this.appendToBuffer(", ");
        } else {
          this.appendToBuffer("; ");
        }
      }
      this.appendLineWrapToBuffer();
      body.accept(this);
      prev = body;
    }
  }
  ASTNode _root = it.getRoot();
  if ((_root instanceof CompilationUnit)) {
    ASTNode _root_1 = it.getRoot();
    final CompilationUnit cu = ((CompilationUnit) _root_1);
    final Consumer<Comment> _function = (Comment it_1) -> {
      it_1.accept(this);
      this.assignedComments.add(it_1);
    };
    this.unAssignedComments(cu).forEach(_function);
  }
  this.decreaseIndent();
  this.appendLineWrapToBuffer();
  this.appendToBuffer("}");
  return false;
}
 
Example #16
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
private Iterable<Comment> unAssignedComments(final CompilationUnit cu) {
  final Function1<Comment, Boolean> _function = (Comment c) -> {
    return Boolean.valueOf(((!(c.isDocComment() && (c.getParent() != null))) && this.notAssigned(c)));
  };
  return IterableExtensions.<Comment>filter(cu.getCommentList(), _function);
}
 
Example #17
Source File: ICommentsExtractor.java    From compiler with Apache License 2.0 4 votes vote down vote up
protected CommentItem(final Comment node, final String value) {
    this.node = node;
    this.value = value;
}
 
Example #18
Source File: SuperTypeConstraintsCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public final boolean visit(final Comment node) {
	return false;
}
 
Example #19
Source File: CommentObject.java    From JDeodorant with MIT License 4 votes vote down vote up
public void setComment(Comment comment) {
	this.comment = ASTInformationGenerator.generateASTInformation(comment);
}
 
Example #20
Source File: CommentObject.java    From JDeodorant with MIT License 4 votes vote down vote up
public Comment getComment() {
	return (Comment)comment.recoverASTNode();
}
 
Example #21
Source File: ASTReader.java    From JDeodorant with MIT License 4 votes vote down vote up
private ClassObject processEnumDeclaration(IFile iFile, IDocument document, EnumDeclaration enumDeclaration, List<Comment> comments) {
	final ClassObject classObject = new ClassObject();
	classObject.setEnum(true);
	classObject.setIFile(iFile);
	classObject.addComments(processComments(iFile, document, enumDeclaration, comments));
	classObject.setName(enumDeclaration.resolveBinding().getQualifiedName());
	classObject.setAbstractTypeDeclaration(enumDeclaration);
	
	int modifiers = enumDeclaration.getModifiers();
	if((modifiers & Modifier.ABSTRACT) != 0)
		classObject.setAbstract(true);
	
	if((modifiers & Modifier.PUBLIC) != 0)
		classObject.setAccess(Access.PUBLIC);
	else if((modifiers & Modifier.PROTECTED) != 0)
		classObject.setAccess(Access.PROTECTED);
	else if((modifiers & Modifier.PRIVATE) != 0)
		classObject.setAccess(Access.PRIVATE);
	else
		classObject.setAccess(Access.NONE);
	
	if((modifiers & Modifier.STATIC) != 0)
		classObject.setStatic(true);
	
	List<Type> superInterfaceTypes = enumDeclaration.superInterfaceTypes();
	for(Type interfaceType : superInterfaceTypes) {
		ITypeBinding binding = interfaceType.resolveBinding();
		String qualifiedName = binding.getQualifiedName();
		TypeObject typeObject = TypeObject.extractTypeObject(qualifiedName);
		classObject.addInterface(typeObject);
	}
	
	List<EnumConstantDeclaration> enumConstantDeclarations = enumDeclaration.enumConstants();
	for(EnumConstantDeclaration enumConstantDeclaration : enumConstantDeclarations) {
		EnumConstantDeclarationObject enumConstantDeclarationObject = new EnumConstantDeclarationObject(enumConstantDeclaration.getName().getIdentifier());
		enumConstantDeclarationObject.setEnumName(classObject.getName());
		enumConstantDeclarationObject.setEnumConstantDeclaration(enumConstantDeclaration);
		List<Expression> arguments = enumConstantDeclaration.arguments();
		for(Expression argument : arguments) {
			AbstractExpression abstractExpression = new AbstractExpression(argument);
			enumConstantDeclarationObject.addArgument(abstractExpression);
		}
		classObject.addEnumConstantDeclaration(enumConstantDeclarationObject);
	}
	
	List<BodyDeclaration> bodyDeclarations = enumDeclaration.bodyDeclarations();
	for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
		if(bodyDeclaration instanceof MethodDeclaration) {
			processMethodDeclaration(classObject, (MethodDeclaration)bodyDeclaration);
		}
		else if(bodyDeclaration instanceof FieldDeclaration) {
			processFieldDeclaration(classObject, (FieldDeclaration)bodyDeclaration);
		}
	}
	return classObject;
}
 
Example #22
Source File: CopyrightManager.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 2 votes vote down vote up
/**
 * Returns list of {@link Comment}
 *
 * @param compilationUnit
 *            compilation unit to be analyzed
 * @return lists of comments
 */
@SuppressWarnings("unchecked")
private List<Comment> getCommentList(final CompilationUnit compilationUnit) {
	return compilationUnit.getCommentList();
}