/** * Fix for {@link IProblem#NullableFieldReference} * @param context context * @param problem problem to be fixed * @param proposals accumulator for computed proposals */ public static void addExtractCheckedLocalProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { CompilationUnit compilationUnit = context.getASTRoot(); ICompilationUnit cu= (ICompilationUnit) compilationUnit.getJavaElement(); ASTNode selectedNode= problem.getCoveringNode(compilationUnit); SimpleName name= findProblemFieldName(selectedNode, problem.getProblemId()); if (name == null) return; ASTNode method= ASTNodes.getParent(selectedNode, MethodDeclaration.class); if (method == null) method= ASTNodes.getParent(selectedNode, Initializer.class); if (method == null) return; proposals.add(new ExtractToNullCheckedLocalProposal(cu, compilationUnit, name, method)); } private static SimpleName findProblemFieldName(ASTNode selectedNode, int problemID) {
TextEditGroup group= new TextEditGroup(FixMessages.ExtractToNullCheckedLocalProposal_extractCheckedLocal_editName); LinkedProposalPositionGroup localNameGroup= new LinkedProposalPositionGroup(LOCAL_NAME_POSITION_GROUP); getLinkedProposalModel().addPositionGroup(localNameGroup); VariableDeclarationStatement localDecl= ast.newVariableDeclarationStatement(localFrag); localDecl.setType(newType(toReplace.resolveTypeBinding(), ast, imports)); localDecl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD)); String localName= proposeLocalName(this.fieldReference, this.compilationUnit, getCompilationUnit().getJavaProject()); localFrag.setName(ast.newSimpleName(localName)); String elseStatement= "// TODO "+FixMessages.ExtractToNullCheckedLocalProposal_todoHandleNullDescription; //$NON-NLS-1$ if (origStmt instanceof ReturnStatement) { Type returnType= newType(((ReturnStatement)origStmt).getExpression().resolveTypeBinding(), ast, imports); ReturnStatement returnStatement= ast.newReturnStatement(); returnStatement.setExpression(ASTNodeFactory.newDefaultExpression(ast, returnType, 0)); elseStatement+= '\n' + ASTNodes.asFormattedString(returnStatement, 0, String.valueOf('\n'), FormatterProfileManager.getProjectSettings(getCompilationUnit().getJavaProject())); addLinkedPosition(rewrite.track(localFrag.getName()), true/*first*/, LOCAL_NAME_POSITION_GROUP); addLinkedPosition(rewrite.track(nullCheck.getLeftOperand()), false, LOCAL_NAME_POSITION_GROUP); addLinkedPosition(rewrite.track(dereferencedName), false, LOCAL_NAME_POSITION_GROUP);
ParameterizedType parameterizedType= ast.newParameterizedType(elementType); for (ITypeBinding typeArgument : typeArguments) parameterizedType.typeArguments().add(newType(typeArgument, ast, imports)); elementType = parameterizedType;
TextEditGroup group= new TextEditGroup(FixMessages.ExtractToNullCheckedLocalProposal_extractCheckedLocal_editName); LinkedProposalPositionGroup localNameGroup= new LinkedProposalPositionGroup(LOCAL_NAME_POSITION_GROUP); getLinkedProposalModel().addPositionGroup(localNameGroup); VariableDeclarationStatement localDecl= ast.newVariableDeclarationStatement(localFrag); localDecl.setType(newType(toReplace.resolveTypeBinding(), ast, imports)); localDecl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD)); String localName= proposeLocalName(this.fieldReference, this.compilationUnit, getCompilationUnit().getJavaProject()); localFrag.setName(ast.newSimpleName(localName)); String elseStatement= "// TODO "+FixMessages.ExtractToNullCheckedLocalProposal_todoHandleNullDescription; //$NON-NLS-1$ if (origStmt instanceof ReturnStatement) { Type returnType= newType(((ReturnStatement)origStmt).getExpression().resolveTypeBinding(), ast, imports); ReturnStatement returnStatement= ast.newReturnStatement(); returnStatement.setExpression(ASTNodeFactory.newDefaultExpression(ast, returnType, 0)); elseStatement+= '\n'+ASTNodes.asFormattedString(returnStatement, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true)); addLinkedPosition(rewrite.track(localFrag.getName()), true/*first*/, LOCAL_NAME_POSITION_GROUP); addLinkedPosition(rewrite.track(nullCheck.getLeftOperand()), false, LOCAL_NAME_POSITION_GROUP); addLinkedPosition(rewrite.track(dereferencedName), false, LOCAL_NAME_POSITION_GROUP);
ParameterizedType parameterizedType= ast.newParameterizedType(elementType); for (ITypeBinding typeArgument : typeArguments) parameterizedType.typeArguments().add(newType(typeArgument, ast, imports)); elementType = parameterizedType;
/** * Fix for {@link IProblem#NullableFieldReference} * @param context context * @param problem problem to be fixed * @param proposals accumulator for computed proposals */ public static void addExtractCheckedLocalProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) { CompilationUnit compilationUnit = context.getASTRoot(); ICompilationUnit cu= (ICompilationUnit) compilationUnit.getJavaElement(); ASTNode selectedNode= problem.getCoveringNode(compilationUnit); SimpleName name= findProblemFieldName(selectedNode, problem.getProblemId()); if (name == null) return; ASTNode method= ASTNodes.getParent(selectedNode, MethodDeclaration.class); if (method == null) method= ASTNodes.getParent(selectedNode, Initializer.class); if (method == null) return; proposals.add(new ExtractToNullCheckedLocalProposal(cu, compilationUnit, name, method)); } private static SimpleName findProblemFieldName(ASTNode selectedNode, int problemID) {