views:

1149

answers:

1

When attempting to use Joshua Bloch's "Builder Pattern" [Item 2 in Effective Java Second Edition] with reflection [object = constructors[index].newInstance(constructorParameterValues);] the following exception occurs:

java.lang.IllegalAccessException: Class info.soaj.core.util.SjUtilReflection can not access a member of class info.soaj.core.attribute.SjAttributesForThrowable with modifiers "private"

Note: This has been resolved. The accessible (private) constructor was being discarded and a non-accessible (override = false) was being attempted. Bottom Line: Programmer Error

An example Builder Class follows:

package info.soaj.core.attribute;

import info.soaj.core.attribute.internal.SjAttributesForStronglyTypedWrappers;
import info.soaj.core.internal.string.SjPopulatedClassName;
import info.soaj.core.internal.string.SjPopulatedMethodName;
import info.soaj.core.util.internal.SjUtilThrowable;

import java.io.Serializable;

/**
 * <p>
 * The "Builder" pattern as documented by Joshua Bloch ("Effective Java" -
 * Second Edition) is utilized to handle the variable number of required and
 * optional parameters.
 * </p>
 * 
 * <p style="font-family:Verdana; font-size:10px; font-style:italic"> Copyright
 * (c) 2006 - 2008 by Global Technology Consulting Group, Inc. at <a
 * href="http://gtcGroup.com"&gt;gtcGroup.com </a>. </p>
 * 
 * @author [email protected]
 * @since v. 1.0
 */

public class SjAttributesExample implements Serializable {

    /** UID */
    private static final long serialVersionUID = 1L;

    /** The name of class throwing the exception. */
    protected final SjPopulatedClassName classname;

    /** The name of method throwing the exception. */
    protected final SjPopulatedMethodName methodname;

    /**
     * Suppresses logging; default is <code>false</code>.
     */
    protected final boolean suppressLoggingOnly;

    /**
     * Constructor - private
     * 
     * @param builderThrowable
     */
    private SjAttributesExample(final BuilderThrowable builderThrowable) {
     this.classname = builderThrowable.classname;
     this.methodname = builderThrowable.methodname;
     this.suppressLoggingOnly = builderThrowable.suppressLoggingOnly;
    }

    /**
     * This static member immutable class is used to implement the builder
     * pattern.
     * 
     * @author [email protected]
     * @since v. 1.0
     */
    public static class BuilderThrowable {

     /** Class name. */
     private static final String CLASS_NAME = BuilderThrowable.class
       .getName();

     // Required attributes.

     /** The name of class throwing the exception. */
     protected final SjPopulatedClassName classname;

     /** The name of method throwing the exception. */
     protected final SjPopulatedMethodName methodname;

     // Optional attributes.

     /** Prevents action from occurring. Default is false. */
     protected boolean suppressLoggingOnly = false;

     /**
      * Constructor
      * 
      * @param classname
      * @param methodname
      */
     public BuilderThrowable(final String classname, final String methodname) {

      super();

      final String Method_Name = "BuilderThrowable";

      // What happens when handling an exception throws an exception?
      try {

       this.classname = new SjPopulatedClassName(classname,
         new SjAttributesForStronglyTypedWrappers(CLASS_NAME,
           Method_Name));

       this.methodname = new SjPopulatedMethodName(methodname,
         new SjAttributesForStronglyTypedWrappers(CLASS_NAME,
           Method_Name));

      } catch (final RuntimeException e) {

       // Log the contextual details.
       SjUtilThrowable.logExceptionOccuredWhileThrowingException(
         CLASS_NAME, Method_Name, e);

       throw e;
      }

      return;
     }

     /**
      * This method sets a flag to suppress logging.
      * 
      * @param isLoggingSuppressed
      * @return BuilderThrowable
      */
     public BuilderThrowable suppressLoggingOnly(
       final boolean isLoggingSuppressed) {

      this.suppressLoggingOnly = isLoggingSuppressed;

      return this;
     }

     /**
      * This method is used for instantiating this class.
      * 
      * @return SjAttributesForThrowable
      */
     @SuppressWarnings("synthetic-access")
     public SjAttributesExample build() {

      return new SjAttributesExample(this);
     }
    }

    /**
     * This method returns an attribute.
     * 
     * @return String - Returns the <code>classname</code> attribute.
     */
    public String getClassname() {
     return this.classname.getString();
    }

    /**
     * This method returns an attribute.
     * 
     * @return String - Returns the <code>methodname</code> attribute.
     */
    public String getMethodname() {
     return this.methodname.getString();
    }

    /**
     * This method returns an attribute.
     * 
     * @return boolean - Returns the <code>suppressLoggingOnly</code> attribute.
     */
    public boolean isLoggingSuppressed() {
     return this.suppressLoggingOnly;
    }
}
A: 

Note: This has been resolved. The accessible (private) constructor was being discarded and a non-accessible (override = false) was being attempted. Bottom Line: Programmer Error