Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2014 Red Hat, Inc., and individual contributors
   * as indicated by the @author tags.
   *
   * 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 org.jboss.marshalling;
 
A setter for a (possibly final) field, which allows for correct object initialization of java.io.Serializable objects with readObject() methods, even in the presence of final fields.
 
 public final class FieldSetter {
     private final Field field;
 
     private FieldSetter(final Field field) {
         this. = field;
     }

    
Set the value of the field to the given object.

Parameters:
instance the instance to set
value the new value
Throws:
java.lang.IllegalArgumentException if the given instance is null or not of the correct class
 
     public void set(Object instanceObject valuethrows IllegalArgumentException {
         try {
             .set(instancevalue);
         } catch (IllegalAccessException e) {
             throw illegalState(e);
         }
     }

    
Set the value of the field to the given value.

Parameters:
instance the instance to set
value the new value
Throws:
java.lang.IllegalArgumentException if the given instance is null or not of the correct class
 
     public void setBoolean(Object instanceboolean valuethrows IllegalArgumentException {
         try {
             .setBoolean(instancevalue);
         } catch (IllegalAccessException e) {
             throw illegalState(e);
         }
     }

    
Set the value of the field to the given value.

Parameters:
instance the instance to set
value the new value
Throws:
java.lang.IllegalArgumentException if the given instance is null or not of the correct class
 
     public void setByte(Object instancebyte valuethrows IllegalArgumentException {
         try {
             .setByte(instancevalue);
         } catch (IllegalAccessException e) {
             throw illegalState(e);
         }
     }

    
Set the value of the field to the given value.

Parameters:
instance the instance to set
value the new value
Throws:
java.lang.IllegalArgumentException if the given instance is null or not of the correct class
 
     public void setChar(Object instancechar valuethrows IllegalArgumentException {
         try {
             .setChar(instancevalue);
         } catch (IllegalAccessException e) {
             throw illegalState(e);
         }
     }

    
Set the value of the field to the given value.

Parameters:
instance the instance to set
value the new value
Throws:
java.lang.IllegalArgumentException if the given instance is null or not of the correct class
    public void setDouble(Object instancedouble valuethrows IllegalArgumentException {
        try {
            .setDouble(instancevalue);
        } catch (IllegalAccessException e) {
            throw illegalState(e);
        }
    }

    
Set the value of the field to the given value.

Parameters:
instance the instance to set
value the new value
Throws:
java.lang.IllegalArgumentException if the given instance is null or not of the correct class
    public void setFloat(Object instancefloat valuethrows IllegalArgumentException {
        try {
            .setFloat(instancevalue);
        } catch (IllegalAccessException e) {
            throw illegalState(e);
        }
    }

    
Set the value of the field to the given value.

Parameters:
instance the instance to set
value the new value
Throws:
java.lang.IllegalArgumentException if the given instance is null or not of the correct class
    public void setInt(Object instanceint valuethrows IllegalArgumentException {
        try {
            .setInt(instancevalue);
        } catch (IllegalAccessException e) {
            throw illegalState(e);
        }
    }

    
Set the value of the field to the given value.

Parameters:
instance the instance to set
value the new value
Throws:
java.lang.IllegalArgumentException if the given instance is null or not of the correct class
    public void setLong(Object instancelong valuethrows IllegalArgumentException {
        try {
            .setLong(instancevalue);
        } catch (IllegalAccessException e) {
            throw illegalState(e);
        }
    }

    
Set the value of the field to the given value.

Parameters:
instance the instance to set
value the new value
Throws:
java.lang.IllegalArgumentException if the given instance is null or not of the correct class
    public void setShort(Object instanceshort valuethrows IllegalArgumentException {
        try {
            .setShort(instancevalue);
        } catch (IllegalAccessException e) {
            throw illegalState(e);
        }
    }
        return new IllegalStateException("Unexpected illegal access of accessible field"e);
    }

    
Get an instance for the current class.

Parameters:
clazz the class containing the field
name the name of the field
Returns:
the Field instance
Throws:
java.lang.SecurityException if the field does not belong to the caller's class, or the field is static
java.lang.IllegalArgumentException if there is no field of the given name on the given class
    public static FieldSetter get(final Class<?> clazzfinal String namethrows SecurityExceptionIllegalArgumentException {
        final Class[] stackTrace = ..getClassContext();
        if (stackTrace[2] != clazz) {
            throw new SecurityException("Cannot get accessible field from someone else's class");
        }
        return new FieldSetter(AccessController.doPrivileged(new GetFieldAction(clazzname)));
    }
    private static final class Holder {
        static final StackTraceReader STACK_TRACE_READER;
        static {
             = AccessController.doPrivileged(new PrivilegedAction<StackTraceReader>() {
                public StackTraceReader run() {
                    return new StackTraceReader();
                }
            });
        }
        private Holder() {
        }
        static final class StackTraceReader extends SecurityManager {
            protected Class[] getClassContext() {
                return super.getClassContext();
            }
        }
    }
    private static class GetFieldAction implements PrivilegedAction<Field> {
        private final Class<?> clazz;
        private final String name;
        private GetFieldAction(final Class<?> clazzfinal String name) {
            this. = clazz;
            this. = name;
        }
        public Field run() {
            try {
                final Field field = .getDeclaredField();
                final int modifiers = field.getModifiers();
                if (Modifier.isStatic(modifiers)) {
                    throw new SecurityException("Cannot get access to static field '" +  + "'");
                }
                field.setAccessible(true);
                return field;
            } catch (NoSuchFieldException e) {
                throw new IllegalArgumentException("No such field '" +  + "'"e);
            }
        }
    }
New to GrepCode? Check out our FAQ X