use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class AbstractSerialVersionOperation method rewriteAST.
/**
* {@inheritDoc}
*/
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
final ASTRewrite rewrite = cuRewrite.getASTRewrite();
VariableDeclarationFragment fragment = null;
for (int i = 0; i < fNodes.length; i++) {
final ASTNode node = fNodes[i];
final AST ast = node.getAST();
fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(NAME_FIELD));
final FieldDeclaration declaration = ast.newFieldDeclaration(fragment);
declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG));
declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL));
if (!addInitializer(fragment, node))
continue;
if (fragment.getInitializer() != null) {
final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite);
if (node instanceof AbstractTypeDeclaration)
rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup);
else if (node instanceof AnonymousClassDeclaration)
rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
else if (node instanceof ParameterizedType) {
final ParameterizedType type = (ParameterizedType) node;
final ASTNode parent = type.getParent();
if (parent instanceof ClassInstanceCreation) {
final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration();
if (anonymous != null)
rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
}
} else
Assert.isTrue(false);
addLinkedPositions(rewrite, fragment, positionGroups);
}
final String comment = CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, StubUtility.getLineDelimiterUsed(fUnit));
if (comment != null && comment.length() > 0) {
final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
declaration.setJavadoc(doc);
}
}
if (fragment == null)
return;
positionGroups.setEndPosition(rewrite.track(fragment));
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class ScopeAnalyzer method addLocalDeclarations.
private boolean addLocalDeclarations(ASTNode node, int offset, int flags, IBindingRequestor requestor) {
if (hasFlag(VARIABLES, flags) || hasFlag(TYPES, flags)) {
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
if (declaration instanceof MethodDeclaration || declaration instanceof Initializer || declaration instanceof FieldDeclaration) {
ScopeAnalyzerVisitor visitor = new ScopeAnalyzerVisitor(offset, flags, requestor);
declaration.accept(visitor);
return visitor.fBreak;
}
}
return false;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class JavadocTagsSubProcessor method getMissingJavadocCommentProposals.
public static void getMissingJavadocCommentProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
ASTNode node = problem.getCoveringNode(context.getASTRoot());
if (node == null) {
return;
}
BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(node);
if (declaration == null) {
return;
}
ICompilationUnit cu = context.getCompilationUnit();
ITypeBinding binding = Bindings.getBindingOfParentType(declaration);
if (binding == null) {
return;
}
if (declaration instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) declaration;
IMethodBinding methodBinding = methodDecl.resolveBinding();
IMethodBinding overridden = null;
if (methodBinding != null) {
overridden = Bindings.findOverriddenMethod(methodBinding, true);
}
String string = CodeGeneration.getMethodComment(cu, binding.getName(), methodDecl, overridden, String.valueOf('\n'));
if (string != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_method_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_METHOD, declaration.getStartPosition(), string));
}
} else if (declaration instanceof AbstractTypeDeclaration) {
String typeQualifiedName = Bindings.getTypeQualifiedName(binding);
String[] typeParamNames;
if (declaration instanceof TypeDeclaration) {
List<TypeParameter> typeParams = ((TypeDeclaration) declaration).typeParameters();
typeParamNames = new String[typeParams.size()];
for (int i = 0; i < typeParamNames.length; i++) {
typeParamNames[i] = (typeParams.get(i)).getName().getIdentifier();
}
} else {
typeParamNames = new String[0];
}
String string = CodeGeneration.getTypeComment(cu, typeQualifiedName, typeParamNames, String.valueOf('\n'));
if (string != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_type_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_TYPE, declaration.getStartPosition(), string));
}
} else if (declaration instanceof FieldDeclaration) {
//$NON-NLS-1$
String comment = "/**\n *\n */\n";
List<VariableDeclarationFragment> fragments = ((FieldDeclaration) declaration).fragments();
if (fragments != null && fragments.size() > 0) {
VariableDeclaration decl = fragments.get(0);
String fieldName = decl.getName().getIdentifier();
String typeName = binding.getName();
comment = CodeGeneration.getFieldComment(cu, typeName, fieldName, String.valueOf('\n'));
}
if (comment != null) {
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_field_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_FIELD, declaration.getStartPosition(), comment));
}
} else if (declaration instanceof EnumConstantDeclaration) {
EnumConstantDeclaration enumDecl = (EnumConstantDeclaration) declaration;
String id = enumDecl.getName().getIdentifier();
String comment = CodeGeneration.getFieldComment(cu, binding.getName(), id, String.valueOf('\n'));
String label = CorrectionMessages.JavadocTagsSubProcessor_addjavadoc_enumconst_description;
proposals.add(new AddJavadocCommentProposal(label, cu, IProposalRelevance.ADD_JAVADOC_ENUM, declaration.getStartPosition(), comment));
}
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class PromoteTempToFieldRefactoring method createNewFieldDeclaration.
private FieldDeclaration createNewFieldDeclaration(ASTRewrite rewrite) {
AST ast = getAST();
VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
SimpleName variableName = ast.newSimpleName(fFieldName);
fragment.setName(variableName);
addLinkedName(rewrite, variableName, false);
List<Dimension> extraDimensions = DimensionRewrite.copyDimensions(fTempDeclarationNode.extraDimensions(), rewrite);
fragment.extraDimensions().addAll(extraDimensions);
if (fInitializeIn == INITIALIZE_IN_FIELD && tempHasInitializer()) {
Expression initializer = (Expression) rewrite.createCopyTarget(getTempInitializer());
fragment.setInitializer(initializer);
}
FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(fragment);
VariableDeclarationStatement vds = getTempDeclarationStatement();
Type type = (Type) rewrite.createCopyTarget(vds.getType());
fieldDeclaration.setType(type);
fieldDeclaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getModifiers()));
return fieldDeclaration;
}
use of org.eclipse.jdt.core.dom.FieldDeclaration in project che by eclipse.
the class PromoteTempToFieldRefactoring method checkClashesWithExistingFields.
private RefactoringStatus checkClashesWithExistingFields() {
FieldDeclaration[] existingFields = getFieldDeclarations();
for (int i = 0; i < existingFields.length; i++) {
FieldDeclaration declaration = existingFields[i];
VariableDeclarationFragment[] fragments = (VariableDeclarationFragment[]) declaration.fragments().toArray(new VariableDeclarationFragment[declaration.fragments().size()]);
for (int j = 0; j < fragments.length; j++) {
VariableDeclarationFragment fragment = fragments[j];
if (fFieldName.equals(fragment.getName().getIdentifier())) {
//cannot conflict with more than 1 name
RefactoringStatusContext context = JavaStatusContext.create(fCu, fragment);
return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.PromoteTempToFieldRefactoring_Name_conflict_with_field, context);
}
}
}
return null;
}
Aggregations