コード例 #1
0
 private static ASTNode getRawReference(SimpleName name, CompilationUnit compilationUnit) {
   SimpleName[] names = LinkedNodeFinder.findByNode(compilationUnit, name);
   for (int j = 0; j < names.length; j++) {
     if (names[j].getParent() instanceof VariableDeclarationFragment) {
       VariableDeclarationFragment fragment = (VariableDeclarationFragment) names[j].getParent();
       if (fragment.getParent() instanceof VariableDeclarationStatement) {
         VariableDeclarationStatement statement =
             (VariableDeclarationStatement) fragment.getParent();
         ASTNode result =
             (ASTNode) statement.getStructuralProperty(VariableDeclarationStatement.TYPE_PROPERTY);
         if (isRawTypeReference(result)) return result;
       } else if (fragment.getParent() instanceof FieldDeclaration) {
         FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
         ASTNode result =
             (ASTNode) declaration.getStructuralProperty(FieldDeclaration.TYPE_PROPERTY);
         if (isRawTypeReference(result)) return result;
       }
     } else if (names[j].getParent() instanceof SingleVariableDeclaration) {
       SingleVariableDeclaration declaration = (SingleVariableDeclaration) names[j].getParent();
       ASTNode result =
           (ASTNode) declaration.getStructuralProperty(SingleVariableDeclaration.TYPE_PROPERTY);
       if (isRawTypeReference(result)) return result;
     } else if (names[j].getParent() instanceof MethodDeclaration) {
       MethodDeclaration methodDecl = (MethodDeclaration) names[j].getParent();
       ASTNode result =
           (ASTNode) methodDecl.getStructuralProperty(MethodDeclaration.RETURN_TYPE2_PROPERTY);
       if (isRawTypeReference(result)) return result;
     }
   }
   return null;
 }
コード例 #2
0
 private String getFieldName(VariableDeclarationFragment node) {
   StringBuffer buffer = new StringBuffer();
   buffer.append(node.getName().toString());
   ASTNode parent = node.getParent();
   if (parent instanceof FieldDeclaration) {
     FieldDeclaration fd = (FieldDeclaration) parent;
     buffer.append(" : "); // $NON-NLS-1$
     buffer.append(getType(fd.getType()));
   }
   return buffer.toString();
 }
コード例 #3
0
 @Override
 public boolean visit(VariableDeclarationFragment node) {
   boolean result = super.visit(node);
   if (isFirstSelectedNode(node)) {
     if (node.getParent() instanceof FieldDeclaration) {
       invalidSelection(
           RefactoringCoreMessages
               .ExtractMethodAnalyzer_cannot_extract_variable_declaration_fragment_from_field,
           JavaStatusContext.create(fCUnit, node));
     } else {
       invalidSelection(
           RefactoringCoreMessages
               .ExtractMethodAnalyzer_cannot_extract_variable_declaration_fragment,
           JavaStatusContext.create(fCUnit, node));
     }
     return false;
   }
   return result;
 }
コード例 #4
0
  private SingleVariableDeclaration createParameterDeclaration(
      String parameterName,
      VariableDeclarationFragment fragement,
      Expression arrayAccess,
      ForStatement statement,
      ImportRewrite importRewrite,
      ASTRewrite rewrite,
      TextEditGroup group,
      LinkedProposalPositionGroup pg,
      boolean makeFinal) {
    CompilationUnit compilationUnit = (CompilationUnit) arrayAccess.getRoot();
    AST ast = compilationUnit.getAST();

    SingleVariableDeclaration result = ast.newSingleVariableDeclaration();

    SimpleName name = ast.newSimpleName(parameterName);
    pg.addPosition(rewrite.track(name), true);
    result.setName(name);

    ITypeBinding arrayTypeBinding = arrayAccess.resolveTypeBinding();
    Type type =
        importType(arrayTypeBinding.getElementType(), statement, importRewrite, compilationUnit);
    if (arrayTypeBinding.getDimensions() != 1) {
      type = ast.newArrayType(type, arrayTypeBinding.getDimensions() - 1);
    }
    result.setType(type);

    if (fragement != null) {
      VariableDeclarationStatement declaration =
          (VariableDeclarationStatement) fragement.getParent();
      ModifierRewrite.create(rewrite, result).copyAllModifiers(declaration, group);
    }
    if (makeFinal
        && (fragement == null
            || ASTNodes.findModifierNode(Modifier.FINAL, ASTNodes.getModifiers(fragement))
                == null)) {
      ModifierRewrite.create(rewrite, result).setModifiers(Modifier.FINAL, 0, group);
    }

    return result;
  }
コード例 #5
0
    private boolean fieldCanBeFinal(
        VariableDeclarationFragment fragment, IVariableBinding binding) {
      if (Modifier.isStatic(((FieldDeclaration) fragment.getParent()).getModifiers())) return false;

      if (!fWrittenVariables.containsKey(binding)) {
        // variable is not written
        if (fragment.getInitializer() == null) { // variable is not initialized
          return false;
        } else {
          return true;
        }
      }

      if (fragment.getInitializer() != null) // variable is initialized and written
      return false;

      ITypeBinding declaringClass = binding.getDeclaringClass();
      if (declaringClass == null) return false;

      ArrayList writes = (ArrayList) fWrittenVariables.get(binding);
      if (!isWrittenInTypeConstructors(writes, declaringClass)) return false;

      HashSet writingConstructorBindings = new HashSet();
      ArrayList writingConstructors = new ArrayList();
      for (int i = 0; i < writes.size(); i++) {
        SimpleName name = (SimpleName) writes.get(i);
        MethodDeclaration constructor = getWritingConstructor(name);
        if (writingConstructors.contains(
            constructor)) // variable is written twice or more in constructor
        return false;

        if (canReturn(constructor)) return false;

        writingConstructors.add(constructor);
        IMethodBinding constructorBinding = constructor.resolveBinding();
        if (constructorBinding == null) return false;

        writingConstructorBindings.add(constructorBinding);
      }

      for (int i = 0; i < writingConstructors.size(); i++) {
        MethodDeclaration constructor = (MethodDeclaration) writingConstructors.get(i);
        if (callsWritingConstructor(
            constructor,
            writingConstructorBindings)) // writing constructor calls other writing constructor
        return false;
      }

      MethodDeclaration constructor = (MethodDeclaration) writingConstructors.get(0);
      TypeDeclaration typeDecl =
          (TypeDeclaration) ASTNodes.getParent(constructor, TypeDeclaration.class);
      if (typeDecl == null) return false;

      MethodDeclaration[] methods = typeDecl.getMethods();
      for (int i = 0; i < methods.length; i++) {
        if (methods[i].isConstructor()) {
          IMethodBinding methodBinding = methods[i].resolveBinding();
          if (methodBinding == null) return false;

          if (!writingConstructorBindings.contains(methodBinding)) {
            if (!callsWritingConstructor(
                methods[i],
                writingConstructorBindings)) // non writing constructor does not call a writing
              // constructor
              return false;
          }
        }
      }

      return true;
    }
コード例 #6
0
 public boolean visit(VariableDeclarationFragment node) {
   String name = getFieldName(node);
   ASTNode parent = node.getParent();
   push(JavaNode.FIELD, name, parent.getStartPosition(), parent.getLength());
   return false;
 }