Tabnine Logo
ExtractToNullCheckedLocalProposal
Code IndexAdd Tabnine to your IDE (free)

How to use
ExtractToNullCheckedLocalProposal
in
org.eclipse.jdt.internal.ui.text.correction.proposals

Best Java code snippets using org.eclipse.jdt.internal.ui.text.correction.proposals.ExtractToNullCheckedLocalProposal (Showing top 6 results out of 315)

origin: org.eclipse.scout.sdk.deps/org.eclipse.jdt.ui

/**
 * 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) {
origin: org.eclipse.jdt/org.eclipse.jdt.ui

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);
origin: org.eclipse.scout.sdk.deps/org.eclipse.jdt.ui

ParameterizedType parameterizedType= ast.newParameterizedType(elementType);
for (ITypeBinding typeArgument : typeArguments)
  parameterizedType.typeArguments().add(newType(typeArgument, ast, imports));
elementType = parameterizedType;
origin: org.eclipse.scout.sdk.deps/org.eclipse.jdt.ui

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);
origin: org.eclipse.jdt/org.eclipse.jdt.ui

ParameterizedType parameterizedType= ast.newParameterizedType(elementType);
for (ITypeBinding typeArgument : typeArguments)
  parameterizedType.typeArguments().add(newType(typeArgument, ast, imports));
elementType = parameterizedType;
origin: org.eclipse.jdt/org.eclipse.jdt.ui

/**
 * 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) {
org.eclipse.jdt.internal.ui.text.correction.proposalsExtractToNullCheckedLocalProposal

Javadoc

Fix for field related null-issues:
  1. IProblem#NullableFieldReference
  2. IProblem#RequiredNonNullButProvidedSpecdNullable if relating to a field
  3. IProblem#RequiredNonNullButProvidedUnknown if relating to a field
Extract the field reference to a fresh local variable. Add a null check for that local variable and move the dereference into the then-block of this null-check:
 
 @Nullable Exception e;void test() { 
e.printStackTrace(); 
}
will be converted to:
 
 @Nullable Exception e;void test() { 
final Exception e2 = e; 
if (e2 != null) { 
e2.printStackTrace(); 
} else { 
// TODO handle null value 
} 
}

The final keyword is added to remind the user that writing to the local variable has no effect on the original field.

Rrespects scoping if the problem occurs inside the initialization of a local variable (by moving statements into the new then block).

Most used methods

  • <init>
  • addLinkedPosition
  • getCompilationUnit
  • getLinkedProposalModel
  • newType
    Create a fresh type reference
  • proposeLocalName

Popular in Java

  • Making http requests using okhttp
  • putExtra (Intent)
  • requestLocationUpdates (LocationManager)
  • getSystemService (Context)
  • File (java.io)
    An "abstract" representation of a file system entity identified by a pathname. The pathname may be a
  • NoSuchElementException (java.util)
    Thrown when trying to retrieve an element past the end of an Enumeration or Iterator.
  • Properties (java.util)
    A Properties object is a Hashtable where the keys and values must be Strings. Each property can have
  • Semaphore (java.util.concurrent)
    A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each #acquire blocks if
  • BoxLayout (javax.swing)
  • SAXParseException (org.xml.sax)
    Encapsulate an XML parse error or warning.> This module, both source code and documentation, is in t
  • 21 Best IntelliJ Plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now