Java Code Examples for org.eclipse.jdt.internal.compiler.lookup.TypeIds#T_JavaLangString

The following examples show how to use org.eclipse.jdt.internal.compiler.lookup.TypeIds#T_JavaLangString . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VariableBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public Object getConstantValue() {
	Constant c = this.binding.constant();
	if (c == null || c == Constant.NotAConstant) return null;
	switch (c.typeID()) {
		case TypeIds.T_boolean:
			return Boolean.valueOf(c.booleanValue());
		case TypeIds.T_byte:
			return new Byte(c.byteValue());
		case TypeIds.T_char:
			return new Character(c.charValue());
		case TypeIds.T_double:
			return new Double(c.doubleValue());
		case TypeIds.T_float:
			return new Float(c.floatValue());
		case TypeIds.T_int:
			return new Integer(c.intValue());
		case TypeIds.T_long:
			return new Long(c.longValue());
		case TypeIds.T_short:
			return new Short(c.shortValue());
		case TypeIds.T_JavaLangString:
			return c.stringValue();
	}
	return null;
}
 
Example 2
Source File: DefaultBindingResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
Object resolveConstantExpressionValue(Expression expression) {
	org.eclipse.jdt.internal.compiler.ast.ASTNode node = (org.eclipse.jdt.internal.compiler.ast.ASTNode) this.newAstToOldAst.get(expression);
	if (node instanceof org.eclipse.jdt.internal.compiler.ast.Expression) {
		org.eclipse.jdt.internal.compiler.ast.Expression compilerExpression = (org.eclipse.jdt.internal.compiler.ast.Expression) node;
		Constant constant = compilerExpression.constant;
		if (constant != null && constant != Constant.NotAConstant) {
			switch (constant.typeID()) {
				case TypeIds.T_int : return new Integer(constant.intValue());
				case TypeIds.T_byte : return new Byte(constant.byteValue());
				case TypeIds.T_short : return new Short(constant.shortValue());
				case TypeIds.T_char : return new Character(constant.charValue());
				case TypeIds.T_float : return new Float(constant.floatValue());
				case TypeIds.T_double : return new Double(constant.doubleValue());
				case TypeIds.T_boolean : return constant.booleanValue() ? Boolean.TRUE : Boolean.FALSE;
				case TypeIds.T_long : return new Long(constant.longValue());
				case TypeIds.T_JavaLangString : return constant.stringValue();
			}
			return null;
		}
	}
	return null;
}
 
Example 3
Source File: ClassfileDigester.java    From takari-lifecycle with Eclipse Public License 1.0 5 votes vote down vote up
private void updateConstant(Constant constant) {
  updateInt(constant.typeID());
  updateString(constant.getClass().getName());
  switch (constant.typeID()) {
    case TypeIds.T_int:
      updateInt(constant.intValue());
      break;
    case TypeIds.T_byte:
      updateByte(constant.byteValue());
      break;
    case TypeIds.T_short:
      updateShort(constant.shortValue());
      break;
    case TypeIds.T_char:
      updateChar(constant.charValue());
      break;
    case TypeIds.T_long:
      updateLong(constant.longValue());
      break;
    case TypeIds.T_float:
      updateFloat(constant.floatValue());
      break;
    case TypeIds.T_double:
      updateDouble(constant.doubleValue());
      break;
    case TypeIds.T_boolean:
      updateBoolean(constant.booleanValue());
      break;
    case TypeIds.T_JavaLangString:
      updateString(constant.stringValue());
      break;
    default:
      throw new IllegalArgumentException("Unexpected constant typeID=" + constant.typeID());
  }
}
 
Example 4
Source File: AnnotationValueImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings("unchecked") // Need to cast Object _value to a List<AnnotationValue>
@Override
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
	switch (_kind) {
	case TypeIds.T_boolean:
		return v.visitBoolean((Boolean)_value, p);
	case TypeIds.T_byte:
		return v.visitByte((Byte)_value, p);
	case TypeIds.T_char:
		return v.visitChar((Character)_value, p);
	case TypeIds.T_double:
		return v.visitDouble((Double)_value, p);
	case TypeIds.T_float:
		return v.visitFloat((Float)_value, p);
	case TypeIds.T_int:
		return v.visitInt((Integer)_value, p);
	case TypeIds.T_JavaLangString:
		return v.visitString((String)_value, p);
	case TypeIds.T_long:
		return v.visitLong((Long)_value, p);
	case TypeIds.T_short:
		return v.visitShort((Short)_value, p);
	case T_EnumConstant:
		return v.visitEnumConstant((VariableElement)_value, p);
	case T_ClassObject:
		return v.visitType((TypeMirror)_value, p);
	case T_AnnotationMirror:
		return v.visitAnnotation((AnnotationMirror)_value, p);
	case T_ArrayType:
		return v.visitArray((List<AnnotationValue>)_value, p);
	default:
		return null;
	}
}
 
