Java Code Examples for org.eclipse.jdt.core.dom.IVariableBinding#getConstantValue()

The following examples show how to use org.eclipse.jdt.core.dom.IVariableBinding#getConstantValue() . 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: CompilationUnitBuilder.java    From j2cl with Apache License 2.0 6 votes vote down vote up
private List<Field> convert(FieldDeclaration fieldDeclaration) {
  List<Field> fields = new ArrayList<>();
  for (Object object : fieldDeclaration.fragments()) {
    org.eclipse.jdt.core.dom.VariableDeclarationFragment fragment =
        (org.eclipse.jdt.core.dom.VariableDeclarationFragment) object;
    Expression initializer;
    IVariableBinding variableBinding = fragment.resolveBinding();
    if (variableBinding.getConstantValue() == null) {
      initializer = convertOrNull(fragment.getInitializer());
    } else {
      initializer =
          convertConstantToLiteral(
              variableBinding.getConstantValue(),
              JdtUtils.createTypeDescriptor(variableBinding.getType()));
    }
    Field field =
        Field.Builder.from(JdtUtils.createFieldDescriptor(variableBinding))
            .setInitializer(initializer)
            .setSourcePosition(getSourcePosition(fieldDeclaration))
            .setNameSourcePosition(Optional.of(getSourcePosition(fragment.getName())))
            .build();
    fields.add(field);
  }
  return fields;
}
 
Example 2
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmField createField(StringBuilder typeName, IVariableBinding field) {
	JvmField result;
	if (!field.isEnumConstant()) {
		result = TypesFactory.eINSTANCE.createJvmField();
		Object constantValue = field.getConstantValue();
		if (constantValue != null) {
			result.setConstant(true);
			result.setConstantValue(constantValue);
		} else {
			result.setConstant(false);
		}
	} else
		result = TypesFactory.eINSTANCE.createJvmEnumerationLiteral();
	String name = field.getName();
	result.internalSetIdentifier(typeName.append(name).toString());
	result.setSimpleName(name);
	int modifiers = field.getModifiers();
	result.setFinal(Modifier.isFinal(modifiers));
	result.setStatic(Modifier.isStatic(modifiers));
	result.setTransient(Modifier.isTransient(modifiers));
	result.setVolatile(Modifier.isVolatile(modifiers));
	result.setDeprecated(field.isDeprecated());
	setVisibility(result, modifiers);
	result.setType(createTypeReference(field.getType()));
	createAnnotationValues(field, result);
	return result;
}
 
Example 3
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static Object getVariableBindingConstValue(ASTNode node, IField field) {
	if (node != null && node.getNodeType() == ASTNode.SIMPLE_NAME) {
		IBinding binding = ((SimpleName) node).resolveBinding();
		if (binding != null && binding.getKind() == IBinding.VARIABLE) {
			IVariableBinding variableBinding = (IVariableBinding) binding;
			if (field.equals(variableBinding.getJavaElement())) {
				return variableBinding.getConstantValue();
			}
		}
	}
	return null;
}
 
Example 4
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static Object getVariableBindingConstValue(ASTNode node, IField field) {
	if (node != null && node.getNodeType() == ASTNode.SIMPLE_NAME) {
		IBinding binding= ((SimpleName) node).resolveBinding();
		if (binding != null && binding.getKind() == IBinding.VARIABLE) {
			IVariableBinding variableBinding= (IVariableBinding) binding;
			if (field.equals(variableBinding.getJavaElement())) {
				return variableBinding.getConstantValue();
			}
		}
	}
	return null;
}
 
Example 5
Source File: SwitchControlCase.java    From JDeodorant with MIT License 5 votes vote down vote up
private String getConstantValue(Name name)
{
	IBinding nameBinding = name.resolveBinding();
	if (nameBinding.getKind() == IBinding.VARIABLE)
	{
		IVariableBinding nameVariableBinding = (IVariableBinding)nameBinding;
		Object valueObject = nameVariableBinding.getConstantValue();
		if (valueObject instanceof Integer || valueObject instanceof Byte || valueObject instanceof Character || valueObject instanceof String)
		{
			return valueObject.toString();
		}
	}
	return null;
}
 
