/** * Creates a method with a body for the getter. * * @return getter body */ private String createGetterBody() { // TODO: use access modifier given by user int modifier = this.getGetterVisibility(); if (field.isStatic()) { modifier |= BinModifier.STATIC; } BinMethod getter = new BinMethod( getterName, BinParameter.NO_PARAMS, getField().getTypeRef(), modifier, BinMethod.Throws.NO_THROWS); getter.setOwner(field.getOwner()); BinMethodFormatter formatter = (BinMethodFormatter) getter.getFormatter(); String result = FormatSettings.LINEBREAK + formatter.formHeader(); // TODO migrate to formatting engine also? int baseIndent = new BinTypeFormatter(field.getOwner().getBinCIType()).getMemberIndent(); result += FormatSettings.getIndentString(baseIndent + FormatSettings.getBlockIndent()); result += "return "; if (field.isStatic()) { result += hostingClass.getName(); } else { result += "this"; } result += "." + getField().getName() + ";"; result += FormatSettings.LINEBREAK; result += formatter.formFooter(); return result; }
public ManagingIndexer getSupervisor(BinField field) { if ((supervisor == null) || (accessorsChanged)) { supervisor = new ManagingIndexer(); new EncapsulateFieldIndexer(supervisor, field, getterName, setterName); // Find usages if (field.isPrivate()) { supervisor.visit(field.getCompilationUnit()); } else { supervisor.visit(field.getOwner().getProject()); } } this.accessorsChanged = false; return supervisor; }
public EncapsulateField(RefactorItContext context, BinField field) { super("EncapsulateField", context); this.field = field; setterName = PropertyNameUtil.getDefaultSetterName(field); getterName = PropertyNameUtil.getDefaultGetterName(field)[0]; hostingClass = field.getOwner().getBinCIType(); }
/** @return setter for curent encapsulation, null if not present */ private BinMethod getSetterMethod() { return hostingClass.getDeclaredMethod( setterName, new BinParameter[] {new BinParameter(field.getName(), field.getTypeRef(), 0)}); }
/** @see net.sf.refactorit.refactorings.Refactoring#performChange */ public TransformationList performChange() { TransformationList transList = new TransformationList(); if (this.fieldVisibility != -1) { int changedAccessModifier = -1; MemberVisibilityAnalyzer accessAnalyzer = new MemberVisibilityAnalyzer(this.field); int allowedAccess = accessAnalyzer.getPosterioriFieldAccess(this.getAllUsages(), this.usages); if (this.fieldVisibility == BinModifier.PRIVATE) { if (allowedAccess != BinModifier.PRIVATE) { changedAccessModifier = allowedAccess; } } else if (this.fieldVisibility == BinModifier.PACKAGE_PRIVATE) { if ((allowedAccess != BinModifier.PRIVATE) && (allowedAccess != BinModifier.PACKAGE_PRIVATE)) { changedAccessModifier = allowedAccess; } } else if (this.fieldVisibility == BinModifier.PROTECTED) { if (allowedAccess == BinModifier.PUBLIC) { changedAccessModifier = allowedAccess; } } if (changedAccessModifier != -1) { transList .getStatus() .addEntry( "Couldn't change field access to " + getAccessName(fieldVisibility) + ".\n" + "Using " + getAccessName(changedAccessModifier) + " access instead.", RefactoringStatus.WARNING); this.fieldVisibility = changedAccessModifier; } } Map usageMap = new HashMap(); for (int i = 0; i < usages.size(); ++i) { EncapsulationInvocationData id = (EncapsulationInvocationData) usages.get(i); if (id.isEncapsulationPossible()) { CompilationUnit sf = id.getCompilationUnit(); List usagesInSource = (List) usageMap.get(sf); if (usagesInSource == null) { usagesInSource = new ArrayList(); usageMap.put(sf, usagesInSource); } usagesInSource.add(id); } } for (Iterator i = usageMap.keySet().iterator(); i.hasNext(); ) { CompilationUnit compilationUnit = (CompilationUnit) i.next(); new EncapsulateEditor(field, getterName, setterName, (List) usageMap.get(compilationUnit)) .generateEditors(transList); } int column = 0; int line = 0; BinClass hostingClass = (BinClass) getField().getOwner().getBinCIType(); line = hostingClass.getEndLine(); StringBuffer buffer = new StringBuffer(); if (encapsulateRead) { BinMethod getter = getGetterMethod(); if (getter == null) { buffer.append(createGetterBody()); } else { transList.add( new ModifierEditor( getter, BinModifier.setFlags(getter.getModifiers(), this.getGetterVisibility()))); } } if (encapsulateWrite) { BinMethod setter = getSetterMethod(); if (setter == null) { buffer.append(createSetterBody()); } else { transList.add( new ModifierEditor( setter, BinModifier.setFlags(setter.getModifiers(), this.getSetterVisibility()))); } } if (buffer.length() > 0) { StringInserter inserter = new StringInserter(hostingClass.getCompilationUnit(), line, column, buffer.toString()); transList.add(inserter); } if (this.fieldVisibility != -1) { transList.add( new ModifierEditor(field, BinModifier.setFlags(field.getModifiers(), fieldVisibility))); } return transList; }