Example 5
Source File: VariableElementImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Object getConstantValue() {
	VariableBinding variableBinding = (VariableBinding) _binding;
	Constant constant = variableBinding.constant();
	if (constant == null || constant == Constant.NotAConstant) return null;
	TypeBinding type = variableBinding.type;
	switch (type.id) {
		case TypeIds.T_boolean:
			return constant.booleanValue();
		case TypeIds.T_byte:
			return constant.byteValue();
		case TypeIds.T_char:
			return constant.charValue();
		case TypeIds.T_double:
			return constant.doubleValue();
		case TypeIds.T_float:
			return constant.floatValue();
		case TypeIds.T_int:
			return constant.intValue();
		case TypeIds.T_JavaLangString:
			return constant.stringValue();
		case TypeIds.T_long:
			return constant.longValue();
		case TypeIds.T_short:
			return constant.shortValue();
	}
	return null;
}
 
Example 6
Source File: Member.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Converts a field constant from the compiler's representation
 * to the Java Model constant representation (Number or String).
 */
protected static Object convertConstant(Constant constant) {
	if (constant == null)
		return null;
	if (constant == Constant.NotAConstant) {
		return null;
	}
	switch (constant.typeID()) {
		case TypeIds.T_boolean :
			return constant.booleanValue() ? Boolean.TRUE : Boolean.FALSE;
		case TypeIds.T_byte :
			return new Byte(constant.byteValue());
		case TypeIds.T_char :
			return new Character(constant.charValue());
		case TypeIds.T_double :
			return new Double(constant.doubleValue());
		case TypeIds.T_float :
			return new Float(constant.floatValue());
		case TypeIds.T_int :
			return new Integer(constant.intValue());
		case TypeIds.T_long :
			return new Long(constant.longValue());
		case TypeIds.T_short :
			return new Short(constant.shortValue());
		case TypeIds.T_JavaLangString :
			return constant.stringValue();
		default :
			return null;
	}
}
 
Example 7
Source File: Util.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static Object getAnnotationMemberValue(MemberValuePair memberValuePair, Constant constant) {
	if (constant == null) {
		memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
		return null;
	}
	switch (constant.typeID()) {
		case TypeIds.T_int :
			memberValuePair.valueKind = IMemberValuePair.K_INT;
			return new Integer(constant.intValue());
		case TypeIds.T_byte :
			memberValuePair.valueKind = IMemberValuePair.K_BYTE;
			return new Byte(constant.byteValue());
		case TypeIds.T_short :
			memberValuePair.valueKind = IMemberValuePair.K_SHORT;
			return new Short(constant.shortValue());
		case TypeIds.T_char :
			memberValuePair.valueKind = IMemberValuePair.K_CHAR;
			return new Character(constant.charValue());
		case TypeIds.T_float :
			memberValuePair.valueKind = IMemberValuePair.K_FLOAT;
			return new Float(constant.floatValue());
		case TypeIds.T_double :
			memberValuePair.valueKind = IMemberValuePair.K_DOUBLE;
			return new Double(constant.doubleValue());
		case TypeIds.T_boolean :
			memberValuePair.valueKind = IMemberValuePair.K_BOOLEAN;
			return Boolean.valueOf(constant.booleanValue());
		case TypeIds.T_long :
			memberValuePair.valueKind = IMemberValuePair.K_LONG;
			return new Long(constant.longValue());
		case TypeIds.T_JavaLangString :
			memberValuePair.valueKind = IMemberValuePair.K_STRING;
			return constant.stringValue();
		default:
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
			return null;
	}
}
 
Example 8
Source File: Constant.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns true if both constants have the same type and the same actual value
 * @param otherConstant
 */
public boolean hasSameValue(Constant otherConstant) {
	if (this == otherConstant)
		return true;
	int typeID;
	if ((typeID = typeID()) != otherConstant.typeID())
		return false;
	switch (typeID) {
		case TypeIds.T_boolean:
			return booleanValue() == otherConstant.booleanValue();
		case TypeIds.T_byte:
			return byteValue() == otherConstant.byteValue();
		case TypeIds.T_char:
			return charValue() == otherConstant.charValue();
		case TypeIds.T_double:
			return doubleValue() == otherConstant.doubleValue();
		case TypeIds.T_float:
			return floatValue() == otherConstant.floatValue();
		case TypeIds.T_int:
			return intValue() == otherConstant.intValue();
		case TypeIds.T_short:
			return shortValue() == otherConstant.shortValue();
		case TypeIds.T_long:
			return longValue() == otherConstant.longValue();
		case TypeIds.T_JavaLangString:
			String value = stringValue();
			return value == null
				? otherConstant.stringValue() == null
				: value.equals(otherConstant.stringValue());
	}
	return false;
}
 
Example 9
Source File: FieldInfo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return a wrapper that contains the constant of the field.
 * @return java.lang.Object
 */