Example 6
Source File: JdtUtils.java    From j2cl with Apache License 2.0 4 votes vote down vote up
public static FieldDescriptor createFieldDescriptor(IVariableBinding variableBinding) {
  checkArgument(!isArrayLengthBinding(variableBinding));

  boolean isStatic = isStatic(variableBinding);
  Visibility visibility = getVisibility(variableBinding);
  DeclaredTypeDescriptor enclosingTypeDescriptor =
      createDeclaredTypeDescriptor(variableBinding.getDeclaringClass());
  String fieldName = variableBinding.getName();

  TypeDescriptor thisTypeDescriptor =
      createTypeDescriptorWithNullability(
          variableBinding.getType(), variableBinding.getAnnotations());

  if (variableBinding.isEnumConstant()) {
    // Enum fields are always non-nullable.
    thisTypeDescriptor = thisTypeDescriptor.toNonNullable();
  }

  FieldDescriptor declarationFieldDescriptor = null;
  if (variableBinding.getVariableDeclaration() != variableBinding) {
    declarationFieldDescriptor = createFieldDescriptor(variableBinding.getVariableDeclaration());
  }

  JsInfo jsInfo = JsInteropUtils.getJsInfo(variableBinding);
  boolean isCompileTimeConstant = variableBinding.getConstantValue() != null;
  boolean isFinal = JdtUtils.isFinal(variableBinding);
  return FieldDescriptor.newBuilder()
      .setEnclosingTypeDescriptor(enclosingTypeDescriptor)
      .setName(fieldName)
      .setTypeDescriptor(thisTypeDescriptor)
      .setStatic(isStatic)
      .setVisibility(visibility)
      .setJsInfo(jsInfo)
      .setFinal(isFinal)
      .setCompileTimeConstant(isCompileTimeConstant)
      .setDeclarationDescriptor(declarationFieldDescriptor)
      .setEnumConstant(variableBinding.isEnumConstant())
      .setUnusableByJsSuppressed(
          JsInteropAnnotationUtils.isUnusableByJsSuppressed(variableBinding))
      .setDeprecated(isDeprecated(variableBinding))
      .build();
}
 
Example 7
Source File: JavadocContentAccess2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private boolean handleConstantValue(IField field, boolean link) throws JavaModelException {
	String text = null;

	ISourceRange nameRange = field.getNameRange();
	if (SourceRange.isAvailable(nameRange)) {
		CompilationUnit cuNode = CoreASTProvider.getInstance().getAST(field.getTypeRoot(), CoreASTProvider.WAIT_YES, new NullProgressMonitor());
		if (cuNode != null) {
			ASTNode nameNode = NodeFinder.perform(cuNode, nameRange);
			if (nameNode instanceof SimpleName) {
				IBinding binding = ((SimpleName) nameNode).resolveBinding();
				if (binding instanceof IVariableBinding) {
					IVariableBinding variableBinding = (IVariableBinding) binding;
					Object constantValue = variableBinding.getConstantValue();
					if (constantValue != null) {
						if (constantValue instanceof String) {
							text = ASTNodes.getEscapedStringLiteral((String) constantValue);
						} else {
							text = constantValue.toString(); // Javadoc tool is even worse for chars...
						}
					}
				}
			}
		}
	}

	if (text == null) {
		Object constant = field.getConstant();
		if (constant != null) {
			text = constant.toString();
		}
	}

	if (text != null) {
		text = convertToHTMLContentWithWhitespace(text);
		if (link) {
			String uri;
			try {
				uri = JavaElementLinks.createURI("eclipse-javadoc", field);
				fBuf.append(JavaElementLinks.createLink(uri, text));
			} catch (URISyntaxException e) {
				return false;
			}
		} else {
			handleText(text);
		}
		return true;
	}
	return false;
}
 
Example 8
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean handleConstantValue(IField field, boolean link) throws JavaModelException {
	String text= null;
	
	ISourceRange nameRange= field.getNameRange();
	if (SourceRange.isAvailable(nameRange)) {
		CompilationUnit cuNode= SharedASTProvider.getAST(field.getTypeRoot(), SharedASTProvider.WAIT_ACTIVE_ONLY, null);
		if (cuNode != null) {
			ASTNode nameNode= NodeFinder.perform(cuNode, nameRange);
			if (nameNode instanceof SimpleName) {
				IBinding binding= ((SimpleName) nameNode).resolveBinding();
				if (binding instanceof IVariableBinding) {
					IVariableBinding variableBinding= (IVariableBinding) binding;
					Object constantValue= variableBinding.getConstantValue();
					if (constantValue != null) {
						if (constantValue instanceof String) {
							text= ASTNodes.getEscapedStringLiteral((String) constantValue);
						} else {
							text= constantValue.toString(); // Javadoc tool is even worse for chars...
						}
					}
				}
			}
		}
	}
	
	if (text == null) {
		Object constant= field.getConstant();
		if (constant != null) {
			text= constant.toString();
		}
	}
	
	if (text != null) {
		text= HTMLPrinter.convertToHTMLContentWithWhitespace(text);
		if (link) {
			String uri;
			try {
				uri= JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, field);
				fBuf.append(JavaElementLinks.createLink(uri, text));
			} catch (URISyntaxException e) {
				JavaPlugin.log(e);
				return false;
			}
		} else {
			handleText(text);
		}
		return true;
	}
	return false;
}