Esempio n. 1
0
  public void setUp() {
    if (p == null) {
      try {
        p =
            MinimizeAccessRightsTest.getProjectForTest(
                MinimizeAccessRightsTest.checkPossibleEjbMethodsTest);
      } catch (Exception e) {
        throw new ChainableRuntimeException(e);
      }

      beanRef = p.getTypeRefForName("a.MainBean");
      bean = (BinClass) beanRef.getBinCIType();
      ejbFindAll = bean.getDeclaredMethod("ejbFindAll", BinTypeRef.NO_TYPEREFS);
      ejbSelect = bean.getDeclaredMethod("ejbSelect", BinTypeRef.NO_TYPEREFS);
      toStringMethod = bean.getDeclaredMethod("toString", BinTypeRef.NO_TYPEREFS);
    }
  }
  @Override
  public boolean prepareSymptom(Symptom symptom) throws RefactoringException {
    loadEnvironment();
    //        reloadEnv();

    CreateFactoryMethodSymptom createFactoryMethodSymptom = (CreateFactoryMethodSymptom) symptom;

    currentClass =
        getProject()
            .getTypeRefForSourceName(createFactoryMethodSymptom.getParentQualifiedName())
            .getBinCIType();

    BinClass cls = (BinClass) currentClass;
    BinMember member = cls.getConstructor(createFactoryMethodSymptom.getParametersTypes());
    if (member == null) {
      member = getConstructorManually(currentClass, createFactoryMethodSymptom.getBinParameters());
    }
    currentConstructor = (BinConstructor) member;

    return reloadRefactoring();
  }
  @Override
  public List<Symptom> findAllSymptoms() throws RefactoringException {
    if (!loadEnvironment()) {
      return null;
    }

    List<Symptom> result = new ArrayList<Symptom>();

    for (Object object : getProject().getDefinedTypes()) {
      BinCITypeRef binCITypeRef = (BinCITypeRef) object;
      BinCIType bcitr = binCITypeRef.getBinCIType();

      if (bcitr.isInterface()) {
        continue;
      }

      if (bcitr.isInnerType() && !bcitr.isStatic()) {
        continue;
      }

      if (bcitr.isAbstract()) {
        continue;
      }

      if (!classShouldBeVerified(binCITypeRef)) {
        continue;
      }

      BinClass cls = (BinClass) bcitr;
      BinConstructor[] constructors = cls.getConstructors();
      for (BinConstructor binConstructor : constructors) {
        Symptom symptom = new CreateFactoryMethodSymptom(binConstructor, this);
        result.add(symptom);
      }
    }
    return result;
  }
  /** @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;
  }
Esempio n. 5
0
 public void testIsEntityBean() throws Exception {
   assertTrue(EjbUtil.isEnterpriseBean(bean.getTypeRef()));
   assertFalse(EjbUtil.isEnterpriseBean(p.getObjectRef()));
 }