public Object getWrappedConstantValue() {

	if (this.wrappedConstantValue == null) {
		if (hasConstant()) {
			Constant fieldConstant = getConstant();
			switch (fieldConstant.typeID()) {
				case TypeIds.T_int :
					this.wrappedConstantValue = new Integer(fieldConstant.intValue());
					break;
				case TypeIds.T_byte :
					this.wrappedConstantValue = new Byte(fieldConstant.byteValue());
					break;
				case TypeIds.T_short :
					this.wrappedConstantValue = new Short(fieldConstant.shortValue());
					break;
				case TypeIds.T_char :
					this.wrappedConstantValue = new Character(fieldConstant.charValue());
					break;
				case TypeIds.T_float :
					this.wrappedConstantValue = new Float(fieldConstant.floatValue());
					break;
				case TypeIds.T_double :
					this.wrappedConstantValue = new Double(fieldConstant.doubleValue());
					break;
				case TypeIds.T_boolean :
					this.wrappedConstantValue = Util.toBoolean(fieldConstant.booleanValue());
					break;
				case TypeIds.T_long :
					this.wrappedConstantValue = new Long(fieldConstant.longValue());
					break;
				case TypeIds.T_JavaLangString :
					this.wrappedConstantValue = fieldConstant.stringValue();
			}
		}
	}
	return this.wrappedConstantValue;
}
 
Example 10
Source File: Eclipse.java    From EasyMPermission with MIT License 5 votes vote down vote up
/**
 * Returns the actual value of the given Literal or Literal-like node.
 */
public static Object calculateValue(Expression e) {
	if (e instanceof Literal) {
		((Literal)e).computeConstant();
		switch (e.constant.typeID()) {
		case TypeIds.T_int: return e.constant.intValue();
		case TypeIds.T_byte: return e.constant.byteValue();
		case TypeIds.T_short: return e.constant.shortValue();
		case TypeIds.T_char: return e.constant.charValue();
		case TypeIds.T_float: return e.constant.floatValue();
		case TypeIds.T_double: return e.constant.doubleValue();
		case TypeIds.T_boolean: return e.constant.booleanValue();
		case TypeIds.T_long: return e.constant.longValue();
		case TypeIds.T_JavaLangString: return e.constant.stringValue();
		default: return null;
		}
	} else if (e instanceof ClassLiteralAccess) {
		return Eclipse.toQualifiedName(((ClassLiteralAccess)e).type.getTypeName());
	} else if (e instanceof SingleNameReference) {
		return new String(((SingleNameReference)e).token);
	} else if (e instanceof QualifiedNameReference) {
		String qName = Eclipse.toQualifiedName(((QualifiedNameReference)e).tokens);
		int idx = qName.lastIndexOf('.');
		return idx == -1 ? qName : qName.substring(idx+1);
	}
	
	return null;
}
 
Example 11
Source File: QualifiedNameReference.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void checkInternalNPE(BlockScope scope, FlowContext flowContext, FlowInfo flowInfo, boolean checkString) {
	if ((this.bits & ASTNode.RestrictiveFlagMASK) == Binding.LOCAL) {
		LocalVariableBinding local = (LocalVariableBinding) this.binding;
		if (local != null &&
			(local.type.tagBits & TagBits.IsBaseType) == 0 &&
			(checkString || local.type.id != TypeIds.T_JavaLangString)) {
			if ((this.bits & ASTNode.IsNonNull) == 0) {
				flowContext.recordUsingNullReference(scope, local, this,
					FlowContext.MAY_NULL, flowInfo);
			}
			flowInfo.markAsComparedEqualToNonNull(local);
			// from thereon it is set
			flowContext.markFinallyNullStatus(local, FlowInfo.NON_NULL);
		}
	}
	if (this.otherBindings != null) {
		if ((this.bits & ASTNode.RestrictiveFlagMASK) == Binding.FIELD) {
			// is the first field dereferenced annotated Nullable? If so, report immediately
			checkNullableFieldDereference(scope, (FieldBinding) this.binding, this.sourcePositions[this.indexOfFirstFieldBinding-1]);
		}
		// look for annotated fields, they do not depend on flow context -> check immediately:
		int length = this.otherBindings.length - 1; // don't check the last binding
		for (int i = 0; i < length; i++) {
			checkNullableFieldDereference(scope, this.otherBindings[i], this.sourcePositions[this.indexOfFirstFieldBinding+i]);
		}
	}
}
 
Example 12
Source File: Expression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void generateOptimizedStringConcatenation(BlockScope blockScope, CodeStream codeStream, int typeID) {
	if (typeID == TypeIds.T_JavaLangString && this.constant != Constant.NotAConstant && this.constant.stringValue().length() == 0) {
		return; // optimize str + ""
	}
	generateCode(blockScope, codeStream, true);
	codeStream.invokeStringConcatenationAppendForType(typeID);
}
 
