List of usage examples for org.eclipse.jdt.core.dom Modifier setKeyword
public void setKeyword(ModifierKeyword modifierKeyord)
From source file:ch.acanda.eclipse.pmd.java.resolution.design.SingularFieldQuickFix.java
License:Open Source License
/** * Replaces the assignment with a variable declaration. If the assignment is the only one in the block for this * variable, the final modifier is added to the declaration. */// w w w . java 2 s. c om @SuppressWarnings("unchecked") private void replaceAssignment(final VariableDeclarationFragment node, final Assignment assignment, final boolean finalDeclaration) { final FieldDeclaration fieldDeclaration = (FieldDeclaration) node.getParent(); final VariableDeclarationStatement declaration = (VariableDeclarationStatement) node.getAST() .createInstance(VariableDeclarationStatement.class); declaration.setType(ASTUtil.copy(fieldDeclaration.getType())); final VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getAST() .createInstance(VariableDeclarationFragment.class); fragment.setName(ASTUtil.copy(node.getName())); fragment.setInitializer(ASTUtil.copy(assignment.getRightHandSide())); declaration.fragments().add(fragment); if (finalDeclaration) { final Modifier modifier = (Modifier) node.getAST().createInstance(Modifier.class); modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD); declaration.modifiers().add(modifier); } ASTUtil.replace(assignment.getParent(), declaration); }
From source file:ch.acanda.eclipse.pmd.java.resolution.design.UseUtilityClassQuickFix.java
License:Open Source License
private void addFinalIfNecessary(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) { @SuppressWarnings("unchecked") final List<IExtendedModifier> modifiers = typeDeclaration.modifiers(); if (!Iterables.any(modifiers, isFinal())) { final ListRewrite modifierRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.MODIFIERS2_PROPERTY); final Modifier modifier = (Modifier) typeDeclaration.getAST().createInstance(Modifier.class); modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD); modifierRewrite.insertLast(modifier, null); }//w w w . j a v a2 s .c o m }
From source file:ch.acanda.eclipse.pmd.java.resolution.design.UseUtilityClassQuickFix.java
License:Open Source License
@SuppressWarnings("unchecked") private void addPrivateConstructor(final TypeDeclaration typeDeclaration, final ASTRewrite rewrite) { final AST ast = typeDeclaration.getAST(); final MethodDeclaration constructor = (MethodDeclaration) ast.createInstance(MethodDeclaration.class); constructor.setConstructor(true);//from w w w . j ava 2 s . com final Modifier modifier = (Modifier) ast.createInstance(Modifier.class); modifier.setKeyword(ModifierKeyword.PRIVATE_KEYWORD); constructor.modifiers().add(modifier); constructor.setName(ASTUtil.copy(typeDeclaration.getName())); final Block body = (Block) ast.createInstance(Block.class); constructor.setBody(body); final ListRewrite statementRewrite = rewrite.getListRewrite(body, Block.STATEMENTS_PROPERTY); final ASTNode comment = rewrite.createStringPlaceholder("// hide constructor of utility class", ASTNode.EMPTY_STATEMENT); statementRewrite.insertFirst(comment, null); final int position = findConstructorPosition(typeDeclaration); final ListRewrite bodyDeclarationRewrite = rewrite.getListRewrite(typeDeclaration, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); bodyDeclarationRewrite.insertAt(constructor, position, null); }
From source file:ch.acanda.eclipse.pmd.java.resolution.optimization.LocalVariableCouldBeFinalQuickFix.java
License:Open Source License
/** * Adds the final modifier to the variable declaration. *//*from w w w.j av a 2 s . co m*/ @Override @SuppressWarnings("unchecked") protected boolean apply(final VariableDeclarationStatement node) { final Modifier modifier = (Modifier) node.getAST().createInstance(Modifier.class); modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD); node.modifiers().add(modifier); return true; }
From source file:ch.acanda.eclipse.pmd.java.resolution.optimization.MethodArgumentCouldBeFinalQuickFix.java
License:Open Source License
/** * Adds the final modifier to the variable declaration. *///from w w w. j a va 2s . c o m @Override @SuppressWarnings("unchecked") protected boolean apply(final SingleVariableDeclaration node) { final Modifier modifier = (Modifier) node.getAST().createInstance(Modifier.class); modifier.setKeyword(ModifierKeyword.FINAL_KEYWORD); node.modifiers().add(modifier); return true; }
From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaModifierModelAdapter.java
License:Open Source License
@Override public void setValueFeatureValue(Object element, Object feature, Object value) { if (AstCacheCodePackage.eINSTANCE.getModifier_Type().equals(feature)) { if (element instanceof Modifier) { Modifier modifier = (Modifier) element; int flag = (int) value; modifier.setKeyword(Modifier.ModifierKeyword.fromFlagValue(flag)); }// w ww.j a v a2 s . com return; } super.setValueFeatureValue(element, feature, value); }
From source file:lombok.eclipse.agent.PatchValEclipse.java
License:Open Source License
public static Modifier createModifier(AST ast, ModifierKeyword keyword, int start, int end) { Modifier modifier = null; try {/*from w w w . j a v a2 s. c o m*/ modifier = Reflection.modifierConstructor.newInstance(ast); } catch (InstantiationException e) { throw Lombok.sneakyThrow(e); } catch (IllegalAccessException e) { throw Lombok.sneakyThrow(e); } catch (InvocationTargetException e) { throw Lombok.sneakyThrow(e); } if (modifier != null) { modifier.setKeyword(keyword); modifier.setSourceRange(start, end - start + 1); } return modifier; }
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
/** * @return a new modifier// ww w.j ava 2 s. com */ private Modifier createModifier(ModifierKeyword keyword) { final Modifier modifier = new Modifier(this.ast); modifier.setKeyword(keyword); int start = this.scanner.getCurrentTokenStartPosition(); int end = this.scanner.getCurrentTokenEndPosition(); modifier.setSourceRange(start, end - start + 1); return modifier; }
From source file:org.jastemf.refactorings.RefactoringManager.java
License:BSD License
/** * <p>//ww w.j a va2 s.co m * Execute adaptations of the <tt>ASTNode$State</tt> class JastAdd * generates. Such adaptations are required because generated AST classes * are moved throughout the integration process into different packages, * which results in shadowed <tt>ASTNode$State</tt> fields and methods. * </p> * The following adaptations are conducted: * <ul> * <li>Protected fields are made public. Currently these are * ASTNode$State.duringInterpretation, ASTNode$State.CircularValue.value, * ASTNode$State.CircularValue.visited</li> * <li>Additionally: See {@link BasicJDTASTVisitor}</li> * </ul> * * @param context * The current integration context. * @throws JastEMFException * Thrown, iff an IO error occurs or an adaptation cannot be * applied. */ public static void performASTNodeStateAdaptations(IIntegrationContext context) throws JastEMFException { final IFile compilationUnitFile = IOSupport.getFile(context.astfolder(), "ASTNode$State.java"); final CompilationUnit compilationUnit = JDTSupport.loadCompilationUnit(compilationUnitFile); compilationUnit.recordModifications(); ASTVisitor visitor = new BasicJDTASTVisitor(context) { @SuppressWarnings("unchecked") public boolean visit(FieldDeclaration decl) { super.visit(decl); AST ast = decl.getAST(); // making protected fields duringInterpretation,value,visited // and all others (!) public TypeDeclaration typeDecl = (TypeDeclaration) decl.getParent(); String ident = typeDecl.getName().getIdentifier(); if (("ASTNode$State").equals(ident) || ("CircularValue").equals(ident)) { ListIterator<?> it = decl.modifiers().listIterator(); boolean isPublicOrPrivate = false; while (it.hasNext()) { Modifier modifier = (Modifier) it.next(); if (modifier.isPublic() || modifier.isPrivate()) { isPublicOrPrivate = true; break; } if (modifier.isProtected()) { modifier.setKeyword(Modifier.ModifierKeyword.PUBLIC_KEYWORD); isPublicOrPrivate = true; break; } } if (!isPublicOrPrivate) { Modifier newModifier = ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD); decl.modifiers().add(newModifier); } } return false; } @SuppressWarnings("unchecked") public boolean visit(TypeDeclaration decl) { if ("IdentityHashSet".equals(decl.getName().getIdentifier())) { Modifier modifier = JDTSupport.findModifier(decl, ModifierKeyword.PROTECTED_KEYWORD); if (modifier != null) { modifier.setKeyword(ModifierKeyword.PUBLIC_KEYWORD); } else { Modifier newModifier = decl.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD); decl.modifiers().add(newModifier); } } return true; } }; //compilationUnit.accept(visitor); JDTSupport.applyRewritesAndSave(compilationUnit, compilationUnitFile); }
From source file:org.jastemf.refactorings.RefactoringManager.java
License:BSD License
/** * <p>//from w ww . j a v a2s . com * Execute adaptations of the <tt>ASTNode</tt> class JastAdd generates. * </p> * The following adaptations are conducted: * <ul> * <li><tt>EObjectImpl</tt> is set as super type.</li> * <li>Change visibility of <tt>ASTNode.childIndex</tt> from private to * public.</li> * <li>Delete the children array.</li> * <li>Additionally: See {@link BasicJDTASTVisitor}</li> * </ul> * * @param context * The current integration context. * @throws JastEMFException * Thrown, iff an IO error occurs or an adaptation cannot be * applied. */ public static void performASTNodeAdaptations(final IIntegrationContext context) throws JastEMFException { final IFile compilationUnitFile = IOSupport.getFile(context.astfolder(), "ASTNode.java"); final CompilationUnit compilationUnit = JDTSupport.loadCompilationUnit(compilationUnitFile); compilationUnit.recordModifications(); ASTVisitor visitor = new BasicJDTASTVisitor(context) { public boolean visit(TypeDeclaration decl) { AST ast = decl.getAST(); if (!decl.isInterface()) { if ("ASTNode".equals(decl.getName().getIdentifier())) { decl.setSuperclassType( ast.newSimpleType(ast.newName("org.eclipse.emf.ecore.impl.EObjectImpl"))); return true; } } return false; } public boolean visit(VariableDeclarationFragment declFragment) { if ("childIndex".equals(declFragment.getName().getIdentifier())) { FieldDeclaration decl = null; if (declFragment.getParent() instanceof FieldDeclaration) { decl = (FieldDeclaration) declFragment.getParent(); } else { //This is just a debug information for users. IOSupport.warn("Declaration of childIndex is not a FieldDeclaration but a " + declFragment.getParent().getClass().getName() + ". This may be caused by a declaration of a local variable with the same name. " + "Ignoring this declaration."); return false; } for (Object o : decl.modifiers()) { Modifier modifier = (Modifier) o; if (modifier.isPrivate()) { modifier.setKeyword(Modifier.ModifierKeyword.PUBLIC_KEYWORD); break; } } } else if (("children").equals(declFragment.getName().getIdentifier())) { FieldDeclaration decl = (FieldDeclaration) declFragment.getParent(); decl.delete(); } return false; } }; IOSupport.log("Visiting ASTNode ..."); //compilationUnit.accept(visitor); JDTSupport.applyRewritesAndSave(compilationUnit, compilationUnitFile); }