All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.syndication.feed.impl.ToStringBean Maven / Gradle / Ivy

There is a newer version: 3.1.0-incubating
Show newest version
/*
 * Copyright 2004 Sun Microsystems, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.sun.syndication.feed.impl;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Stack;
import java.io.Serializable;

/**
 * Provides deep Bean toString support.
 * 

* It works on all read/write properties, recursively. It support all primitive types, Strings, Collections, * ToString objects and multi-dimensional arrays of any of them. *

* @author Alejandro Abdelnur * */ public class ToStringBean implements Serializable { private static final ThreadLocal PREFIX_TL = new ThreadLocal() { public Object get() { Object o = super.get(); if (o==null) { o = new Stack(); set(o); } return o; } }; private static final Object[] NO_PARAMS = new Object[0]; private Class _beanClass; private Object _obj; /** * Default constructor. *

* To be used by classes extending ToStringBean only. *

* @param beanClass indicates the class to scan for properties, normally an interface class. * */ protected ToStringBean(Class beanClass) { _beanClass = beanClass; _obj = this; } /** * Creates a ToStringBean to be used in a delegation pattern. *

* For example: *

* * public class Foo implements ToString { * * public String toString(String prefix) { * ToStringBean tsb = new ToStringBean(this); * return tsb.toString(prefix); * } * * public String toString() { * return toString("Foo"); * } * * } * *

* @param beanClass indicates the class to scan for properties, normally an interface class. * @param obj object bean to create String representation. * */ public ToStringBean(Class beanClass,Object obj) { _beanClass = beanClass; _obj = obj; } /** * Returns the String representation of the bean given in the constructor. *

* It uses the Class name as the prefix. *

* @return bean object String representation. * */ public String toString() { Stack stack = (Stack) PREFIX_TL.get(); String[] tsInfo = (String[]) ((stack.isEmpty()) ? null : stack.peek()); String prefix; if (tsInfo==null) { String className = _obj.getClass().getName(); prefix = className.substring(className.lastIndexOf(".")+1); } else { prefix = tsInfo[0]; tsInfo[1] = prefix; } return toString(prefix); } /** * Returns the String representation of the bean given in the constructor. *

* @param prefix to use for bean properties. * @return bean object String representation. * */ private String toString(String prefix) { StringBuffer sb = new StringBuffer(128); try { PropertyDescriptor[] pds = BeanIntrospector.getPropertyDescriptors(_beanClass); if (pds!=null) { for (int i=0;i





© 2015 - 2025 Weber Informatics LLC | Privacy Policy