Example 13
Source File: CombinedBinaryExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext,
		FlowInfo flowInfo) {
	// keep implementation in sync with BinaryExpression#analyseCode
	if (this.referencesTable == null) {
		return super.analyseCode(currentScope, flowContext, flowInfo);
	}
	try {
		BinaryExpression cursor;
		if ((cursor = this.referencesTable[0]).resolvedType.id !=
				TypeIds.T_JavaLangString) {
			cursor.left.checkNPE(currentScope, flowContext, flowInfo);
		}
		flowInfo = cursor.left.analyseCode(currentScope, flowContext, flowInfo).
			unconditionalInits();
		for (int i = 0, end = this.arity; i < end; i ++) {
			if ((cursor = this.referencesTable[i]).resolvedType.id !=
					TypeIds.T_JavaLangString) {
				cursor.right.checkNPE(currentScope, flowContext, flowInfo);
			}
			flowInfo = cursor.right.
				analyseCode(currentScope, flowContext, flowInfo).
					unconditionalInits();
		}
		if (this.resolvedType.id != TypeIds.T_JavaLangString) {
			this.right.checkNPE(currentScope, flowContext, flowInfo);
		}
		return this.right.analyseCode(currentScope, flowContext, flowInfo).
			unconditionalInits();
	} finally {
		// account for exception possibly thrown by arithmetics
		flowContext.recordAbruptExit();
	}
}
 
Example 14
Source File: CastExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Check binary operator casted arguments
 */
public static void checkNeedForArgumentCasts(BlockScope scope, int operator, int operatorSignature, Expression left, int leftTypeId, boolean leftIsCast, Expression right, int rightTypeId, boolean rightIsCast) {
	if (scope.compilerOptions().getSeverity(CompilerOptions.UnnecessaryTypeCheck) == ProblemSeverities.Ignore) return;

	// check need for left operand cast
	int alternateLeftTypeId = leftTypeId;
	if (leftIsCast) {
		if ((left.bits & ASTNode.UnnecessaryCast) == 0 && left.resolvedType.isBaseType()) {
			// narrowing conversion on base type may change value, thus necessary
			leftIsCast = false;
		} else  {
			TypeBinding alternateLeftType = ((CastExpression)left).expression.resolvedType;
			if (alternateLeftType == null) return; // cannot do better
			if ((alternateLeftTypeId = alternateLeftType.id) == leftTypeId || scope.environment().computeBoxingType(alternateLeftType).id == leftTypeId) { // obvious identity cast
				scope.problemReporter().unnecessaryCast((CastExpression)left);
				leftIsCast = false;
			} else if (alternateLeftTypeId == TypeIds.T_null) {
				alternateLeftTypeId = leftTypeId;  // tolerate null argument cast
				leftIsCast = false;
			}
		}
	}
	// check need for right operand cast
	int alternateRightTypeId = rightTypeId;
	if (rightIsCast) {
		if ((right.bits & ASTNode.UnnecessaryCast) == 0 && right.resolvedType.isBaseType()) {
			// narrowing conversion on base type may change value, thus necessary
			rightIsCast = false;
		} else {
			TypeBinding alternateRightType = ((CastExpression)right).expression.resolvedType;
			if (alternateRightType == null) return; // cannot do better
			if ((alternateRightTypeId = alternateRightType.id) == rightTypeId || scope.environment().computeBoxingType(alternateRightType).id == rightTypeId) { // obvious identity cast
				scope.problemReporter().unnecessaryCast((CastExpression)right);
				rightIsCast = false;
			} else if (alternateRightTypeId == TypeIds.T_null) {
				alternateRightTypeId = rightTypeId;  // tolerate null argument cast
				rightIsCast = false;
			}
		}
	}
	if (leftIsCast || rightIsCast) {
		if (alternateLeftTypeId > 15 || alternateRightTypeId > 15) { // must convert String + Object || Object + String
			if (alternateLeftTypeId == TypeIds.T_JavaLangString) {
				alternateRightTypeId = TypeIds.T_JavaLangObject;
			} else if (alternateRightTypeId == TypeIds.T_JavaLangString) {
				alternateLeftTypeId = TypeIds.T_JavaLangObject;
			} else {
				return; // invalid operator
			}
		}
		int alternateOperatorSignature = OperatorExpression.OperatorSignatures[operator][(alternateLeftTypeId << 4) + alternateRightTypeId];
		// (cast)  left   Op (cast)  right --> result
		//  1111   0000       1111   0000     1111
		//  <<16   <<12       <<8    <<4       <<0
		final int CompareMASK = (0xF<<16) + (0xF<<8) + 0xF; // mask hiding compile-time types
		if ((operatorSignature & CompareMASK) == (alternateOperatorSignature & CompareMASK)) { // same promotions and result
			if (leftIsCast) scope.problemReporter().unnecessaryCast((CastExpression)left);
			if (rightIsCast) scope.problemReporter().unnecessaryCast((CastExpression)right);
		}
	}
}
 
