org.eclipse.jdt.internal.compiler.ast.Literal Java Examples

The following examples show how to use org.eclipse.jdt.internal.compiler.ast.Literal. 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: 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 #2
Source File: CompilationUnitStructureRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected Object getMemberValue(org.eclipse.jdt.internal.core.MemberValuePair memberValuePair, Expression expression) {
	if (expression instanceof NullLiteral) {
		return null;
	} else if (expression instanceof Literal) {
		((Literal) expression).computeConstant();
		return Util.getAnnotationMemberValue(memberValuePair, expression.constant);
	} else if (expression instanceof org.eclipse.jdt.internal.compiler.ast.Annotation) {
		org.eclipse.jdt.internal.compiler.ast.Annotation annotation = (org.eclipse.jdt.internal.compiler.ast.Annotation) expression;
		Object handle = acceptAnnotation(annotation, null, (JavaElement) this.handleStack.peek());
		memberValuePair.valueKind = IMemberValuePair.K_ANNOTATION;
		return handle;
	} else if (expression instanceof ClassLiteralAccess) {
		ClassLiteralAccess classLiteral = (ClassLiteralAccess) expression;
		char[] name = CharOperation.concatWith(classLiteral.type.getTypeName(), '.');
		memberValuePair.valueKind = IMemberValuePair.K_CLASS;
		return new String(name);
	} else if (expression instanceof QualifiedNameReference) {
		char[] qualifiedName = CharOperation.concatWith(((QualifiedNameReference) expression).tokens, '.');
		memberValuePair.valueKind = IMemberValuePair.K_QUALIFIED_NAME;
		return new String(qualifiedName);
	} else if (expression instanceof SingleNameReference) {
		char[] simpleName = ((SingleNameReference) expression).token;
		if (simpleName == RecoveryScanner.FAKE_IDENTIFIER) {
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
			return null;
		}
		memberValuePair.valueKind = IMemberValuePair.K_SIMPLE_NAME;
		return new String(simpleName);
	} else if (expression instanceof ArrayInitializer) {
		memberValuePair.valueKind = -1; // modified below by the first call to getMemberValue(...)
		Expression[] expressions = ((ArrayInitializer) expression).expressions;
		int length = expressions == null ? 0 : expressions.length;
		Object[] values = new Object[length];
		for (int i = 0; i < length; i++) {
			int previousValueKind = memberValuePair.valueKind;
			Object value = getMemberValue(memberValuePair, expressions[i]);
			if (previousValueKind != -1 && memberValuePair.valueKind != previousValueKind) {
				// values are heterogeneous, value kind is thus unknown
				memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
			}
			values[i] = value;
		}
		if (memberValuePair.valueKind == -1)
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
		return values;
	} else if (expression instanceof UnaryExpression) {			// to deal with negative numerals (see bug - 248312)
		UnaryExpression unaryExpression = (UnaryExpression) expression;
		if ((unaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.MINUS) {
			if (unaryExpression.expression instanceof Literal) {
				Literal subExpression = (Literal) unaryExpression.expression;
				subExpression.computeConstant();
				return Util.getNegativeAnnotationMemberValue(memberValuePair, subExpression.constant);
			}
		}
		memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
		return null;
	} else {
		memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
		return null;
	}
}
 
Example #3
Source File: LocalVariable.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private Object getAnnotationMemberValue(MemberValuePair memberValuePair, Expression expression, JavaElement parentElement) {
	if (expression instanceof NullLiteral) {
		return null;
	} else if (expression instanceof Literal) {
		((Literal) expression).computeConstant();
		return Util.getAnnotationMemberValue(memberValuePair, expression.constant);
	} else if (expression instanceof org.eclipse.jdt.internal.compiler.ast.Annotation) {
		memberValuePair.valueKind = IMemberValuePair.K_ANNOTATION;
		return getAnnotation((org.eclipse.jdt.internal.compiler.ast.Annotation) expression, parentElement);
	} else if (expression instanceof ClassLiteralAccess) {
		ClassLiteralAccess classLiteral = (ClassLiteralAccess) expression;
		char[] typeName = CharOperation.concatWith(classLiteral.type.getTypeName(), '.');
		memberValuePair.valueKind = IMemberValuePair.K_CLASS;
		return new String(typeName);
	} else if (expression instanceof QualifiedNameReference) {
		char[] qualifiedName = CharOperation.concatWith(((QualifiedNameReference) expression).tokens, '.');
		memberValuePair.valueKind = IMemberValuePair.K_QUALIFIED_NAME;
		return new String(qualifiedName);
	} else if (expression instanceof SingleNameReference) {
		char[] simpleName = ((SingleNameReference) expression).token;
		if (simpleName == RecoveryScanner.FAKE_IDENTIFIER) {
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
			return null;
		}
		memberValuePair.valueKind = IMemberValuePair.K_SIMPLE_NAME;
		return new String(simpleName);
	} else if (expression instanceof ArrayInitializer) {
		memberValuePair.valueKind = -1; // modified below by the first call to getMemberValue(...)
		Expression[] expressions = ((ArrayInitializer) expression).expressions;
		int length = expressions == null ? 0 : expressions.length;
		Object[] values = new Object[length];
		for (int i = 0; i < length; i++) {
			int previousValueKind = memberValuePair.valueKind;
			Object value = getAnnotationMemberValue(memberValuePair, expressions[i], parentElement);
			if (previousValueKind != -1 && memberValuePair.valueKind != previousValueKind) {
				// values are heterogeneous, value kind is thus unknown
				memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
			}
			values[i] = value;
		}
		if (memberValuePair.valueKind == -1)
			memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
		return values;
	} else if (expression instanceof UnaryExpression) {			//to deal with negative numerals (see bug - 248312)
		UnaryExpression unaryExpression = (UnaryExpression) expression;
		if ((unaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.MINUS) {
			if (unaryExpression.expression instanceof Literal) {
				Literal subExpression = (Literal) unaryExpression.expression;
				subExpression.computeConstant();
				return Util.getNegativeAnnotationMemberValue(memberValuePair, subExpression.constant);
			}
		}
		memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
		return null;
	} else {
		memberValuePair.valueKind = IMemberValuePair.K_UNKNOWN;
		return null;
	}
}
 
Example #4
Source File: DefaultBindingResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
synchronized ITypeBinding resolveExpressionType(Expression expression) {
	try {
		switch(expression.getNodeType()) {
			case ASTNode.CLASS_INSTANCE_CREATION :
				org.eclipse.jdt.internal.compiler.ast.ASTNode astNode = (org.eclipse.jdt.internal.compiler.ast.ASTNode) this.newAstToOldAst.get(expression);
				if (astNode instanceof org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) {
					// anonymous type case
					org.eclipse.jdt.internal.compiler.ast.TypeDeclaration typeDeclaration = (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) astNode;
					ITypeBinding typeBinding = this.getTypeBinding(typeDeclaration.binding);
					if (typeBinding != null) {
						return typeBinding;
					}
				} else if (astNode instanceof AllocationExpression) {
					// should be an AllocationExpression
					AllocationExpression allocationExpression = (AllocationExpression) astNode;
					return this.getTypeBinding(allocationExpression.resolvedType);
				}
				break;
			case ASTNode.SIMPLE_NAME :
			case ASTNode.QUALIFIED_NAME :
				return resolveTypeBindingForName((Name) expression);
			case ASTNode.ARRAY_INITIALIZER :
			case ASTNode.ARRAY_CREATION :
			case ASTNode.ASSIGNMENT :
			case ASTNode.POSTFIX_EXPRESSION :
			case ASTNode.PREFIX_EXPRESSION :
			case ASTNode.CAST_EXPRESSION :
			case ASTNode.TYPE_LITERAL :
			case ASTNode.INFIX_EXPRESSION :
			case ASTNode.INSTANCEOF_EXPRESSION :
			case ASTNode.LAMBDA_EXPRESSION:
			case ASTNode.CREATION_REFERENCE:
			case ASTNode.EXPRESSION_METHOD_REFERENCE:
			case ASTNode.TYPE_METHOD_REFERENCE:
			case ASTNode.SUPER_METHOD_REFERENCE :
			case ASTNode.FIELD_ACCESS :
			case ASTNode.SUPER_FIELD_ACCESS :
			case ASTNode.ARRAY_ACCESS :
			case ASTNode.METHOD_INVOCATION :
			case ASTNode.SUPER_METHOD_INVOCATION :
			case ASTNode.CONDITIONAL_EXPRESSION :
			case ASTNode.MARKER_ANNOTATION :
			case ASTNode.NORMAL_ANNOTATION :
			case ASTNode.SINGLE_MEMBER_ANNOTATION :
				org.eclipse.jdt.internal.compiler.ast.Expression compilerExpression = (org.eclipse.jdt.internal.compiler.ast.Expression) this.newAstToOldAst.get(expression);
				if (compilerExpression != null) {
					return this.getTypeBinding(compilerExpression.resolvedType);
				}
				break;
			case ASTNode.STRING_LITERAL :
				if (this.scope != null) {
					return this.getTypeBinding(this.scope.getJavaLangString());
				}
				break;
			case ASTNode.BOOLEAN_LITERAL :
			case ASTNode.NULL_LITERAL :
			case ASTNode.CHARACTER_LITERAL :
			case ASTNode.NUMBER_LITERAL :
				Literal literal = (Literal) this.newAstToOldAst.get(expression);
				if (literal != null) {
					return this.getTypeBinding(literal.literalType(null));
				}
				break;
			case ASTNode.THIS_EXPRESSION :
				ThisReference thisReference = (ThisReference) this.newAstToOldAst.get(expression);
				BlockScope blockScope = (BlockScope) this.astNodesToBlockScope.get(expression);
				if (blockScope != null) {
					return this.getTypeBinding(thisReference.resolveType(blockScope));
				}
				break;
			case ASTNode.PARENTHESIZED_EXPRESSION :
				ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) expression;
				return resolveExpressionType(parenthesizedExpression.getExpression());
			case ASTNode.VARIABLE_DECLARATION_EXPRESSION :
				VariableDeclarationExpression variableDeclarationExpression = (VariableDeclarationExpression) expression;
				Type type = variableDeclarationExpression.getType();
				if (type != null) {
					return type.resolveBinding();
				}
				break;
		}
	} catch (AbortCompilation e) {
		// handle missing types
	}
	return null;
}