private VariableDeclarationStatement createTempDeclaration(Expression initializer) throws CoreException { AST ast = fCURewrite.getAST(); VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName(fTempName)); vdf.setInitializer(initializer); VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf); if (fDeclareFinal) { vds.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD)); } vds.setType(createTempType()); if (fLinkedProposalModel != null) { ASTRewrite rewrite = fCURewrite.getASTRewrite(); LinkedProposalPositionGroup nameGroup = fLinkedProposalModel.getPositionGroup(KEY_NAME, true); nameGroup.addPosition(rewrite.track(vdf.getName()), true); String[] nameSuggestions = guessTempNames(); if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fTempName)) { nameGroup.addProposal(fTempName, null, nameSuggestions.length + 1); } for (int i = 0; i < nameSuggestions.length; i++) { nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i); } } return vds; }
public TypeDeclaration createBuilderClass(AST ast, TypeDeclaration originalType) { TypeDeclaration builderType = ast.newTypeDeclaration(); builderType.setName(ast.newSimpleName(getBuilderName(originalType))); if (preferencesManager.getPreferenceValue(ADD_GENERATED_ANNOTATION)) { generatedAnnotationPopulator.addGeneratedAnnotation(ast, builderType); } builderType.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); builderType.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD)); builderType.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD)); if (preferencesManager.getPreferenceValue(GENERATE_JAVADOC_ON_BUILDER_CLASS)) { Javadoc javadoc = javadocGenerator.generateJavadoc(ast, String.format(Locale.ENGLISH, "Builder to build {@link %s}.", originalType.getName().toString()), Collections.emptyMap()); builderType.setJavadoc(javadoc); } return builderType; }
private MethodDeclaration createNewWithMethod(AST ast, String fieldName, Block newBlock, SingleVariableDeclaration methodParameterDeclaration, TypeDeclaration builderType, BuilderField builderField) { MethodDeclaration builderMethod = ast.newMethodDeclaration(); builderMethod.setName(ast.newSimpleName(builderClassMethodNameGeneratorService.build(fieldName))); builderMethod.setReturnType2(ast.newSimpleType( ast.newName(builderType.getName().getIdentifier()))); builderMethod.setBody(newBlock); builderMethod.parameters().add(methodParameterDeclaration); javadocAdder.addJavadocForWithMethod(ast, fieldName, builderMethod); if (preferencesManager.getPreferenceValue(ADD_NONNULL_ON_RETURN)) { markerAnnotationAttacher.attachNonNull(ast, builderMethod); } builderMethod.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); return builderMethod; }
public MethodDeclaration createNewWithMethod(AST ast, BuilderField builderField, StagedBuilderProperties nextStage) { String fieldName = builderField.getBuilderFieldName(); MethodDeclaration builderMethod = ast.newMethodDeclaration(); builderMethod.setName(ast.newSimpleName(builderClassMethodNameGeneratorService.build(fieldName))); builderMethod.setReturnType2(ast.newSimpleType(ast.newName(nextStage.getInterfaceName()))); builderMethod.parameters() .add(withMethodParameterCreatorFragment.createWithMethodParameter(ast, builderField.getFieldType(), fieldName)); if (preferencesManager.getPreferenceValue(ADD_NONNULL_ON_RETURN)) { markerAnnotationAttacher.attachNonNull(ast, builderMethod); } builderMethod.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); return builderMethod; }
@SuppressWarnings("unchecked") public MethodDeclaration createPrivateConstructorDefinition(AST ast, TypeDeclaration originalType, TypeDeclaration builderType, List<BuilderField> builderFields) { MethodDeclaration method = ast.newMethodDeclaration(); method.setConstructor(true); method.setName(ast.newSimpleName(originalType.getName().toString())); if (preferencesManager.getPreferenceValue(ADD_GENERATED_ANNOTATION)) { generatedAnnotationPopulator.addGeneratedAnnotation(ast, method); } method.modifiers().add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD)); SingleVariableDeclaration methodParameterDeclaration = ast.newSingleVariableDeclaration(); methodParameterDeclaration.setType(ast.newSimpleType(ast.newName(builderType.getName().toString()))); methodParameterDeclaration.setName(ast.newSimpleName(camelCaseConverter.toLowerCamelCase(builderType.getName().toString()))); method.parameters().add(methodParameterDeclaration); return method; }
public void addBuilderMethodToCompilationUnit(CompilationUnitModificationDomain modificationDomain, TypeDeclaration builderType, StagedBuilderProperties currentStage) { AST ast = modificationDomain.getAst(); ListRewrite listRewrite = modificationDomain.getListRewrite(); BuilderField firstField = currentStage.getNamedVariableDeclarationField().get(0); StagedBuilderProperties nextStage = currentStage.getNextStage().orElse(currentStage); MethodDeclaration staticWithMethod = stagedBuilderWithMethodDefiniationCreatorFragment.createNewWithMethod(ast, firstField, nextStage); staticWithMethod.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD)); String parameterName = firstField.getBuilderFieldName(); String withMethodName = staticWithMethod.getName().toString(); Block block = newBuilderAndWithMethodCallCreationFragment.createReturnBlock(ast, builderType, withMethodName, parameterName); javadocAdder.addJavadocForWithBuilderMethod(ast, builderType.getName().toString(), parameterName, staticWithMethod); staticWithMethod.setBody(block); listRewrite.insertLast(staticWithMethod, null); }
public static void main(String[] args) throws Exception { new SearchIn(new JavaParser().parseCompilationUnit(new File("./..."))) .inClass("Bla") .isMethod("blubb").withArgumentTypes(String.class, Integer.class) .withModifiers(ModifierKeyword.PUBLIC_KEYWORD) .withStatement(and(waitStatement(), notifyStatement())) .searchThis(); new SearchIn(new JavaParser().parseCompilationUnit(new File("./..."))) .inClass("") .isAnonymousClass() .andIsAnonymousClass() .searchThis(); new SearchIn(null) .inClass("*") .isAnonymousClass(); new SearchIn(null) .inClass("*") .inInnerClass("*"); }
private void adjustOutgoingVisibilityChain(final IMember member, final IProgressMonitor monitor) throws JavaModelException { if (!Modifier.isPublic(member.getFlags())) { final ModifierKeyword threshold = computeOutgoingVisibilityThreshold(fReferencing, member, monitor); if (member instanceof IMethod) { adjustOutgoingVisibility( member, threshold, RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_method_warning); } else if (member instanceof IField) { adjustOutgoingVisibility((IField) member, threshold); } else if (member instanceof IType) { adjustOutgoingVisibility( member, threshold, RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_type_warning); } } if (member.getDeclaringType() != null) adjustOutgoingVisibilityChain(member.getDeclaringType(), monitor); }
private void addAdjustment( IMember whoToAdjust, ModifierKeyword neededVisibility, Map<IMember, IncomingMemberVisibilityAdjustment> adjustments) throws JavaModelException { ModifierKeyword currentVisibility = ModifierKeyword.fromFlagValue(JdtFlags.getVisibilityCode(whoToAdjust)); if (MemberVisibilityAdjustor.hasLowerVisibility(currentVisibility, neededVisibility) && MemberVisibilityAdjustor.needsVisibilityAdjustments( whoToAdjust, neededVisibility, adjustments)) adjustments.put( whoToAdjust, new MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment( whoToAdjust, neededVisibility, RefactoringStatus.createWarningStatus( Messages.format( MemberVisibilityAdjustor.getMessage(whoToAdjust), new String[] { MemberVisibilityAdjustor.getLabel(whoToAdjust), MemberVisibilityAdjustor.getLabel(neededVisibility) }), JavaStatusContext.create(whoToAdjust)))); }
private List<ResourceChange> createParameterObject(ParameterObjectFactory pof, IPackageFragmentRoot packageRoot) throws CoreException { FieldUpdate fieldUpdate= new FieldUpdate(); if (fDescriptor.isCreateTopLevel()) return pof.createTopLevelParameterObject(packageRoot, fieldUpdate); else { CompilationUnit root= fBaseCURewrite.getRoot(); TypeDeclaration typeDecl= ASTNodeSearchUtil.getTypeDeclarationNode(fDescriptor.getType(), root); ASTRewrite rewrite= fBaseCURewrite.getASTRewrite(); ListRewrite listRewrite= rewrite.getListRewrite(typeDecl, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); TypeDeclaration paramClass= pof.createClassDeclaration(typeDecl.getName().getFullyQualifiedName(), fBaseCURewrite, fieldUpdate); paramClass.modifiers().add(rewrite.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD)); if (shouldParamClassBeStatic(typeDecl)) { paramClass.modifiers().add(rewrite.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD)); } listRewrite.insertFirst(paramClass, fBaseCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_insert_parameter)); return new ArrayList<ResourceChange>(); //Change will be generated later for fBaseCURewrite } }
private FieldDeclaration createField(ParameterInfo pi, CompilationUnitRewrite cuRewrite) throws CoreException { AST ast= cuRewrite.getAST(); ICompilationUnit unit= cuRewrite.getCu(); VariableDeclarationFragment fragment= ast.newVariableDeclarationFragment(); String lineDelim= StubUtility.getLineDelimiterUsed(unit); SimpleName fieldName= ast.newSimpleName(pi.getNewName()); fragment.setName(fieldName); FieldDeclaration declaration= ast.newFieldDeclaration(fragment); if (createComments(unit.getJavaProject())) { String comment= StubUtility.getFieldComment(unit, pi.getNewTypeName(), pi.getNewName(), lineDelim); if (comment != null) { Javadoc doc= (Javadoc) cuRewrite.getASTRewrite().createStringPlaceholder(comment, ASTNode.JAVADOC); declaration.setJavadoc(doc); } } List<Modifier> modifiers= new ArrayList<Modifier>(); if (fCreateGetter) { modifiers.add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD)); } else { modifiers.add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); } declaration.modifiers().addAll(modifiers); declaration.setType(importBinding(pi.getNewTypeBinding(), cuRewrite)); return declaration; }
private void adjustOutgoingVisibilityChain(final IMember member, final IProgressMonitor monitor) throws JavaModelException { if (!Modifier.isPublic(member.getFlags())) { final ModifierKeyword threshold= computeOutgoingVisibilityThreshold(fReferencing, member, monitor); if (member instanceof IMethod) { adjustOutgoingVisibility(member, threshold, RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_method_warning); } else if (member instanceof IField) { adjustOutgoingVisibility((IField) member, threshold); } else if (member instanceof IType) { adjustOutgoingVisibility(member, threshold, RefactoringCoreMessages.MemberVisibilityAdjustor_change_visibility_type_warning); } } if (member.getDeclaringType() != null) adjustOutgoingVisibilityChain(member.getDeclaringType(), monitor); }
/** * Returns the visibility threshold from a type to a field. * * @param referencing the referencing type * @param referenced the referenced field * @param monitor the progress monitor to use * @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility * @throws JavaModelException if the java elements could not be accessed */ private ModifierKeyword thresholdTypeToField(final IType referencing, final IField referenced, final IProgressMonitor monitor) throws JavaModelException { ModifierKeyword keyword= ModifierKeyword.PUBLIC_KEYWORD; final ICompilationUnit referencedUnit= referenced.getCompilationUnit(); if (referenced.getDeclaringType().equals(referencing)) keyword= ModifierKeyword.PRIVATE_KEYWORD; else { final ITypeHierarchy hierarchy= getTypeHierarchy(referencing, new SubProgressMonitor(monitor, 1)); final IType[] types= hierarchy.getSupertypes(referencing); IType superType= null; for (int index= 0; index < types.length; index++) { superType= types[index]; if (superType.equals(referenced.getDeclaringType())) { keyword= ModifierKeyword.PROTECTED_KEYWORD; return keyword; } } } final ICompilationUnit typeUnit= referencing.getCompilationUnit(); if (referencedUnit != null && referencedUnit.equals(typeUnit)) keyword= ModifierKeyword.PRIVATE_KEYWORD; else if (referencedUnit != null && typeUnit != null && referencedUnit.getParent().equals(typeUnit.getParent())) keyword= null; return keyword; }
private VariableDeclarationStatement createTempDeclaration(Expression initializer) throws CoreException { AST ast= fCURewrite.getAST(); VariableDeclarationFragment vdf= ast.newVariableDeclarationFragment(); vdf.setName(ast.newSimpleName(fTempName)); vdf.setInitializer(initializer); VariableDeclarationStatement vds= ast.newVariableDeclarationStatement(vdf); if (fDeclareFinal) { vds.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD)); } vds.setType(createTempType()); if (fLinkedProposalModel != null) { ASTRewrite rewrite= fCURewrite.getASTRewrite(); LinkedProposalPositionGroup nameGroup= fLinkedProposalModel.getPositionGroup(KEY_NAME, true); nameGroup.addPosition(rewrite.track(vdf.getName()), true); String[] nameSuggestions= guessTempNames(); if (nameSuggestions.length > 0 && !nameSuggestions[0].equals(fTempName)) { nameGroup.addProposal(fTempName, null, nameSuggestions.length + 1); } for (int i= 0; i < nameSuggestions.length; i++) { nameGroup.addProposal(nameSuggestions[i], null, nameSuggestions.length - i); } } return vds; }
/** * @return a statement in form of <code>final int maxLen = 10;</code> */ protected VariableDeclarationStatement createMaxLenDeclaration() { VariableDeclarationFragment fragment= fAst.newVariableDeclarationFragment(); fragment.setName(fAst.newSimpleName(fMaxLenVariableName)); fragment.setInitializer(fAst.newNumberLiteral(String.valueOf(fContext.getLimitItemsValue()))); VariableDeclarationStatement declExpression= fAst.newVariableDeclarationStatement(fragment); declExpression.setType(fAst.newPrimitiveType(PrimitiveType.INT)); declExpression.modifiers().add(fAst.newModifier(ModifierKeyword.FINAL_KEYWORD)); return declExpression; }
/** * Makes an attribute constant. */ @SuppressWarnings("unchecked") public void makeConstant() { List<IExtendedModifier> modifierList = fieldDeclaration.modifiers(); logger.debug("Trying to make attribute '" + attributeName + "' constant..."); /* * Test if modifier already exists. If they don't exist than add them to the modifier list. */ if (testIfModifierExists(ModifierKeyword.STATIC_KEYWORD, modifierList)) { logger.debug("modifier 'static' already exists"); } else { modifierList.add(fieldDeclaration.getAST().newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD)); logger.debug("Added 'static' modifier to '" + attributeName + "'"); } if (testIfModifierExists(ModifierKeyword.FINAL_KEYWORD, modifierList)) { logger.debug("modifier 'final' already exists..."); } else { modifierList.add(fieldDeclaration.getAST().newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD)); logger.debug("Added 'final' modifier to '" + attributeName + "'"); } notifyChanged(); }
/** * Makes a constant attribute variable. */ @SuppressWarnings("unchecked") public void makeVariable() { List<IExtendedModifier> modifierList = fieldDeclaration.modifiers(); List<IExtendedModifier> removeList = new ArrayList<IExtendedModifier>(); logger.debug("Trying to make attribute '" + attributeName + "' variable..."); for (Object modifier : modifierList) { /* * Mark modifier for deletion if it exists. To avoid NoSuchElementException this modifier will be put in a * separate list and will be removed later. */ if (modifier.toString().equals(Modifier.ModifierKeyword.FINAL_KEYWORD.toString()) || modifier.toString().equals(Modifier.ModifierKeyword.STATIC_KEYWORD.toString())) { logger.debug("'" + modifier + "' flagged for deletion..."); removeList.add((IExtendedModifier) modifier); } } logger.debug("Remove modifiers..."); modifierList.removeAll(removeList); notifyChanged(); }
private List<ResourceChange> createParameterObject(ParameterObjectFactory pof, IPackageFragmentRoot packageRoot) throws CoreException { FieldUpdate fieldUpdate= new FieldUpdate(); if (fDescriptor.isCreateTopLevel()) return pof.createTopLevelParameterObject(packageRoot, fieldUpdate); else { CompilationUnit root= fBaseCURewrite.getRoot(); TypeDeclaration typeDecl= (TypeDeclaration) NodeFinder.perform(root, fDescriptor.getType().getSourceRange()); ASTRewrite rewrite= fBaseCURewrite.getASTRewrite(); ListRewrite listRewrite= rewrite.getListRewrite(typeDecl, TypeDeclaration.BODY_DECLARATIONS_PROPERTY); TypeDeclaration paramClass= pof.createClassDeclaration(typeDecl.getName().getFullyQualifiedName(), fBaseCURewrite, fieldUpdate); paramClass.modifiers().add(rewrite.getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD)); paramClass.modifiers().add(rewrite.getAST().newModifier(ModifierKeyword.STATIC_KEYWORD)); listRewrite.insertFirst(paramClass, fBaseCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractClassRefactoring_group_insert_parameter)); return new ArrayList<ResourceChange>(); //Change will be generated later for fBaseCURewrite } }
protected void updateLocalVariableDeclarations(final ASTRewrite rewrite, final Set<String> variables, Block block) { Assert.isNotNull(rewrite); Assert.isNotNull(block); Assert.isNotNull(variables); final AST ast = rewrite.getAST(); block.accept(new ASTVisitor() { @Override public boolean visit(VariableDeclarationFragment fragment) { if (variables.contains(fragment.getName().getFullyQualifiedName())) { ASTNode parent = fragment.getParent(); if (parent instanceof VariableDeclarationStatement) { ListRewrite listRewrite = rewrite .getListRewrite(parent, VariableDeclarationStatement.MODIFIERS2_PROPERTY); listRewrite.insertLast(ast.newModifier(ModifierKeyword.FINAL_KEYWORD), null); } } return true; } }); }
/** * Sets the public modifier and deletes the protected one. */ private void setPublic(MethodDeclaration md) { // if already public, do nothing if (isPublic(md)) { return; } // set public Modifier modifier = ast.newModifier(ModifierKeyword.fromFlagValue(Modifier.PUBLIC)); md.modifiers().add(0, modifier); // delete protected for (int i = 0; i < md.modifiers().size(); i++) { if (((IExtendedModifier)md.modifiers().get(i)).isModifier()) { Modifier m = (Modifier)md.modifiers().get(i); if (m.isProtected()) { m.delete(); break; } } } }
@SuppressWarnings("unchecked") public void writeLabels(Collection<String> labels) { if (labels.isEmpty()) return; EnumDeclaration enumType = getAST().newEnumDeclaration(); enumType.setName(getAST().newSimpleName("TAG")); enumType.modifiers().add(getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); enumType.modifiers().add(getAST().newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD)); // elements int num = 0; for (String label : labels) { EnumConstantDeclaration constantDeclaration = getAST().newEnumConstantDeclaration(); constantDeclaration.setName(getAST().newSimpleName(normalizeEnumName(label))); enumType.enumConstants().add(num, constantDeclaration); num++; } getTarget().bodyDeclarations().add(enumType); }
@SuppressWarnings("unchecked") public void writeMessages(Collection<String> messages) { EnumDeclaration enumType = getAST().newEnumDeclaration(); enumType.setName(getAST().newSimpleName("QCPFMSG")); enumType.modifiers().add(getAST().newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD)); enumType.modifiers().add(getAST().newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD)); // elements int num = 0; for (String message : messages) { if (message.equalsIgnoreCase("CPF0000")) continue; EnumConstantDeclaration constantDeclaration = getAST().newEnumConstantDeclaration(); constantDeclaration.setName(getAST().newSimpleName(normalizeEnumName(message))); enumType.enumConstants().add(num, constantDeclaration); num++; } getTarget().bodyDeclarations().add(enumType); }
@SuppressWarnings("unchecked") public void writeCursors(List<QCursorTerm> cursors) { writeImport(QCursor.class); writeImport(CursorType.class); for (QCursorTerm cursorTerm : cursors) { VariableDeclarationFragment variable = getAST().newVariableDeclarationFragment(); FieldDeclaration field = getAST().newFieldDeclaration(variable); if (cursorTerm.isHold()) writeAnnotation(field, CursorDef.class, "hold", cursorTerm.isHold()); writeAnnotation(field, CursorDef.class, "type", cursorTerm.getCursorType()); field.modifiers().add(getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD)); Type dataSetType = getAST().newSimpleType(getAST().newSimpleName(QCursor.class.getSimpleName())); field.setType(dataSetType); variable.setName(getAST().newSimpleName(getCompilationUnit().normalizeTermName(cursorTerm.getName()))); getTarget().bodyDeclarations().add(field); } }
@SuppressWarnings("unchecked") public void writeStatements(List<QStatementTerm> statements) { writeImport(QStatement.class); for (QStatementTerm statementTerm : statements) { VariableDeclarationFragment variable = getAST().newVariableDeclarationFragment(); FieldDeclaration field = getAST().newFieldDeclaration(variable); field.modifiers().add(getAST().newModifier(ModifierKeyword.PUBLIC_KEYWORD)); Type dataSetType = getAST().newSimpleType(getAST().newSimpleName(QStatement.class.getSimpleName())); field.setType(dataSetType); variable.setName(getAST().newSimpleName(getCompilationUnit().normalizeTermName(statementTerm.getName()))); getTarget().bodyDeclarations().add(field); } }
private boolean isStatic(FieldDeclaration field) { List<IExtendedModifier> fieldModifiers = field.modifiers(); return fieldModifiers.stream() .filter(modifier -> modifier instanceof Modifier) .filter(modifer -> ((Modifier) modifer).getKeyword().equals(ModifierKeyword.STATIC_KEYWORD)) .findFirst() .isPresent(); }
public MethodDeclaration createMethod(AST ast, TypeDeclaration originalType) { MethodDeclaration method = ast.newMethodDeclaration(); method.setName(ast.newSimpleName(getBuildMethodName(originalType))); method.setReturnType2(ast.newSimpleType(ast.newName(originalType.getName().toString()))); if (preferencesManager.getPreferenceValue(ADD_NONNULL_ON_RETURN)) { markerAnnotationAttacher.attachNonNull(ast, method); } method.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); return method; }
public TypeDeclaration createStageBuilderInterface(AST ast, String interfaceName) { TypeDeclaration addedInterface = ast.newTypeDeclaration(); addedInterface.setInterface(true); addedInterface.setName(ast.newSimpleName(interfaceName)); addedInterface.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); javadocAdder.addJavadocForStagedInterface(ast, interfaceName, addedInterface); return addedInterface; }
public void addEmptyPrivateConstructor(AST ast, TypeDeclaration builderType) { MethodDeclaration privateConstructorMethod = ast.newMethodDeclaration(); privateConstructorMethod.setBody(ast.newBlock()); privateConstructorMethod.setConstructor(true); privateConstructorMethod.setName(ast.newSimpleName(builderType.getName().toString())); privateConstructorMethod.modifiers().add(ast.newModifier(ModifierKeyword.PRIVATE_KEYWORD)); builderType.bodyDeclarations().add(privateConstructorMethod); }
@SuppressWarnings("unchecked") public MethodDeclaration createBuilderMethod(AST ast, TypeDeclaration originalType, String builderName) { MethodDeclaration builderMethod = ast.newMethodDeclaration(); builderMethod.setName(ast.newSimpleName(getBuilderMethodName(originalType))); addGenerateAnnotationIfNeeded(ast, builderMethod); builderMethod.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD)); builderMethod.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD)); builderMethod.setReturnType2(ast.newSimpleType(ast.newName(builderName))); javadocAdder.addJavadocForBuilderMethod(ast, originalType.getName().toString(), builderMethod); return builderMethod; }
@Override public String getDisplayString() { if (fModifier == 0) { return CorrectionMessages.ModifierCorrectionSubProcessor_default_visibility_label; } else { return ModifierKeyword.fromFlagValue(fModifier).toString(); } }
private static Modifier findVisibilityModifier(List<IExtendedModifier> modifiers) { for (int i = 0; i < modifiers.size(); i++) { IExtendedModifier curr = modifiers.get(i); if (curr instanceof Modifier) { Modifier modifier = (Modifier) curr; ModifierKeyword keyword = modifier.getKeyword(); if (keyword == ModifierKeyword.PUBLIC_KEYWORD || keyword == ModifierKeyword.PROTECTED_KEYWORD || keyword == ModifierKeyword.PRIVATE_KEYWORD) { return modifier; } } } return null; }
@SuppressWarnings("unchecked") private void removeModifierKeyword(MethodDeclaration methodDeclaration, ModifierKeyword modifierKeyword, ASTRewrite rewrite) { ListRewrite listRewrite = rewrite.getListRewrite(methodDeclaration, methodDeclaration.getModifiersProperty()); listRewrite.getOriginalList().stream().filter(o -> o instanceof Modifier).map(Modifier.class::cast) .filter(m -> ((Modifier) m).getKeyword().equals(modifierKeyword)).findAny() .ifPresent(m -> listRewrite.remove((ASTNode) m, null)); }
public MethodExpression(final String methodName) { this.fMethodName = methodName; this.fExpressions = new ArrayList<IEvaluable<Block>>(); this.fParameters = Collections.emptyList(); this.fModifiers = new ArrayList<ModifierKeyword>(1); }
@SuppressWarnings("unchecked") //keine Generics in ASTParser private boolean hasModifiers(final List modifiers) { for(ModifierKeyword modifierWanted : fModifiers) { if(!containsModifier(modifiers, modifierWanted)) { return false; //ein modifier nicht vorhanden } } //invariante: alle modifier die vorhanden sein sollten waren in actual liste return true; }
@SuppressWarnings("unchecked") //keine Generics in ASTParser private boolean containsModifier(final List modifiers, final ModifierKeyword keyword) { for(int i=0;i<modifiers.size();i++) { Object obj = modifiers.get(i); if(obj instanceof Modifier) { Modifier modifier = (Modifier)obj; if(modifier.getKeyword().equals(keyword)) { return true; } } } return false; }