1 /******************************************************************************* 2 * Copyright (c) 2000, 2013 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 * Stephan Herrmann - Contribution for 11 * bug 331649 - [compiler][null] consider null annotations for fields 12 * Bug 392099 - [1.8][compiler][null] Apply null annotation on types for null analysis 13 *******************************************************************************/ 14 package org.eclipse.jdt.internal.compiler.lookup; 15 16 import org.eclipse.jdt.internal.compiler.ast.ASTNode; 17 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 18 import org.eclipse.jdt.internal.compiler.impl.Constant; 19 20 public abstract class VariableBinding extends Binding { 21 22 public int modifiers; 23 public TypeBinding type; 24 public char[] name; 25 protected Constant constant; 26 public int id; // for flow-analysis (position in flowInfo bit vector) 27 public long tagBits; 28 VariableBinding(char[] name, TypeBinding type, int modifiers, Constant constant)29 public VariableBinding(char[] name, TypeBinding type, int modifiers, Constant constant) { 30 this.name = name; 31 this.type = type; 32 this.modifiers = modifiers; 33 this.constant = constant; 34 if (type != null) { 35 this.tagBits |= (type.tagBits & TagBits.HasMissingType); 36 } 37 } 38 constant()39 public Constant constant() { 40 return this.constant; 41 } 42 getAnnotations()43 public abstract AnnotationBinding[] getAnnotations(); 44 isBlankFinal()45 public final boolean isBlankFinal(){ 46 return (this.modifiers & ExtraCompilerModifiers.AccBlankFinal) != 0; 47 } 48 49 /* Answer true if the receiver is explicitly or implicitly final 50 * and cannot be changed. Resources on try and multi catch variables are 51 * marked as implicitly final. 52 */ isFinal()53 public final boolean isFinal() { 54 return (this.modifiers & ClassFileConstants.AccFinal) != 0; 55 } 56 isEffectivelyFinal()57 public final boolean isEffectivelyFinal() { 58 return (this.tagBits & TagBits.IsEffectivelyFinal) != 0; 59 } 60 61 /** Answer true if null annotations are enabled and this field is specified @NonNull */ isNonNull()62 public boolean isNonNull() { 63 return (this.tagBits & TagBits.AnnotationNonNull) != 0 64 || (this.type != null 65 && (this.type.tagBits & TagBits.AnnotationNonNull) != 0); 66 } 67 68 /** Answer true if null annotations are enabled and this field is specified @Nullable */ isNullable()69 public boolean isNullable() { 70 return (this.tagBits & TagBits.AnnotationNullable) != 0 71 || (this.type != null 72 && (this.type.tagBits & TagBits.AnnotationNullable) != 0); 73 } 74 readableName()75 public char[] readableName() { 76 return this.name; 77 } setConstant(Constant constant)78 public void setConstant(Constant constant) { 79 this.constant = constant; 80 } toString()81 public String toString() { 82 StringBuffer output = new StringBuffer(10); 83 ASTNode.printModifiers(this.modifiers, output); 84 if ((this.modifiers & ExtraCompilerModifiers.AccUnresolved) != 0) { 85 output.append("[unresolved] "); //$NON-NLS-1$ 86 } 87 output.append(this.type != null ? this.type.debugName() : "<no type>"); //$NON-NLS-1$ 88 output.append(" "); //$NON-NLS-1$ 89 output.append((this.name != null) ? new String(this.name) : "<no name>"); //$NON-NLS-1$ 90 return output.toString(); 91 } 92 } 93