Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004-2010 Oracle Corporation. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Kohsuke Kawaguchi /
  
 
 package org.kohsuke.stapler.export;
 
 
 import java.net.URL;
 import java.util.Date;
 import java.util.Map;
 import java.util.Set;

Exposes one exposed property of ExportedBean to DataWriter.

Author(s):
Kohsuke Kawaguchi
 
 public abstract class Property implements Comparable<Property> {
    
Name of the property.
 
     public final String name;
     final ModelBuilder owner;
    
Visibility depth level of this property.

 
     public final int visibility;

    
Model to which this property belongs to. Never null.
 
     public final Model parent;

    
 
     public final boolean inline;
 
     private String[] verboseMap;
 
     Property(Model parentString nameExported exported) {
         this. = parent;
         this. = parent.parent;
         this. = exported.name().length()>1 ? exported.name() : name;
         int v = exported.visibility();
         if(v==0)
             v = parent.defaultVisibility;
         this. = v;
         this. = exported.inline();
         String[] s = exported.verboseMap().split("/");
         if (s.length<2)
             this. = null;
         else
             this. = s;
     }
 
     public int compareTo(Property that) {
         return this..compareTo(that.name);
     }
 
     public abstract Type getGenericType();
     public abstract Class getType();

    
Gets the associated javadoc, if any, or null.
 
     public abstract String getJavadoc();

    
Writes one property of the given object to DataWriter.

Parameters:
pruner Determines how to prune the object graph tree.
 
     public void writeTo(Object objectTreePruner prunerDataWriter writerthrows IOException {
        TreePruner child = pruner.accept(objectthis);
        if (child==null)        return;
        try {
            writer.name();
            writeValue(getValue(object),child,writer);
        } catch (IllegalAccessException e) {
            IOException x = new IOException("Failed to write " + );
            x.initCause(e);
            throw x;
        } catch (InvocationTargetException e) {
            IOException x = new IOException("Failed to write " + );
            x.initCause(e);
            throw x;
        }
    }

    

Deprecated:
as of 1.139
    public void writeTo(Object objectint depthDataWriter writerthrows IOException {
        writeTo(object,new ByDepth(depth),writer);
    }

    
Writes one value of the property to DataWriter.
    private void writeValue(Object valueTreePruner prunerDataWriter writerthrows IOException {
        writeValue(value,pruner,writer,false);
    }

    
Writes one value of the property to DataWriter.
    private void writeValue(Object valueTreePruner prunerDataWriter writerboolean skipIfFailthrows IOException {
        if(value==null) {
            writer.valueNull();
            return;
        }
        if(value instanceof CustomExportedBean) {
            writeValue(((CustomExportedBean)value).toExportedObject(),pruner,writer);
            return;
        }
        Class c = value.getClass();
        if(.contains(c)) {
            writer.value(value.toString());
            return;
        }
        if(.contains(c)) {
            writer.valuePrimitive(value);
            return;
        }
        if(c.getComponentType()!=null) { // array
            writer.startArray();
            if (value instanceof Object[]) {
                // typical case
                for (Object item : (Object[]) value)
                    writeValue(item,pruner,writer,true);
            } else {
                // more generic case
                int len = Array.getLength(value);
                for (int i=0; i<leni++)
                    writeValue(Array.get(value,i),pruner,writer,true);
            }
            writer.endArray();
            return;
        }
        if(value instanceof Collection) {
            writer.startArray();
            for (Object item : (Collectionvalue)
                writeValue(item,pruner,writer,true);
            writer.endArray();
            return;
        }
        if(value instanceof Map) {
            if (!=null) {// verbose form
                writer.startArray();
                for (Map.Entry e : ((Map<?,?>) value).entrySet()) {
                    writer.startObject();
                    writer.name([0]);
                    writeValue(e.getKey(),pruner,writer);
                    writer.name([1]);
                    writeValue(e.getValue(),pruner,writer);
                    writer.endObject();
                }
                writer.endArray();
            } else {// compact form
                writer.startObject();
                for (Map.Entry e : ((Map<?,?>) value).entrySet()) {
                    writer.name(e.getKey().toString());
                    writeValue(e.getValue(),pruner,writer);
                }
                writer.endObject();
            }
            return;
        }
        if(value instanceof Date) {
            writer.valuePrimitive(((Datevalue).getTime());
            return;
        }
        if(value instanceof Calendar) {
            writer.valuePrimitive(((Calendarvalue).getTimeInMillis());
            return;
        }
        if(value instanceof Enum) {
            writer.value(value.toString());
            return;
        }
        // otherwise handle it as a bean
        writer.startObject();
        Model model = null;
        try {
            model = .get(c);
        } catch (NotExportableException e) {
            if(!skipIfFail)
                throw e;
            // otherwise ignore this error by writing empty object
        }
        if(model!=null)
            model.writeNestedObjectTo(value,pruner,writer);
        writer.endObject();
    }

    
Gets the value of this property from the bean.
    protected abstract Object getValue(Object beanthrows IllegalAccessExceptionInvocationTargetException;
    /*package*/ static final Set<ClassSTRING_TYPES = new HashSet<Class>(Arrays.asList(
        String.class,
        URL.class
    ));
    /*package*/ static final Set<ClassPRIMITIVE_TYPES = new HashSet<Class>(Arrays.asList(
        Integer.class,
        Long.class,
        Boolean.class,
        Short.class,
        Character.class,
        Float.class,
        Double.class
    ));
New to GrepCode? Check out our FAQ X