Example usage for org.eclipse.jdt.core.dom IVariableBinding getConstantValue

List of usage examples for org.eclipse.jdt.core.dom IVariableBinding getConstantValue

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom IVariableBinding getConstantValue.

Prototype

public Object getConstantValue();

Source Link

Document

Returns this binding's constant value if it has one.

Usage

From source file:astview.Binding.java

License:Open Source License

@Override
public Object[] getChildren() {
    try {// w  ww .  j a va2s .co  m
        if (fBinding != null) {
            fBinding.getKey();
        }
    } catch (RuntimeException e) {
        return new Object[] { new Error(this, "BrokenBinding: " + fBinding, null) };
    }
    if (fBinding != null) {
        ArrayList<ASTAttribute> res = new ArrayList<>();
        res.add(new BindingProperty(this, "NAME", fBinding.getName(), true)); //$NON-NLS-1$
        res.add(new BindingProperty(this, "KEY", fBinding.getKey(), true)); //$NON-NLS-1$
        res.add(new BindingProperty(this, "IS RECOVERED", fBinding.isRecovered(), true)); //$NON-NLS-1$
        switch (fBinding.getKind()) {
        case IBinding.VARIABLE:
            IVariableBinding variableBinding = (IVariableBinding) fBinding;
            res.add(new BindingProperty(this, "IS FIELD", variableBinding.isField(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS ENUM CONSTANT", variableBinding.isEnumConstant(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS PARAMETER", variableBinding.isParameter(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "VARIABLE ID", variableBinding.getVariableId(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "MODIFIERS", getModifiersString(fBinding.getModifiers(), false), //$NON-NLS-1$
                    true));
            res.add(new Binding(this, "TYPE", variableBinding.getType(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "DECLARING CLASS", variableBinding.getDeclaringClass(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "DECLARING METHOD", variableBinding.getDeclaringMethod(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "VARIABLE DECLARATION", variableBinding.getVariableDeclaration(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "CONSTANT VALUE", variableBinding.getConstantValue(), true)); //$NON-NLS-1$ //$NON-NLS-2$
            res.add(new BindingProperty(this, "IS EFFECTIVELY FINAL", variableBinding.isEffectivelyFinal(), //$NON-NLS-1$
                    true));
            break;

        case IBinding.PACKAGE:
            IPackageBinding packageBinding = (IPackageBinding) fBinding;
            res.add(new BindingProperty(this, "IS UNNAMED", packageBinding.isUnnamed(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$
            break;

        case IBinding.TYPE:
            ITypeBinding typeBinding = (ITypeBinding) fBinding;
            res.add(new BindingProperty(this, "QUALIFIED NAME", typeBinding.getQualifiedName(), true)); //$NON-NLS-1$

            int typeKind = getTypeKind(typeBinding);
            boolean isRefType = isType(typeKind, REF_TYPE);
            final boolean isNonPrimitive = !isType(typeKind, PRIMITIVE_TYPE);

            StringBuffer kinds = new StringBuffer("KIND:"); //$NON-NLS-1$
            if (typeBinding.isArray())
                kinds.append(" isArray"); //$NON-NLS-1$
            if (typeBinding.isCapture())
                kinds.append(" isCapture"); //$NON-NLS-1$
            if (typeBinding.isNullType())
                kinds.append(" isNullType"); //$NON-NLS-1$
            if (typeBinding.isPrimitive())
                kinds.append(" isPrimitive"); //$NON-NLS-1$
            if (typeBinding.isTypeVariable())
                kinds.append(" isTypeVariable"); //$NON-NLS-1$
            if (typeBinding.isWildcardType())
                kinds.append(" isWildcardType"); //$NON-NLS-1$
            // ref types
            if (typeBinding.isAnnotation())
                kinds.append(" isAnnotation"); //$NON-NLS-1$
            if (typeBinding.isClass())
                kinds.append(" isClass"); //$NON-NLS-1$
            if (typeBinding.isInterface())
                kinds.append(" isInterface"); //$NON-NLS-1$
            if (typeBinding.isEnum())
                kinds.append(" isEnum"); //$NON-NLS-1$
            res.add(new BindingProperty(this, kinds, true)); //$NON-NLS-1$

            StringBuffer generics = new StringBuffer("GENERICS:"); //$NON-NLS-1$
            if (typeBinding.isRawType())
                generics.append(" isRawType"); //$NON-NLS-1$
            if (typeBinding.isGenericType())
                generics.append(" isGenericType"); //$NON-NLS-1$
            if (typeBinding.isParameterizedType())
                generics.append(" isParameterizedType"); //$NON-NLS-1$
            if (!isType(typeKind, GENERIC | PARAMETRIZED)) {
                generics.append(" (non-generic, non-parameterized)");
            }
            res.add(new BindingProperty(this, generics, isRefType)); //$NON-NLS-1$

            res.add(new Binding(this, "ELEMENT TYPE", typeBinding.getElementType(), //$NON-NLS-1$
                    isType(typeKind, ARRAY_TYPE)));
            res.add(new Binding(this, "COMPONENT TYPE", typeBinding.getComponentType(), //$NON-NLS-1$
                    isType(typeKind, ARRAY_TYPE)));
            res.add(new BindingProperty(this, "DIMENSIONS", typeBinding.getDimensions(), //$NON-NLS-1$
                    isType(typeKind, ARRAY_TYPE)));
            final String createArrayTypeLabel = "CREATE ARRAY TYPE (+1)";
            try {
                ITypeBinding arrayType = typeBinding.createArrayType(1);
                res.add(new Binding(this, createArrayTypeLabel, arrayType, true));
            } catch (RuntimeException e) {
                String msg = e.getClass().getName() + ": " + e.getLocalizedMessage();
                boolean isRelevant = !typeBinding.getName().equals(PrimitiveType.VOID.toString())
                        && !typeBinding.isRecovered();
                if (isRelevant) {
                    res.add(new Error(this, createArrayTypeLabel + ": " + msg, e));
                } else {
                    res.add(new BindingProperty(this, createArrayTypeLabel, msg, false));
                }
            }

            StringBuffer origin = new StringBuffer("ORIGIN:"); //$NON-NLS-1$
            if (typeBinding.isTopLevel())
                origin.append(" isTopLevel"); //$NON-NLS-1$
            if (typeBinding.isNested())
                origin.append(" isNested"); //$NON-NLS-1$
            if (typeBinding.isLocal())
                origin.append(" isLocal"); //$NON-NLS-1$
            if (typeBinding.isMember())
                origin.append(" isMember"); //$NON-NLS-1$
            if (typeBinding.isAnonymous())
                origin.append(" isAnonymous"); //$NON-NLS-1$
            res.add(new BindingProperty(this, origin, isRefType));

            res.add(new BindingProperty(this, "IS FROM SOURCE", typeBinding.isFromSource(), //$NON-NLS-1$
                    isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE)));

            res.add(new Binding(this, "PACKAGE", typeBinding.getPackage(), isRefType)); //$NON-NLS-1$
            res.add(new Binding(this, "DECLARING CLASS", typeBinding.getDeclaringClass(), //$NON-NLS-1$
                    isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE)));
            res.add(new Binding(this, "DECLARING METHOD", typeBinding.getDeclaringMethod(), //$NON-NLS-1$
                    isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE)));
            //               res.add(new Binding(this, "DECLARING MEMBER", typeBinding.getDeclaringMember(), typeBinding.isLocal())); //$NON-NLS-1$
            res.add(new BindingProperty(this, "MODIFIERS", getModifiersString(fBinding.getModifiers(), false), //$NON-NLS-1$
                    isRefType));
            res.add(new BindingProperty(this, "BINARY NAME", typeBinding.getBinaryName(), true)); //$NON-NLS-1$

            String isTypeDeclaration = typeBinding == typeBinding.getTypeDeclaration() ? " ( == this)"
                    : " ( != this)";
            res.add(new Binding(this, "TYPE DECLARATION" + isTypeDeclaration, typeBinding.getTypeDeclaration(), //$NON-NLS-1$
                    true));
            String isErasure = typeBinding == typeBinding.getErasure() ? " ( == this)" : " ( != this)";
            res.add(new Binding(this, "ERASURE" + isErasure, typeBinding.getErasure(), isNonPrimitive)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "TYPE PARAMETERS", typeBinding.getTypeParameters(), //$NON-NLS-1$
                    isType(typeKind, GENERIC)));
            res.add(new BindingProperty(this, "TYPE ARGUMENTS", typeBinding.getTypeArguments(), //$NON-NLS-1$
                    isType(typeKind, PARAMETRIZED)));
            res.add(new BindingProperty(this, "TYPE BOUNDS", typeBinding.getTypeBounds(), //$NON-NLS-1$
                    isType(typeKind, VARIABLE_TYPE | WILDCARD_TYPE | CAPTURE_TYPE)));
            res.add(new Binding(this, "BOUND", typeBinding.getBound(), isType(typeKind, WILDCARD_TYPE))); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS UPPERBOUND", typeBinding.isUpperbound(), //$NON-NLS-1$
                    isType(typeKind, WILDCARD_TYPE)));
            res.add(new Binding(this, "GENERIC TYPE OF WILDCARD TYPE", //$NON-NLS-1$
                    typeBinding.getGenericTypeOfWildcardType(), isType(typeKind, WILDCARD_TYPE)));
            res.add(new BindingProperty(this, "RANK", typeBinding.getRank(), isType(typeKind, WILDCARD_TYPE))); //$NON-NLS-1$
            res.add(new Binding(this, "WILDCARD", typeBinding.getWildcard(), isType(typeKind, CAPTURE_TYPE))); //$NON-NLS-1$

            res.add(new Binding(this, "SUPERCLASS", typeBinding.getSuperclass(), isRefType)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "INTERFACES", typeBinding.getInterfaces(), isRefType)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "DECLARED TYPES", typeBinding.getDeclaredTypes(), isRefType)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "DECLARED FIELDS", typeBinding.getDeclaredFields(), isRefType)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "DECLARED METHODS", typeBinding.getDeclaredMethods(), isRefType)); //$NON-NLS-1$
            res.add(new Binding(this, "FUNCTIONAL INTERFACE METHOD", typeBinding.getFunctionalInterfaceMethod(), //$NON-NLS-1$
                    typeBinding.isInterface()));
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), isNonPrimitive)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), isRefType)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "TYPE ANNOTATIONS", typeBinding.getTypeAnnotations(), true)); //$NON-NLS-1$
            break;

        case IBinding.METHOD:
            IMethodBinding methodBinding = (IMethodBinding) fBinding;
            res.add(new BindingProperty(this, "IS CONSTRUCTOR", methodBinding.isConstructor(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEFAULT CONSTRUCTOR", methodBinding.isDefaultConstructor(), //$NON-NLS-1$
                    true));
            res.add(new Binding(this, "DECLARING CLASS", methodBinding.getDeclaringClass(), true)); //$NON-NLS-1$
            //               res.add(new Binding(this, "DECLARING MEMBER", methodBinding.getDeclaringMember(), methodBinding.getDeclaringMember() != null)); //$NON-NLS-1$
            res.add(new Binding(this, "RETURN TYPE", methodBinding.getReturnType(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "MODIFIERS", getModifiersString(fBinding.getModifiers(), true), //$NON-NLS-1$
                    true));
            res.add(new BindingProperty(this, "PARAMETER TYPES", methodBinding.getParameterTypes(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS VARARGS", methodBinding.isVarargs(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "EXCEPTION TYPES", methodBinding.getExceptionTypes(), true)); //$NON-NLS-1$

            StringBuffer genericsM = new StringBuffer("GENERICS:"); //$NON-NLS-1$
            if (methodBinding.isRawMethod())
                genericsM.append(" isRawMethod"); //$NON-NLS-1$
            if (methodBinding.isGenericMethod())
                genericsM.append(" isGenericMethod"); //$NON-NLS-1$
            if (methodBinding.isParameterizedMethod())
                genericsM.append(" isParameterizedMethod"); //$NON-NLS-1$
            res.add(new BindingProperty(this, genericsM, true));

            String isMethodDeclaration = methodBinding == methodBinding.getMethodDeclaration() ? " ( == this)"
                    : " ( != this)";
            res.add(new Binding(this, "METHOD DECLARATION" + isMethodDeclaration, //$NON-NLS-1$
                    methodBinding.getMethodDeclaration(), true));
            res.add(new BindingProperty(this, "TYPE PARAMETERS", methodBinding.getTypeParameters(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "TYPE ARGUMENTS", methodBinding.getTypeArguments(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$

            res.add(new BindingProperty(this, "IS ANNOTATION MEMBER", methodBinding.isAnnotationMember(), //$NON-NLS-1$
                    true));
            res.add(Binding.createValueAttribute(this, "DEFAULT VALUE", methodBinding.getDefaultValue()));

            int parameterCount = methodBinding.getParameterTypes().length;
            BindingProperty[] parametersAnnotations = new BindingProperty[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                parametersAnnotations[i] = new BindingProperty(this, "Parameter " + String.valueOf(i),
                        methodBinding.getParameterAnnotations(i), true);
            }
            res.add(new BindingProperty(this, "PARAMETER ANNOTATIONS", parametersAnnotations, true));
            break;

        case IBinding.ANNOTATION:
            IAnnotationBinding annotationBinding = (IAnnotationBinding) fBinding;
            res.add(new Binding(this, "ANNOTATION TYPE", annotationBinding.getAnnotationType(), true));
            res.add(new BindingProperty(this, "DECLARED MEMBER VALUE PAIRS",
                    annotationBinding.getDeclaredMemberValuePairs(), true));
            res.add(new BindingProperty(this, "ALL MEMBER VALUE PAIRS",
                    annotationBinding.getAllMemberValuePairs(), true));
            break;

        case IBinding.MEMBER_VALUE_PAIR:
            IMemberValuePairBinding memberValuePairBinding = (IMemberValuePairBinding) fBinding;
            res.add(new Binding(this, "METHOD BINDING", memberValuePairBinding.getMethodBinding(), true));
            res.add(new BindingProperty(this, "IS DEFAULT", memberValuePairBinding.isDefault(), true));
            res.add(Binding.createValueAttribute(this, "VALUE", memberValuePairBinding.getValue()));
            break;
        }
        try {
            IAnnotationBinding[] annotations = fBinding.getAnnotations();
            res.add(new BindingProperty(this, "ANNOTATIONS", annotations, true)); //$NON-NLS-1$
        } catch (RuntimeException e) {
            String label = "Error in IBinding#getAnnotations() for \"" + fBinding.getKey() + "\"";
            res.add(new Error(this, label, e));
        }

        try {
            IJavaElement javaElement = fBinding.getJavaElement();
        } catch (RuntimeException e) {
            String label = ">java element: " + e.getClass().getName() + " for \"" + fBinding.getKey() + "\""; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
            res.add(new Error(this, label, e));
        }

        return res.toArray();
    }
    return EMPTY;
}

From source file:cideplus.ui.astview.Binding.java

License:Open Source License

public Object[] getChildren() {
    if (fBinding != null) {
        ArrayList res = new ArrayList();
        res.add(new BindingProperty(this, "NAME", fBinding.getName(), true)); //$NON-NLS-1$
        res.add(new BindingProperty(this, "KEY", fBinding.getKey(), true)); //$NON-NLS-1$
        res.add(new BindingProperty(this, "IS RECOVERED", fBinding.isRecovered(), true)); //$NON-NLS-1$
        switch (fBinding.getKind()) {
        case IBinding.VARIABLE:
            IVariableBinding variableBinding = (IVariableBinding) fBinding;
            res.add(new BindingProperty(this, "IS FIELD", variableBinding.isField(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS ENUM CONSTANT", variableBinding.isEnumConstant(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS PARAMETER", variableBinding.isParameter(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "VARIABLE ID", variableBinding.getVariableId(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), true)); //$NON-NLS-1$
            res.add(new Binding(this, "TYPE", variableBinding.getType(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "DECLARING CLASS", variableBinding.getDeclaringClass(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "DECLARING METHOD", variableBinding.getDeclaringMethod(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "VARIABLE DECLARATION", variableBinding.getVariableDeclaration(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "CONSTANT VALUE", variableBinding.getConstantValue(), true)); //$NON-NLS-1$ //$NON-NLS-2$
            break;

        case IBinding.PACKAGE:
            IPackageBinding packageBinding = (IPackageBinding) fBinding;
            res.add(new BindingProperty(this, "IS UNNAMED", packageBinding.isUnnamed(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$
            break;

        case IBinding.TYPE:
            if (1 == 1) {
                break;//no iremos mostrar todas essas informaes da classe
            }/* w w w.jav a 2s .  co m*/
            ITypeBinding typeBinding = (ITypeBinding) fBinding;
            res.add(new BindingProperty(this, "QUALIFIED NAME", typeBinding.getQualifiedName(), true)); //$NON-NLS-1$

            int typeKind = getTypeKind(typeBinding);
            boolean isRefType = isType(typeKind, REF_TYPE);
            final boolean isNonPrimitive = !isType(typeKind, PRIMITIVE_TYPE);

            StringBuffer kinds = new StringBuffer("KIND:"); //$NON-NLS-1$
            if (typeBinding.isArray())
                kinds.append(" isArray"); //$NON-NLS-1$
            if (typeBinding.isCapture())
                kinds.append(" isCapture"); //$NON-NLS-1$
            if (typeBinding.isNullType())
                kinds.append(" isNullType"); //$NON-NLS-1$
            if (typeBinding.isPrimitive())
                kinds.append(" isPrimitive"); //$NON-NLS-1$
            if (typeBinding.isTypeVariable())
                kinds.append(" isTypeVariable"); //$NON-NLS-1$
            if (typeBinding.isWildcardType())
                kinds.append(" isWildcardType"); //$NON-NLS-1$
            // ref types
            if (typeBinding.isAnnotation())
                kinds.append(" isAnnotation"); //$NON-NLS-1$
            if (typeBinding.isClass())
                kinds.append(" isClass"); //$NON-NLS-1$
            if (typeBinding.isInterface())
                kinds.append(" isInterface"); //$NON-NLS-1$
            if (typeBinding.isEnum())
                kinds.append(" isEnum"); //$NON-NLS-1$
            res.add(new BindingProperty(this, kinds, true)); //$NON-NLS-1$

            StringBuffer generics = new StringBuffer("GENERICS:"); //$NON-NLS-1$
            if (typeBinding.isRawType())
                generics.append(" isRawType"); //$NON-NLS-1$
            if (typeBinding.isGenericType())
                generics.append(" isGenericType"); //$NON-NLS-1$
            if (typeBinding.isParameterizedType())
                generics.append(" isParameterizedType"); //$NON-NLS-1$
            if (!isType(typeKind, GENERIC | PARAMETRIZED)) {
                generics.append(" (non-generic, non-parameterized)");
            }
            res.add(new BindingProperty(this, generics, isRefType)); //$NON-NLS-1$

            res.add(new Binding(this, "ELEMENT TYPE", typeBinding.getElementType(), //$NON-NLS-1$
                    isType(typeKind, ARRAY_TYPE)));
            res.add(new Binding(this, "COMPONENT TYPE", typeBinding.getComponentType(), //$NON-NLS-1$
                    isType(typeKind, ARRAY_TYPE)));
            res.add(new BindingProperty(this, "DIMENSIONS", typeBinding.getDimensions(), //$NON-NLS-1$
                    isType(typeKind, ARRAY_TYPE)));
            final String createArrayTypeLabel = "CREATE ARRAY TYPE (+1)";
            try {
                ITypeBinding arrayType = typeBinding.createArrayType(1);
                res.add(new Binding(this, createArrayTypeLabel, arrayType, true));
            } catch (RuntimeException e) {
                String msg = e.getClass().getName() + ": " + e.getLocalizedMessage();
                boolean isRelevant = !typeBinding.getName().equals(PrimitiveType.VOID.toString())
                        && !typeBinding.isRecovered();
                if (isRelevant) {
                    res.add(new Error(this, createArrayTypeLabel + ": " + msg, e));
                } else {
                    res.add(new BindingProperty(this, createArrayTypeLabel, msg, false));
                }
            }

            res.add(new BindingProperty(this, "TYPE BOUNDS", typeBinding.getTypeBounds(), //$NON-NLS-1$
                    isType(typeKind, VARIABLE_TYPE | CAPTURE_TYPE)));

            StringBuffer origin = new StringBuffer("ORIGIN:"); //$NON-NLS-1$
            if (typeBinding.isTopLevel())
                origin.append(" isTopLevel"); //$NON-NLS-1$
            if (typeBinding.isNested())
                origin.append(" isNested"); //$NON-NLS-1$
            if (typeBinding.isLocal())
                origin.append(" isLocal"); //$NON-NLS-1$
            if (typeBinding.isMember())
                origin.append(" isMember"); //$NON-NLS-1$
            if (typeBinding.isAnonymous())
                origin.append(" isAnonymous"); //$NON-NLS-1$
            res.add(new BindingProperty(this, origin, isRefType));

            res.add(new BindingProperty(this, "IS FROM SOURCE", typeBinding.isFromSource(), //$NON-NLS-1$
                    isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE)));

            res.add(new Binding(this, "PACKAGE", typeBinding.getPackage(), isRefType)); //$NON-NLS-1$
            res.add(new Binding(this, "DECLARING CLASS", typeBinding.getDeclaringClass(), //$NON-NLS-1$
                    isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE)));
            res.add(new Binding(this, "DECLARING METHOD", typeBinding.getDeclaringMethod(), //$NON-NLS-1$
                    isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE)));
            res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), isRefType)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "BINARY NAME", typeBinding.getBinaryName(), true)); //$NON-NLS-1$

            res.add(new Binding(this, "TYPE DECLARATION", typeBinding.getTypeDeclaration(), isNonPrimitive)); //$NON-NLS-1$
            res.add(new Binding(this, "ERASURE", typeBinding.getErasure(), isNonPrimitive)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "TYPE PARAMETERS", typeBinding.getTypeParameters(), //$NON-NLS-1$
                    isType(typeKind, GENERIC)));
            res.add(new BindingProperty(this, "TYPE ARGUMENTS", typeBinding.getTypeArguments(), //$NON-NLS-1$
                    isType(typeKind, PARAMETRIZED)));
            res.add(new Binding(this, "BOUND", typeBinding.getBound(), isType(typeKind, WILDCARD_TYPE))); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS UPPERBOUND", typeBinding.isUpperbound(), //$NON-NLS-1$
                    isType(typeKind, WILDCARD_TYPE)));
            res.add(new Binding(this, "GENERIC TYPE OF WILDCARD TYPE", //$NON-NLS-1$
                    typeBinding.getGenericTypeOfWildcardType(), isType(typeKind, WILDCARD_TYPE)));
            res.add(new BindingProperty(this, "RANK", typeBinding.getRank(), isType(typeKind, WILDCARD_TYPE))); //$NON-NLS-1$
            res.add(new Binding(this, "WILDCARD", typeBinding.getWildcard(), isType(typeKind, CAPTURE_TYPE))); //$NON-NLS-1$

            res.add(new Binding(this, "SUPERCLASS", typeBinding.getSuperclass(), isRefType)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "INTERFACES", typeBinding.getInterfaces(), isRefType)); //$NON-NLS-1$         
            res.add(new BindingProperty(this, "DECLARED TYPES", typeBinding.getDeclaredTypes(), isRefType)); //$NON-NLS-1$         
            res.add(new BindingProperty(this, "DECLARED FIELDS", typeBinding.getDeclaredFields(), isRefType)); //$NON-NLS-1$         
            res.add(new BindingProperty(this, "DECLARED METHODS", typeBinding.getDeclaredMethods(), isRefType)); //$NON-NLS-1$         
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), isNonPrimitive)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), isRefType)); //$NON-NLS-1$
            break;

        case IBinding.METHOD:
            IMethodBinding methodBinding = (IMethodBinding) fBinding;
            res.add(new BindingProperty(this, "IS CONSTRUCTOR", methodBinding.isConstructor(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEFAULT CONSTRUCTOR", methodBinding.isDefaultConstructor(), //$NON-NLS-1$
                    true));
            res.add(new Binding(this, "DECLARING CLASS", methodBinding.getDeclaringClass(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "RETURN TYPE", methodBinding.getReturnType(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "PARAMETER TYPES", methodBinding.getParameterTypes(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS VARARGS", methodBinding.isVarargs(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "EXCEPTION TYPES", methodBinding.getExceptionTypes(), true)); //$NON-NLS-1$

            StringBuffer genericsM = new StringBuffer("GENERICS:"); //$NON-NLS-1$
            if (methodBinding.isRawMethod())
                genericsM.append(" isRawMethod"); //$NON-NLS-1$
            if (methodBinding.isGenericMethod())
                genericsM.append(" isGenericMethod"); //$NON-NLS-1$
            if (methodBinding.isParameterizedMethod())
                genericsM.append(" isParameterizedMethod"); //$NON-NLS-1$
            res.add(new BindingProperty(this, genericsM, true));

            res.add(new Binding(this, "METHOD DECLARATION", methodBinding.getMethodDeclaration(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "TYPE PARAMETERS", methodBinding.getTypeParameters(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "TYPE ARGUMENTS", methodBinding.getTypeArguments(), true)); //$NON-NLS-1$         
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$

            res.add(new BindingProperty(this, "IS ANNOTATION MEMBER", methodBinding.isAnnotationMember(), //$NON-NLS-1$
                    true));
            res.add(Binding.createValueAttribute(this, "DEFAULT VALUE", methodBinding.getDefaultValue()));

            int parameterCount = methodBinding.getParameterTypes().length;
            GeneralAttribute[] parametersAnnotations = new GeneralAttribute[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                parametersAnnotations[i] = new GeneralAttribute(this, "Parameter " + String.valueOf(i),
                        methodBinding.getParameterAnnotations(i));
            }
            res.add(new GeneralAttribute(this, "PARAMETER ANNOTATIONS", parametersAnnotations));
            break;

        case IBinding.ANNOTATION:
            IAnnotationBinding annotationBinding = (IAnnotationBinding) fBinding;
            res.add(new Binding(this, "ANNOTATION TYPE", annotationBinding.getAnnotationType(), true));
            res.add(new BindingProperty(this, "DECLARED MEMBER VALUE PAIRS",
                    annotationBinding.getDeclaredMemberValuePairs(), true));
            res.add(new BindingProperty(this, "ALL MEMBER VALUE PAIRS",
                    annotationBinding.getAllMemberValuePairs(), true));
            break;

        case IBinding.MEMBER_VALUE_PAIR:
            IMemberValuePairBinding memberValuePairBinding = (IMemberValuePairBinding) fBinding;
            res.add(new Binding(this, "METHOD BINDING", memberValuePairBinding.getMethodBinding(), true));
            res.add(new BindingProperty(this, "IS DEFAULT", memberValuePairBinding.isDefault(), true));
            res.add(Binding.createValueAttribute(this, "VALUE", memberValuePairBinding.getValue()));
            break;
        }
        try {
            IAnnotationBinding[] annotations = fBinding.getAnnotations();
            res.add(new BindingProperty(this, "ANNOTATIONS", annotations, true)); //$NON-NLS-1$
        } catch (RuntimeException e) {
            String label = "Error in IBinding#getAnnotations() for \"" + fBinding.getKey() + "\"";
            res.add(new Error(this, label, e));
            ASTViewPlugin.log("Exception thrown in IBinding#getAnnotations() for \"" + fBinding.getKey() + "\"",
                    e);
        }
        try {
            IJavaElement javaElement = fBinding.getJavaElement();
            res.add(new JavaElement(this, javaElement));
        } catch (RuntimeException e) {
            String label = ">java element: " + e.getClass().getName() + " for \"" + fBinding.getKey() + "\""; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
            res.add(new Error(this, label, e));
            ASTViewPlugin.log("Exception thrown in IBinding#getJavaElement() for \"" + fBinding.getKey() + "\"",
                    e);
        }
        return res.toArray();
    }
    return EMPTY;
}

From source file:coloredide.astview.internal.Binding.java

License:Open Source License

public Object[] getChildren() {

    if (fBinding != null) {
        ArrayList res = new ArrayList();
        res.add(new BindingProperty(this, "NAME", fBinding.getName(), true)); //$NON-NLS-1$
        res.add(new BindingProperty(this, "KEY", fBinding.getKey(), true)); //$NON-NLS-1$
        switch (fBinding.getKind()) {
        case IBinding.VARIABLE:
            IVariableBinding variableBinding = (IVariableBinding) fBinding;
            res.add(new BindingProperty(this, "IS FIELD", variableBinding.isField(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS ENUM CONSTANT", variableBinding.isEnumConstant(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS PARAMETER", variableBinding.isParameter(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "VARIABLE ID", variableBinding.getVariableId(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), true)); //$NON-NLS-1$
            res.add(new Binding(this, "TYPE", variableBinding.getType(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "DECLARING CLASS", variableBinding.getDeclaringClass(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "DECLARING METHOD", variableBinding.getDeclaringMethod(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "VARIABLE DECLARATION", variableBinding.getVariableDeclaration(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$
            Object constVal = variableBinding.getConstantValue();
            res.add(new BindingProperty(this, "CONSTANT VALUE", constVal == null ? "null" : constVal.toString(), //$NON-NLS-1$//$NON-NLS-2$
                    true));/*from  w ww  .j  a v  a2s . c o m*/
            break;

        case IBinding.PACKAGE:
            IPackageBinding packageBinding = (IPackageBinding) fBinding;
            res.add(new BindingProperty(this, "IS UNNAMED", packageBinding.isUnnamed(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$
            break;

        case IBinding.TYPE:
            ITypeBinding typeBinding = (ITypeBinding) fBinding;
            res.add(new BindingProperty(this, "QUALIFIED NAME", typeBinding.getQualifiedName(), true)); //$NON-NLS-1$

            int typeKind = getTypeKind(typeBinding);
            boolean isRefType = isType(typeKind, REF_TYPE);
            final boolean isNonPrimitive = !isType(typeKind, PRIMITIVE_TYPE);

            StringBuffer kinds = new StringBuffer("KIND:"); //$NON-NLS-1$
            if (typeBinding.isArray())
                kinds.append(" isArray"); //$NON-NLS-1$
            if (typeBinding.isCapture())
                kinds.append(" isCapture"); //$NON-NLS-1$
            if (typeBinding.isNullType())
                kinds.append(" isNullType"); //$NON-NLS-1$
            if (typeBinding.isPrimitive())
                kinds.append(" isPrimitive"); //$NON-NLS-1$
            if (typeBinding.isTypeVariable())
                kinds.append(" isTypeVariable"); //$NON-NLS-1$
            if (typeBinding.isWildcardType())
                kinds.append(" isWildcardType"); //$NON-NLS-1$
            // ref types
            if (typeBinding.isAnnotation())
                kinds.append(" isAnnotation"); //$NON-NLS-1$
            if (typeBinding.isClass())
                kinds.append(" isClass"); //$NON-NLS-1$
            if (typeBinding.isInterface())
                kinds.append(" isInterface"); //$NON-NLS-1$
            if (typeBinding.isEnum())
                kinds.append(" isEnum"); //$NON-NLS-1$
            res.add(new BindingProperty(this, kinds, true)); //$NON-NLS-1$

            StringBuffer generics = new StringBuffer("GENERICS:"); //$NON-NLS-1$
            if (typeBinding.isRawType())
                generics.append(" isRawType"); //$NON-NLS-1$
            if (typeBinding.isGenericType())
                generics.append(" isGenericType"); //$NON-NLS-1$
            if (typeBinding.isParameterizedType())
                generics.append(" isParameterizedType"); //$NON-NLS-1$
            if (!isType(typeKind, GENERIC | PARAMETRIZED)) {
                generics.append(" (non-generic, non-parameterized)");
            }
            res.add(new BindingProperty(this, generics, isRefType)); //$NON-NLS-1$

            res.add(new Binding(this, "ELEMENT TYPE", typeBinding.getElementType(), //$NON-NLS-1$
                    isType(typeKind, ARRAY_TYPE)));
            res.add(new Binding(this, "COMPONENT TYPE", typeBinding.getComponentType(), //$NON-NLS-1$
                    isType(typeKind, ARRAY_TYPE)));
            res.add(new BindingProperty(this, "DIMENSIONS", typeBinding.getDimensions(), //$NON-NLS-1$
                    isType(typeKind, ARRAY_TYPE)));

            res.add(new BindingProperty(this, "TYPE BOUNDS", typeBinding.getTypeBounds(), //$NON-NLS-1$
                    isType(typeKind, VARIABLE_TYPE | CAPTURE_TYPE)));

            StringBuffer origin = new StringBuffer("ORIGIN:"); //$NON-NLS-1$
            if (typeBinding.isTopLevel())
                origin.append(" isTopLevel"); //$NON-NLS-1$
            if (typeBinding.isNested())
                origin.append(" isNested"); //$NON-NLS-1$
            if (typeBinding.isLocal())
                origin.append(" isLocal"); //$NON-NLS-1$
            if (typeBinding.isMember())
                origin.append(" isMember"); //$NON-NLS-1$
            if (typeBinding.isAnonymous())
                origin.append(" isAnonymous"); //$NON-NLS-1$
            res.add(new BindingProperty(this, origin, isRefType));

            res.add(new BindingProperty(this, "IS FROM SOURCE", typeBinding.isFromSource(), //$NON-NLS-1$
                    isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE)));

            res.add(new Binding(this, "PACKAGE", typeBinding.getPackage(), isRefType)); //$NON-NLS-1$
            res.add(new Binding(this, "DECLARING CLASS", typeBinding.getDeclaringClass(), //$NON-NLS-1$
                    isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE)));
            res.add(new Binding(this, "DECLARING METHOD", typeBinding.getDeclaringMethod(), //$NON-NLS-1$
                    isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE)));
            res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), isRefType)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "BINARY NAME", typeBinding.getBinaryName(), true)); //$NON-NLS-1$

            res.add(new Binding(this, "TYPE DECLARATION", typeBinding.getTypeDeclaration(), isNonPrimitive)); //$NON-NLS-1$
            res.add(new Binding(this, "ERASURE", typeBinding.getErasure(), isNonPrimitive)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "TYPE PARAMETERS", typeBinding.getTypeParameters(), //$NON-NLS-1$
                    isType(typeKind, GENERIC)));
            res.add(new BindingProperty(this, "TYPE ARGUMENTS", typeBinding.getTypeArguments(), //$NON-NLS-1$
                    isType(typeKind, PARAMETRIZED)));
            res.add(new Binding(this, "BOUND", typeBinding.getBound(), isType(typeKind, WILDCARD_TYPE))); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS UPPERBOUND", typeBinding.isUpperbound(), //$NON-NLS-1$
                    isType(typeKind, WILDCARD_TYPE)));
            res.add(new Binding(this, "WILDCARD", typeBinding.getWildcard(), isType(typeKind, CAPTURE_TYPE))); //$NON-NLS-1$

            res.add(new Binding(this, "SUPERCLASS", typeBinding.getSuperclass(), isRefType)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "INTERFACES", typeBinding.getInterfaces(), isRefType)); //$NON-NLS-1$         
            res.add(new BindingProperty(this, "DECLARED TYPES", typeBinding.getDeclaredTypes(), isRefType)); //$NON-NLS-1$         
            res.add(new BindingProperty(this, "DECLARED FIELDS", typeBinding.getDeclaredFields(), isRefType)); //$NON-NLS-1$         
            res.add(new BindingProperty(this, "DECLARED METHODS", typeBinding.getDeclaredMethods(), isRefType)); //$NON-NLS-1$         
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), isNonPrimitive)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), isRefType)); //$NON-NLS-1$
            break;

        case IBinding.METHOD:
            IMethodBinding methodBinding = (IMethodBinding) fBinding;
            res.add(new BindingProperty(this, "IS CONSTRUCTOR", methodBinding.isConstructor(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEFAULT CONSTRUCTOR", methodBinding.isDefaultConstructor(), //$NON-NLS-1$
                    true));
            res.add(new Binding(this, "DECLARING CLASS", methodBinding.getDeclaringClass(), true)); //$NON-NLS-1$
            res.add(new Binding(this, "RETURN TYPE", methodBinding.getReturnType(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "PARAMETER TYPES", methodBinding.getParameterTypes(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS VARARGS", methodBinding.isVarargs(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "EXCEPTION TYPES", methodBinding.getExceptionTypes(), true)); //$NON-NLS-1$

            StringBuffer genericsM = new StringBuffer("GENERICS:"); //$NON-NLS-1$
            if (methodBinding.isRawMethod())
                genericsM.append(" isRawMethod"); //$NON-NLS-1$
            if (methodBinding.isGenericMethod())
                genericsM.append(" isGenericMethod"); //$NON-NLS-1$
            if (methodBinding.isParameterizedMethod())
                genericsM.append(" isParameterizedMethod"); //$NON-NLS-1$
            res.add(new BindingProperty(this, genericsM, true));

            res.add(new Binding(this, "METHOD DECLARATION", methodBinding.getMethodDeclaration(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "TYPE PARAMETERS", methodBinding.getTypeParameters(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "TYPE ARGUMENTS", methodBinding.getTypeArguments(), true)); //$NON-NLS-1$         
            res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$
            res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$

            res.add(new BindingProperty(this, "IS ANNOTATION MEMBER", methodBinding.isAnnotationMember(), //$NON-NLS-1$
                    true));
            res.add(Binding.createValueAttribute(this, "DEFAULT VALUE", methodBinding.getDefaultValue()));

            int parameterCount = methodBinding.getParameterTypes().length;
            GeneralAttribute[] parametersAnnotations = new GeneralAttribute[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                parametersAnnotations[i] = new GeneralAttribute(this, "Parameter " + String.valueOf(i),
                        methodBinding.getParameterAnnotations(i));
            }
            res.add(new GeneralAttribute(this, "PARAMETER ANNOTATIONS", parametersAnnotations));
            break;

        case IBinding.ANNOTATION:
            IAnnotationBinding annotationBinding = (IAnnotationBinding) fBinding;
            res.add(new Binding(this, "ANNOTATION TYPE", annotationBinding.getAnnotationType(), true));
            res.add(new BindingProperty(this, "DECLARED MEMBER VALUE PAIRS",
                    annotationBinding.getDeclaredMemberValuePairs(), true));
            res.add(new BindingProperty(this, "ALL MEMBER VALUE PAIRS",
                    annotationBinding.getAllMemberValuePairs(), true));
            break;

        case IBinding.MEMBER_VALUE_PAIR:
            IMemberValuePairBinding memberValuePairBinding = (IMemberValuePairBinding) fBinding;
            res.add(new Binding(this, "METHOD BINDING", memberValuePairBinding.getMethodBinding(), true));
            res.add(new BindingProperty(this, "IS DEFAULT", memberValuePairBinding.isDefault(), true));
            res.add(Binding.createValueAttribute(this, "VALUE", memberValuePairBinding.getValue()));
            break;
        }
        try {
            IAnnotationBinding[] annotations = fBinding.getAnnotations();
            res.add(new BindingProperty(this, "ANNOTATIONS", annotations, true)); //$NON-NLS-1$
        } catch (RuntimeException e) {
            String label = "Error in IBinding#getAnnotations() for \"" + fBinding.getKey() + "\"";
            res.add(new Error(this, label, e));
            ColoredIDEPlugin
                    .log("Exception thrown in IBinding#getAnnotations() for \"" + fBinding.getKey() + "\"", e);
        }
        try {
            IJavaElement javaElement = fBinding.getJavaElement();
            res.add(new JavaElement(this, javaElement));
        } catch (RuntimeException e) {
            String label = ">java element: " + e.getClass().getName() + " for \"" + fBinding.getKey() + "\""; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
            res.add(new Error(this, label, e));
            ColoredIDEPlugin
                    .log("Exception thrown in IBinding#getJavaElement() for \"" + fBinding.getKey() + "\"", e);
        }
        return res.toArray();
    }
    return EMPTY;
}

From source file:com.codenvy.ide.ext.java.server.javadoc.JavadocContentAccess2.java

License:Open Source License

private boolean handleConstantValue(IField field, boolean link) throws JavaModelException {
    String text = null;//  w w w .j  a v a  2  s  .  c  o  m

    ISourceRange nameRange = field.getNameRange();
    if (SourceRange.isAvailable(nameRange)) {
        CompilationUnit cuNode = ASTProvider.createAST(field.getTypeRoot(), 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(urlPrefix, field);
                fBuf.append(JavaElementLinks.createLink(uri, text));
            } catch (URISyntaxException e) {
                LOG.error(e.getMessage(), e);
                return false;
            }
        } else {
            handleText(text);
        }
        return true;
    }
    return false;
}

From source file:com.google.devtools.j2cpp.gen.CppStatementGenerator.java

License:Open Source License

/**
 * Prints a multi-dimensional array that is defined using array sizes,
 * rather than an initializer.  For example, "new int[2][3][4]".
 */// w ww  .  j  a v a2s  .  c o m
private void printMultiDimArray(ITypeBinding elementType, List<Expression> dimensions) {
    if (dimensions.size() == 1) {
        printSingleDimArray(elementType, dimensions.get(0), false);
    } else {
        buffer.append("[IOSObjectArray arrayWithObjects:(id[]){ ");
        Expression dimension = dimensions.get(0);
        int dim;
        // An array dimension may either be a number literal, constant, or expression.
        if (dimension instanceof NumberLiteral) {
            dim = Integer.parseInt(dimension.toString());
        } else {
            IVariableBinding var = Types.getVariableBinding(dimension);
            if (var != null) {
                Number constant = (Number) var.getConstantValue();
                dim = constant != null ? constant.intValue() : 1;
            } else {
                dim = 1;
            }
        }
        List<Expression> subDimensions = dimensions.subList(1, dimensions.size());
        for (int i = 0; i < dim; i++) {
            printMultiDimArray(elementType, subDimensions);
            if (i + 1 < dim) {
                buffer.append(',');
            }
            buffer.append(' ');
        }
        buffer.append("} count:");
        dimension.accept(this);
        buffer.append(" type:[IOSClass classWithClass:[");
        buffer.append(
                subDimensions.size() > 1 ? "IOSObjectArray" : Types.resolveArrayType(elementType).toString());
        buffer.append(" class]]]");
    }
}

From source file:com.google.devtools.j2cpp.translate.AnonymousClassConverter.java

License:Open Source License

private boolean isConstant(IVariableBinding var) {
    return var.getConstantValue() != null;
}

From source file:com.google.devtools.j2cpp.translate.AnonymousClassConverter.java

License:Open Source License

/**
 * Convert the anonymous class into an inner class.  Fields are added for
 * final variables that are referenced, and a constructor is added.
 *
 * Note: endVisit is used for a depth-first traversal, to make it easier
 * to scan their containing nodes for references.
 *///from  w  ww. j  a va 2s . com
@Override
@SuppressWarnings("unchecked")
public void endVisit(AnonymousClassDeclaration node) {
    ASTNode parent = node.getParent();
    ClassInstanceCreation newInvocation = null;
    EnumConstantDeclaration enumConstant = null;
    List<Expression> parentArguments;
    String newClassName;
    ITypeBinding innerType;
    boolean isStatic = staticParent(node);
    int modifiers = isStatic ? Modifier.STATIC : 0;
    if (parent instanceof ClassInstanceCreation) {
        newInvocation = (ClassInstanceCreation) parent;
        parentArguments = newInvocation.arguments();
        innerType = Types.getTypeBinding(newInvocation);
        newClassName = innerType.getName();
        innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType,
                modifiers);
    } else if (parent instanceof EnumConstantDeclaration) {
        enumConstant = (EnumConstantDeclaration) parent;
        parentArguments = enumConstant.arguments();
        innerType = Types.getTypeBinding(node);
        newClassName = Types.getTypeBinding(node).getName();
        innerType = RenamedTypeBinding.rename(newClassName, innerType.getDeclaringClass(), innerType,
                modifiers);
    } else {
        throw new AssertionError("unknown anonymous class declaration parent: " + parent.getClass().getName());
    }

    // Create a type declaration for this anonymous class.
    AST ast = node.getAST();
    TypeDeclaration typeDecl = ast.newTypeDeclaration();
    if (isStatic) {
        typeDecl.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
    }
    Types.addBinding(typeDecl, innerType);
    typeDecl.setName(ast.newSimpleName(newClassName));
    Types.addBinding(typeDecl.getName(), innerType);
    typeDecl.setSourceRange(node.getStartPosition(), node.getLength());

    Type superType = Types.makeType(Types.mapType(innerType.getSuperclass()));
    typeDecl.setSuperclassType(superType);
    for (ITypeBinding interfaceType : innerType.getInterfaces()) {
        typeDecl.superInterfaceTypes().add(Types.makeType(Types.mapType(interfaceType)));
    }

    for (Object bodyDecl : node.bodyDeclarations()) {
        BodyDeclaration decl = (BodyDeclaration) bodyDecl;
        typeDecl.bodyDeclarations().add(NodeCopier.copySubtree(ast, decl));
    }
    typeDecl.accept(new InitializationNormalizer());

    // Fix up references to external types, if necessary.
    Set<IVariableBinding> methodVars = getMethodVars(node);
    final List<ReferenceDescription> references = findReferences(node, methodVars);
    final List<Expression> invocationArgs = parentArguments;
    if (!references.isEmpty() || !invocationArgs.isEmpty()) { // is there anything to fix-up?
        List<IVariableBinding> innerVars = getInnerVars(references);
        if (!innerVars.isEmpty() || !invocationArgs.isEmpty()) {
            GeneratedMethodBinding defaultConstructor = addInnerVars(typeDecl, innerVars, references,
                    parentArguments);
            Types.addBinding(parent, defaultConstructor);
            for (IVariableBinding var : innerVars) {
                if (!isConstant(var)) {
                    parentArguments.add(makeFieldRef(var, ast));
                }
            }
            assert defaultConstructor.getParameterTypes().length == parentArguments.size();
            typeDecl.accept(new ASTVisitor() {
                @Override
                public void endVisit(SimpleName node) {
                    IVariableBinding var = Types.getVariableBinding(node);
                    if (var != null) {
                        for (ReferenceDescription ref : references) {
                            if (var.isEqualTo(ref.binding)) {
                                if (ref.innerField != null) {
                                    setProperty(node, makeFieldRef(ref.innerField, node.getAST()));
                                } else {
                                    // In-line constant.
                                    Object o = var.getConstantValue();
                                    assert o != null;
                                    Expression literal = makeLiteral(o, var.getType().getQualifiedName(),
                                            node.getAST());
                                    setProperty(node, literal);
                                }
                                return;
                            }
                        }
                    }
                }
            });
        }
    }

    // If invocation, replace anonymous class invocation with the new constructor.
    if (newInvocation != null) {
        newInvocation.setAnonymousClassDeclaration(null);
        newInvocation.setType(Types.makeType(innerType));
        IMethodBinding oldBinding = Types.getMethodBinding(newInvocation);
        if (oldBinding == null) {
            oldBinding = newInvocation.resolveConstructorBinding();
        }
        if (oldBinding != null) {
            GeneratedMethodBinding invocationBinding = new GeneratedMethodBinding(oldBinding);
            invocationBinding.setDeclaringClass(innerType);
            Types.addBinding(newInvocation, invocationBinding);
        }
    } else {
        enumConstant.setAnonymousClassDeclaration(null);
    }

    // Add type declaration to enclosing type.
    ITypeBinding outerType = innerType.getDeclaringClass();
    if (outerType.isAnonymous()) {
        // Get outerType node.
        ASTNode n = parent.getParent();
        while (!(n instanceof AnonymousClassDeclaration) && !(n instanceof TypeDeclaration)) {
            n = n.getParent();
        }
        if (n instanceof AnonymousClassDeclaration) {
            AnonymousClassDeclaration outerDecl = (AnonymousClassDeclaration) n;
            outerDecl.bodyDeclarations().add(typeDecl);
        }
    } else {
        AbstractTypeDeclaration outerDecl = (AbstractTypeDeclaration) unit.findDeclaringNode(outerType);
        outerDecl.bodyDeclarations().add(typeDecl);
    }
    Symbols.scanAST(typeDecl);
    super.endVisit(node);
}

From source file:com.google.devtools.j2cpp.translate.DeadCodeEliminator.java

License:Open Source License

@Override
public void endVisit(ImportDeclaration node) {
    IBinding binding = node.resolveBinding();
    if (binding instanceof IMethodBinding) {
        // Remove static imports for dead methods
        IMethodBinding method = (IMethodBinding) binding;
        if (allMethodsDeadWithName(method.getDeclaringClass(), getProGuardName(method))) {
            node.delete();//  ww w .  j a  v  a 2s.  co  m
        }
    } else if (binding instanceof IVariableBinding) {
        // Remove static imports for dead non-constant fields
        IVariableBinding var = (IVariableBinding) binding;
        String clazz = Types.getSignature(var.getDeclaringClass());
        String name = var.getName();
        if (deadCodeMap.isDeadField(clazz, name) && var.getConstantValue() == null) {
            node.delete();
        }
    }
    // Skip on-demand imports
}

From source file:com.google.devtools.j2cpp.translate.InitializationNormalizer.java

License:Open Source License

/**
 * Strip field initializers, convert them to assignment statements, and
 * add them to the appropriate list of initialization statements.
 *//*from   w  w w. jav a2  s .  c om*/
private void addFieldInitializer(BodyDeclaration member, List<Statement> initStatements,
        List<Statement> classInitStatements) {
    FieldDeclaration field = (FieldDeclaration) member;
    @SuppressWarnings("unchecked")
    List<VariableDeclarationFragment> fragments = field.fragments(); // safe by specification
    for (VariableDeclarationFragment frag : fragments) {
        if (frag.getInitializer() != null) {
            Statement assignStmt = makeAssignmentStatement(frag);
            if (Modifier.isStatic(field.getModifiers())) {
                IVariableBinding binding = Types.getVariableBinding(frag);
                if (binding.getConstantValue() == null) { // constants don't need initialization
                    classInitStatements.add(assignStmt);
                    frag.setInitializer(null);
                }
            } else {
                // always initialize instance variables, since they can't be constants
                initStatements.add(assignStmt);
                frag.setInitializer(null);
            }
        }
    }
}

From source file:com.google.devtools.j2cpp.types.Types.java

License:Open Source License

public static boolean isPrimitiveConstant(IVariableBinding binding) {
    return binding != null && isConstantVariable(binding) && binding.getType().isPrimitive()
            && binding.getConstantValue() != null;
}