Example #1
0
  @SuppressWarnings("rawtypes")
  private void getASTConstructor(
      final AbstractTypeDeclaration typeDecl, final List<ConstructorDeclaration> results) {

    final List bodyDecls = typeDecl.bodyDeclarations();
    IFile file = null;
    for (int i = 0, len = bodyDecls.size(); i < len; i++) {
      final BodyDeclaration bodyDecl = (BodyDeclaration) bodyDecls.get(i);
      if (bodyDecl.getNodeType() == ASTNode.METHOD_DECLARATION) {
        final org.eclipse.jdt.core.dom.MethodDeclaration methodDecl =
            (org.eclipse.jdt.core.dom.MethodDeclaration) bodyDecl;

        if (methodDecl.isConstructor()) {
          final IMethodBinding methodBinding = methodDecl.resolveBinding();
          // built an ast based representation.
          if (methodBinding == null) {
            if (file == null) file = getResource();
            ConstructorDeclaration mirrorDecl =
                (ConstructorDeclaration) Factory.createDeclaration(methodDecl, file, _env);
            if (mirrorDecl != null) results.add(mirrorDecl);
          }
        }
      }
    }
  }
  @SuppressWarnings("unchecked")
  public void removeUnallowedAssignments() {
    CompilationUnit cu = catroidSource.getSourceAst();
    final List<AbstractTypeDeclaration> types = cu.types();
    assert types.size() > 0;

    for (AbstractTypeDeclaration abstractTypeDecl : types) {
      for (BodyDeclaration bodyDecl :
          new ArrayList<BodyDeclaration>(abstractTypeDecl.bodyDeclarations())) {
        if (bodyDecl.getNodeType() != ASTNode.METHOD_DECLARATION) {
          continue;
        }

        MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDecl;
        Block body = methodDeclaration.getBody();
        if ((body == null) || (body.statements().size() == 0)) {
          continue;
        }
        methodDeclaration.accept(this);
      }
    }
  }
 @Override
 public void endVisit(CompilationUnit node) {
   RefactoringStatus status = getStatus();
   superCall:
   {
     if (status.hasFatalError()) break superCall;
     if (!hasSelectedNodes()) {
       ASTNode coveringNode = getLastCoveringNode();
       if (coveringNode instanceof Block
           && coveringNode.getParent() instanceof MethodDeclaration) {
         MethodDeclaration methodDecl = (MethodDeclaration) coveringNode.getParent();
         Message[] messages = ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
         if (messages.length > 0) {
           status.addFatalError(
               Messages.format(
                   RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors,
                   BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())),
               JavaStatusContext.create(fCUnit, methodDecl));
           break superCall;
         }
       }
       status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
       break superCall;
     }
     fEnclosingBodyDeclaration =
         (BodyDeclaration) ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
     if (fEnclosingBodyDeclaration == null
         || (fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION
             && fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION
             && fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
       status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
       break superCall;
     } else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
       status.addFatalError(
           RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
       break superCall;
     } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
       fEnclosingMethodBinding = ((MethodDeclaration) fEnclosingBodyDeclaration).resolveBinding();
     }
     if (!isSingleExpressionOrStatementSet()) {
       status.addFatalError(
           RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
       break superCall;
     }
     if (isExpressionSelected()) {
       ASTNode expression = getFirstSelectedNode();
       if (expression instanceof Name) {
         Name name = (Name) expression;
         if (name.resolveBinding() instanceof ITypeBinding) {
           status.addFatalError(
               RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
           break superCall;
         }
         if (name.resolveBinding() instanceof IMethodBinding) {
           status.addFatalError(
               RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
           break superCall;
         }
         if (name.resolveBinding() instanceof IVariableBinding) {
           StructuralPropertyDescriptor locationInParent = name.getLocationInParent();
           if (locationInParent == QualifiedName.NAME_PROPERTY
               || (locationInParent == FieldAccess.NAME_PROPERTY
                   && !(((FieldAccess) name.getParent()).getExpression()
                       instanceof ThisExpression))) {
             status.addFatalError(
                 RefactoringCoreMessages
                     .ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
             break superCall;
           }
         }
         if (name.isSimpleName() && ((SimpleName) name).isDeclaration()) {
           status.addFatalError(
               RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
           break superCall;
         }
       }
       fForceStatic =
           ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null
               || ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
     }
     status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
     computeLastStatementSelected();
   }
   super.endVisit(node);
 }
 private void initReturnType(ImportRewrite rewriter) {
   AST ast = fEnclosingBodyDeclaration.getAST();
   fReturnType = null;
   fReturnTypeBinding = null;
   switch (fReturnKind) {
     case ACCESS_TO_LOCAL:
       VariableDeclaration declaration =
           ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
       fReturnType =
           ASTNodeFactory.newType(
               ast,
               declaration,
               rewriter,
               new ContextSensitiveImportRewriteContext(declaration, rewriter));
       if (declaration.resolveBinding() != null) {
         fReturnTypeBinding = declaration.resolveBinding().getType();
       }
       break;
     case EXPRESSION:
       Expression expression = (Expression) getFirstSelectedNode();
       if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
         fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
       } else {
         fExpressionBinding = expression.resolveTypeBinding();
       }
       if (fExpressionBinding != null) {
         if (fExpressionBinding.isNullType()) {
           getStatus()
               .addFatalError(
                   RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type,
                   JavaStatusContext.create(fCUnit, expression));
         } else {
           ITypeBinding normalizedBinding =
               Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
           if (normalizedBinding != null) {
             ImportRewriteContext context =
                 new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
             fReturnType = rewriter.addImport(normalizedBinding, ast, context);
             fReturnTypeBinding = normalizedBinding;
           }
         }
       } else {
         fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
         fReturnTypeBinding = ast.resolveWellKnownType("void"); // $NON-NLS-1$
         getStatus()
             .addError(
                 RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type,
                 JavaStatusContext.create(fCUnit, expression));
       }
       break;
     case RETURN_STATEMENT_VALUE:
       LambdaExpression enclosingLambdaExpr =
           ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
       if (enclosingLambdaExpr != null) {
         fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
         IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
         fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
       } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
         fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
         fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
       }
       break;
     default:
       fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
       fReturnTypeBinding = ast.resolveWellKnownType("void"); // $NON-NLS-1$
   }
   if (fReturnType == null) {
     fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
     fReturnTypeBinding = ast.resolveWellKnownType("void"); // $NON-NLS-1$
   }
 }