Example 15
Source File: CombinedBinaryExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void generateOptimizedStringConcatenationCreation(BlockScope blockScope,
		CodeStream codeStream, int typeID) {
	// keep implementation in sync with BinaryExpression
	// #generateOptimizedStringConcatenationCreation
	if (this.referencesTable == null) {
		super.generateOptimizedStringConcatenationCreation(blockScope,
			codeStream,	typeID);
	} else {
		if ((((this.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) ==
				OperatorIds.PLUS) &&
					((this.bits & ASTNode.ReturnTypeIDMASK) ==
						TypeIds.T_JavaLangString) &&
					this.constant == Constant.NotAConstant) {
			int pc = codeStream.position;
			BinaryExpression cursor = this.referencesTable[this.arity - 1];
				// silence warnings
			int restart = 0;
			for (restart = this.arity - 1; restart >= 0; restart--) {
				if (((((cursor = this.referencesTable[restart]).bits &
						ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) ==
							OperatorIds.PLUS) &&
						((cursor.bits & ASTNode.ReturnTypeIDMASK) ==
							TypeIds.T_JavaLangString)) {
					if (cursor.constant != Constant.NotAConstant) {
						codeStream.newStringContatenation(); // new: java.lang.StringBuffer
						codeStream.dup();
						codeStream.ldc(cursor.constant.stringValue());
						codeStream.invokeStringConcatenationStringConstructor();
						// invokespecial: java.lang.StringBuffer.<init>(Ljava.lang.String;)V
						break;
					}
				} else {
					cursor.generateOptimizedStringConcatenationCreation(blockScope,
						codeStream, cursor.implicitConversion &
							TypeIds.COMPILE_TYPE_MASK);
					break;
				}
			}
			restart++;
			if (restart == 0) { // reached the leftmost expression
				cursor.left.generateOptimizedStringConcatenationCreation(
					blockScope,
					codeStream,
					cursor.left.implicitConversion & TypeIds.COMPILE_TYPE_MASK);
			}
			int pcAux;
			for (int i = restart; i < this.arity; i++) {
				codeStream.recordPositionsFrom(pc,
					(cursor = this.referencesTable[i]).left.sourceStart);
				pcAux = codeStream.position;
				cursor.right.generateOptimizedStringConcatenation(blockScope,
					codeStream,	cursor.right.implicitConversion &
						TypeIds.COMPILE_TYPE_MASK);
				codeStream.recordPositionsFrom(pcAux, cursor.right.sourceStart);
			}
			codeStream.recordPositionsFrom(pc, this.left.sourceStart);
			pc = codeStream.position;
			this.right.generateOptimizedStringConcatenation(
				blockScope,
				codeStream,
				this.right.implicitConversion & TypeIds.COMPILE_TYPE_MASK);
			codeStream.recordPositionsFrom(pc, this.right.sourceStart);
		} else {
			super.generateOptimizedStringConcatenationCreation(blockScope,
				codeStream, typeID);
		}
	}
}
 
Example 16
Source File: CombinedBinaryExpression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void generateOptimizedStringConcatenation(BlockScope blockScope,
		CodeStream codeStream, int typeID) {
	// keep implementation in sync with BinaryExpression and Expression
	// #generateOptimizedStringConcatenation
	if (this.referencesTable == null) {
		super.generateOptimizedStringConcatenation(blockScope, codeStream,
			typeID);
	} else {
		if ((((this.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) ==
				OperatorIds.PLUS)
			&& ((this.bits & ASTNode.ReturnTypeIDMASK) == TypeIds.T_JavaLangString)) {
			if (this.constant != Constant.NotAConstant) {
				codeStream.generateConstant(this.constant, this.implicitConversion);
				codeStream.invokeStringConcatenationAppendForType(
						this.implicitConversion & TypeIds.COMPILE_TYPE_MASK);
			} else {
				BinaryExpression cursor = this.referencesTable[0];

				int restart = 0;
	//			int cursorTypeID;
				int pc = codeStream.position;
				for (restart = this.arity - 1; restart >= 0; restart--) {
					if ((cursor = this.referencesTable[restart]).constant !=
							Constant.NotAConstant) {
						codeStream.generateConstant(cursor.constant,
							cursor.implicitConversion);
						codeStream.invokeStringConcatenationAppendForType(
							cursor.implicitConversion & TypeIds.COMPILE_TYPE_MASK);
						break;
					}
					// never happens for now - may reconsider if we decide to
					// cover more than string concatenation
	//				if (!((((cursor = this.referencesTable[restart]).bits &
	//						ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) ==
	//							OperatorIds.PLUS) &
	//						((cursorTypeID = cursor.bits & ASTNode.ReturnTypeIDMASK) ==
	//							TypeIds.T_JavaLangString)) {
	//					if (cursorTypeID == T_JavaLangString &&
	//							cursor.constant != Constant.NotAConstant &&
	//							cursor.constant.stringValue().length() == 0) {
	//						break; // optimize str + ""
	//					}
	//					cursor.generateCode(blockScope, codeStream, true);
	//					codeStream.invokeStringConcatenationAppendForType(
	//							cursorTypeID);
	//					break;
	//				}
				}
				restart++;
				if (restart == 0) { // reached the leftmost expression
					cursor.left.generateOptimizedStringConcatenation(
						blockScope,
						codeStream,
						cursor.left.implicitConversion & TypeIds.COMPILE_TYPE_MASK);
				}
				int pcAux;
				for (int i = restart; i < this.arity; i++) {
					codeStream.recordPositionsFrom(pc,
						(cursor = this.referencesTable[i]).left.sourceStart);
					pcAux = codeStream.position;
					cursor.right.generateOptimizedStringConcatenation(blockScope,
						codeStream,	cursor.right.implicitConversion &
							TypeIds.COMPILE_TYPE_MASK);
					codeStream.recordPositionsFrom(pcAux, cursor.right.sourceStart);
				}
				codeStream.recordPositionsFrom(pc, this.left.sourceStart);
				pc = codeStream.position;
				this.right.generateOptimizedStringConcatenation(
					blockScope,
					codeStream,
					this.right.implicitConversion & TypeIds.COMPILE_TYPE_MASK);
				codeStream.recordPositionsFrom(pc, this.right.sourceStart);
			}
		} else {
			super.generateOptimizedStringConcatenation(blockScope, codeStream,
				typeID);
		}
	}
}
 
Example 17
Source File: Expression.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Base types need that the widening is explicitly done by the compiler using some bytecode like i2f.
 * Also check unsafe type operations.
 */
public void computeConversion(Scope scope, TypeBinding runtimeType, TypeBinding compileTimeType) {
	if (runtimeType == null || compileTimeType == null)
		return;
	if (this.implicitConversion != 0) return; // already set independently

	// it is possible for a Byte to be unboxed to a byte & then converted to an int
	// but it is not possible for a byte to become Byte & then assigned to an Integer,
	// or to become an int before boxed into an Integer
	if (runtimeType != TypeBinding.NULL && runtimeType.isBaseType()) {
		if (!compileTimeType.isBaseType()) {
			TypeBinding unboxedType = scope.environment().computeBoxingType(compileTimeType);
			this.implicitConversion = TypeIds.UNBOXING;
			scope.problemReporter().autoboxing(this, compileTimeType, runtimeType);
			compileTimeType = unboxedType;
		}
	} else if (compileTimeType != TypeBinding.NULL && compileTimeType.isBaseType()) {
		TypeBinding boxedType = scope.environment().computeBoxingType(runtimeType);
		if (TypeBinding.equalsEquals(boxedType, runtimeType)) // Object o = 12;
			boxedType = compileTimeType;
		this.implicitConversion = TypeIds.BOXING | (boxedType.id << 4) + compileTimeType.id;
		scope.problemReporter().autoboxing(this, compileTimeType, scope.environment().computeBoxingType(boxedType));
		return;
	} else if (this.constant != Constant.NotAConstant && this.constant.typeID() != TypeIds.T_JavaLangString) {
		this.implicitConversion = TypeIds.BOXING;
		return;
	}
	int compileTimeTypeID, runtimeTypeID;
	if ((compileTimeTypeID = compileTimeType.id) >= TypeIds.T_LastWellKnownTypeId) { // e.g. ? extends String  ==> String (103227); >= TypeIds.T_LastWellKnownTypeId implies TypeIds.NoId
		compileTimeTypeID = compileTimeType.erasure().id == TypeIds.T_JavaLangString ? TypeIds.T_JavaLangString : TypeIds.T_JavaLangObject;
	} else if (runtimeType.isPrimitiveType() && compileTimeType instanceof ReferenceBinding && !compileTimeType.isBoxedPrimitiveType()) {
		compileTimeTypeID = TypeIds.T_JavaLangObject; // treatment is the same as for jlO.
	}

	switch (runtimeTypeID = runtimeType.id) {
		case T_byte :
		case T_short :
		case T_char :
			if (compileTimeTypeID == TypeIds.T_JavaLangObject) {
				this.implicitConversion |= (runtimeTypeID << 4) + compileTimeTypeID;
			} else {
				this.implicitConversion |= (TypeIds.T_int << 4) + compileTimeTypeID;
			}
			break;
		case T_JavaLangString :
		case T_float :
		case T_boolean :
		case T_double :
		case T_int : //implicitConversion may result in i2i which will result in NO code gen
		case T_long :
			this.implicitConversion |= (runtimeTypeID << 4) + compileTimeTypeID;
			break;
		default : // regular object ref
//				if (compileTimeType.isRawType() && runtimeTimeType.isBoundParameterizedType()) {
//				    scope.problemReporter().unsafeRawExpression(this, compileTimeType, runtimeTimeType);
//				}
	}
}
 
Example 18
Source File: SingleNameReference.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public void generateAssignment(BlockScope currentScope, CodeStream codeStream, Assignment assignment, boolean valueRequired) {
	// optimizing assignment like: i = i + 1 or i = 1 + i
	if (assignment.expression.isCompactableOperation()) {
		BinaryExpression operation = (BinaryExpression) assignment.expression;
		int operator = (operation.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT;
		SingleNameReference variableReference;
		if ((operation.left instanceof SingleNameReference) && ((variableReference = (SingleNameReference) operation.left).binding == this.binding)) {
			// i = i + value, then use the variable on the right hand side, since it has the correct implicit conversion
			variableReference.generateCompoundAssignment(currentScope, codeStream, this.syntheticAccessors == null ? null : this.syntheticAccessors[SingleNameReference.WRITE], operation.right, operator, operation.implicitConversion, valueRequired);
			if (valueRequired) {
				codeStream.generateImplicitConversion(assignment.implicitConversion);
			}
			return;
		}
		if ((operation.right instanceof SingleNameReference)
				&& ((operator == OperatorIds.PLUS) || (operator == OperatorIds.MULTIPLY)) // only commutative operations
				&& ((variableReference = (SingleNameReference) operation.right).binding == this.binding)
				&& (operation.left.constant != Constant.NotAConstant) // exclude non constant expressions, since could have side-effect
				&& (((operation.left.implicitConversion & TypeIds.IMPLICIT_CONVERSION_MASK) >> 4) != TypeIds.T_JavaLangString) // exclude string concatenation which would occur backwards
				&& (((operation.right.implicitConversion & TypeIds.IMPLICIT_CONVERSION_MASK) >> 4) != TypeIds.T_JavaLangString)) { // exclude string concatenation which would occur backwards
			// i = value + i, then use the variable on the right hand side, since it has the correct implicit conversion
			variableReference.generateCompoundAssignment(currentScope, codeStream, this.syntheticAccessors == null ? null : this.syntheticAccessors[SingleNameReference.WRITE], operation.left, operator, operation.implicitConversion, valueRequired);
			if (valueRequired) {
				codeStream.generateImplicitConversion(assignment.implicitConversion);
			}
			return;
		}
	}
	switch (this.bits & ASTNode.RestrictiveFlagMASK) {
		case Binding.FIELD : // assigning to a field
			int pc = codeStream.position;
			FieldBinding codegenBinding = ((FieldBinding) this.binding).original();
			if (!codegenBinding.isStatic()) { // need a receiver?
				if ((this.bits & ASTNode.DepthMASK) != 0) {
					ReferenceBinding targetType = currentScope.enclosingSourceType().enclosingTypeAt((this.bits & ASTNode.DepthMASK) >> ASTNode.DepthSHIFT);
					Object[] emulationPath = currentScope.getEmulationPath(targetType, true /*only exact match*/, false/*consider enclosing arg*/);
					codeStream.generateOuterAccess(emulationPath, this, targetType, currentScope);
				} else {
					generateReceiver(codeStream);
				}
			}
			codeStream.recordPositionsFrom(pc, this.sourceStart);
			assignment.expression.generateCode(currentScope, codeStream, true);
			fieldStore(currentScope, codeStream, codegenBinding, this.syntheticAccessors == null ? null : this.syntheticAccessors[SingleNameReference.WRITE], this.actualReceiverType, true /*implicit this*/, valueRequired);
			if (valueRequired) {
				codeStream.generateImplicitConversion(assignment.implicitConversion);
			}
			// no need for generic cast as value got dupped
			return;
		case Binding.LOCAL : // assigning to a local variable
			LocalVariableBinding localBinding = (LocalVariableBinding) this.binding;
			if (localBinding.resolvedPosition != -1) {
				assignment.expression.generateCode(currentScope, codeStream, true);
			} else {
				if (assignment.expression.constant != Constant.NotAConstant) {
					// assigning an unused local to a constant value = no actual assignment is necessary
					if (valueRequired) {
						codeStream.generateConstant(assignment.expression.constant, assignment.implicitConversion);
					}
				} else {
					assignment.expression.generateCode(currentScope, codeStream, true);
					/* Even though the value may not be required, we force it to be produced, and discard it later
					on if it was actually not necessary, so as to provide the same behavior as JDK1.2beta3.	*/
					if (valueRequired) {
						codeStream.generateImplicitConversion(assignment.implicitConversion); // implicit conversion
					} else {
						switch(localBinding.type.id) {
							case TypeIds.T_long :
							case TypeIds.T_double :
								codeStream.pop2();
								break;
							default :
								codeStream.pop();
								break;
						}						
					}
				}
				return;
			}
			// 26903, need extra cast to store null in array local var
			if (localBinding.type.isArrayType()
				&& ((assignment.expression instanceof CastExpression)	// arrayLoc = (type[])null
						&& (((CastExpression)assignment.expression).innermostCastedExpression().resolvedType == TypeBinding.NULL))){
				codeStream.checkcast(localBinding.type);
			}

			// normal local assignment (since cannot store in outer local which are final locations)
			codeStream.store(localBinding, valueRequired);
			if ((this.bits & ASTNode.FirstAssignmentToLocal) != 0) { // for local variable debug attributes
				localBinding.recordInitializationStartPC(codeStream.position);
			}
			// implicit conversion
			if (valueRequired) {
				codeStream.generateImplicitConversion(assignment.implicitConversion);
			}
	}
}
 
Example 19
Source File: ClassFileReader.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean hasStructuralFieldChanges(FieldInfo currentFieldInfo, FieldInfo otherFieldInfo) {
	// generic signature
	if (!CharOperation.equals(currentFieldInfo.getGenericSignature(), otherFieldInfo.getGenericSignature()))
		return true;
	if (currentFieldInfo.getModifiers() != otherFieldInfo.getModifiers())
		return true;
	if ((currentFieldInfo.getTagBits() & TagBits.AnnotationDeprecated) != (otherFieldInfo.getTagBits() & TagBits.AnnotationDeprecated))
		return true;
	if (hasStructuralAnnotationChanges(currentFieldInfo.getAnnotations(), otherFieldInfo.getAnnotations()))
		return true;
	if (!CharOperation.equals(currentFieldInfo.getName(), otherFieldInfo.getName()))
		return true;
	if (!CharOperation.equals(currentFieldInfo.getTypeName(), otherFieldInfo.getTypeName()))
		return true;
	if (currentFieldInfo.hasConstant() != otherFieldInfo.hasConstant())
		return true;
	if (currentFieldInfo.hasConstant()) {
		Constant currentConstant = currentFieldInfo.getConstant();
		Constant otherConstant = otherFieldInfo.getConstant();
		if (currentConstant.typeID() != otherConstant.typeID())
			return true;
		if (!currentConstant.getClass().equals(otherConstant.getClass()))
			return true;
		switch (currentConstant.typeID()) {
			case TypeIds.T_int :
				return currentConstant.intValue() != otherConstant.intValue();
			case TypeIds.T_byte :
				return currentConstant.byteValue() != otherConstant.byteValue();
			case TypeIds.T_short :
				return currentConstant.shortValue() != otherConstant.shortValue();
			case TypeIds.T_char :
				return currentConstant.charValue() != otherConstant.charValue();
			case TypeIds.T_long :
				return currentConstant.longValue() != otherConstant.longValue();
			case TypeIds.T_float :
				return currentConstant.floatValue() != otherConstant.floatValue();
			case TypeIds.T_double :
				return currentConstant.doubleValue() != otherConstant.doubleValue();
			case TypeIds.T_boolean :
				return currentConstant.booleanValue() != otherConstant.booleanValue();
			case TypeIds.T_JavaLangString :
				return !currentConstant.stringValue().equals(otherConstant.stringValue());
		}
	}
	return false;
}
 
Example 20
Source File: AnnotationValueImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Convert the JDT representation of a single constant into its javax.lang.model
 * representation.  For instance, convert a StringConstant into a String, or
 * a FieldBinding into a VariableElement.  This does not handle the case where
 * value is an Object[].
 * @param value the JDT object
 * @param type the return type of the annotation member.  If null or not a
 * BaseTypeBinding, this is ignored and the value is inspected to determine type.
 * @param kind an int array whose first element will be set to the type of the
 * converted object, represented with T_* values from TypeIds or from this class.
 * @return
 */
private Object convertToMirrorType(Object value, TypeBinding type, int kind[]) {
	if (type == null) {
		kind[0] = TypeIds.T_JavaLangString;
		return "<error>"; //$NON-NLS-1$
	} else if (type instanceof BaseTypeBinding || type.id == TypeIds.T_JavaLangString) {
		if (value == null) {
			if (type instanceof BaseTypeBinding
					|| type.id == TypeIds.T_JavaLangString) {
				// return a string with error in it to reflect a value that could not be resolved
				kind[0] = TypeIds.T_JavaLangString;
				return "<error>"; //$NON-NLS-1$
			} else if (type.isAnnotationType()) {
				kind[0] = T_AnnotationMirror;
				return _env.getFactory().newAnnotationMirror(null);
			}
		} else if (value instanceof Constant) {
			if (type instanceof BaseTypeBinding) {
				kind[0] = ((BaseTypeBinding)type).id;
			}
			else if (type.id == TypeIds.T_JavaLangString) {
				kind[0] = ((Constant)value).typeID();
			} else {
				// error case
				kind[0] = TypeIds.T_JavaLangString;
				return "<error>"; //$NON-NLS-1$
			}
			switch (kind[0]) {
			case T_boolean:
				return ((Constant)value).booleanValue();
			case T_byte:
				return ((Constant)value).byteValue();
			case T_char:
				return ((Constant)value).charValue();
			case T_double:
				return ((Constant)value).doubleValue();
			case T_float:
				return ((Constant)value).floatValue();
			case T_int:
				try {
					if (value instanceof LongConstant
							|| value instanceof DoubleConstant
							|| value instanceof FloatConstant) {
						// error case
						kind[0] = TypeIds.T_JavaLangString;
						return "<error>"; //$NON-NLS-1$
					}
					return ((Constant)value).intValue();
				} catch (ShouldNotImplement e) {
					kind[0] = TypeIds.T_JavaLangString;
					return "<error>"; //$NON-NLS-1$
				}
			case T_JavaLangString:
				return ((Constant)value).stringValue();
			case T_long:
				return ((Constant)value).longValue();
			case T_short:
				return ((Constant)value).shortValue();
			}
		}
	} else if (type.isEnum()) {
		if (value instanceof FieldBinding) {
			kind[0] = T_EnumConstant;
			return (VariableElement) _env.getFactory().newElement((FieldBinding) value);
		} else {
			kind[0] = TypeIds.T_JavaLangString;
			return "<error>"; //$NON-NLS-1$
		}
	} else if (type.isAnnotationType()) {
		if (value instanceof AnnotationBinding) {
			kind[0] = T_AnnotationMirror;
			return _env.getFactory().newAnnotationMirror((AnnotationBinding) value);
		}
	} else if (value instanceof TypeBinding) {
		kind[0] = T_ClassObject;
		return _env.getFactory().newTypeMirror((TypeBinding) value);
	}
	// error case
	kind[0] = TypeIds.T_JavaLangString;
	return "<error>"; //$NON-NLS-1$
}