From 63eeac0b800f6797dd406a1ef109b2eacf804e1a Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Thu, 3 Sep 2009 07:23:59 +0000 Subject: [PATCH] extract method git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@810791 13f79535-47bb-0310-9956-ffa450edef68 --- .../ant/filters/util/ChainReaderHelper.java | 132 ++++++++++-------- 1 file changed, 72 insertions(+), 60 deletions(-) diff --git a/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java b/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java index 41ba53b1f..707702965 100644 --- a/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java +++ b/src/main/org/apache/tools/ant/filters/util/ChainReaderHelper.java @@ -145,66 +145,9 @@ public final class ChainReaderHelper { Object o = finalFilters.elementAt(i); if (o instanceof AntFilterReader) { - final AntFilterReader filter - = (AntFilterReader) finalFilters.elementAt(i); - final String className = filter.getClassName(); - final Path classpath = filter.getClasspath(); - final Project pro = filter.getProject(); - if (className != null) { - try { - Class clazz = null; - if (classpath == null) { - clazz = Class.forName(className); - } else { - AntClassLoader al - = pro.createClassLoader(classpath); - classLoadersToCleanUp.add(al); - clazz = Class.forName(className, true, al); - } - if (clazz != null) { - if (!FilterReader.class.isAssignableFrom(clazz)) { - throw new BuildException(className - + " does not extend java.io.FilterReader"); - } - final Constructor[] constructors = - clazz.getConstructors(); - int j = 0; - boolean consPresent = false; - for (; j < constructors.length; j++) { - Class[] types = constructors[j] - .getParameterTypes(); - if (types.length == 1 - && types[0].isAssignableFrom(Reader.class)) { - consPresent = true; - break; - } - } - if (!consPresent) { - throw new BuildException(className - + " does not define a public constructor" - + " that takes in a Reader as its " - + "single argument."); - } - final Reader[] rdr = {instream}; - instream = - (Reader) constructors[j].newInstance((Object[]) rdr); - setProjectOnObject(instream); - if (Parameterizable.class.isAssignableFrom(clazz)) { - final Parameter[] params = filter.getParams(); - ((Parameterizable) - instream).setParameters(params); - } - } - } catch (final ClassNotFoundException cnfe) { - throw new BuildException(cnfe); - } catch (final InstantiationException ie) { - throw new BuildException(ie); - } catch (final IllegalAccessException iae) { - throw new BuildException(iae); - } catch (final InvocationTargetException ite) { - throw new BuildException(ite); - } - } + instream = + expandReader((AntFilterReader) finalFilters.elementAt(i), + instream, classLoadersToCleanUp); } else if (o instanceof ChainableReader) { setProjectOnObject(o); instream = ((ChainableReader) o).chain(instream); @@ -271,4 +214,73 @@ public final class ChainReaderHelper { throws IOException { return FileUtils.readFully(rdr, bufferSize); } + + /** + * Creates and parameterizes a new FilterReader from a + * <filterreader> element. + * + * @since Ant 1.8.0 + */ + private Reader expandReader(AntFilterReader filter, Reader ancestor, + List/**/ classLoadersToCleanUp) { + final String className = filter.getClassName(); + final Path classpath = filter.getClasspath(); + final Project pro = filter.getProject(); + if (className != null) { + try { + Class clazz = null; + if (classpath == null) { + clazz = Class.forName(className); + } else { + AntClassLoader al = pro.createClassLoader(classpath); + classLoadersToCleanUp.add(al); + clazz = Class.forName(className, true, al); + } + if (clazz != null) { + if (!FilterReader.class.isAssignableFrom(clazz)) { + throw new BuildException(className + " does not extend" + + " java.io.FilterReader"); + } + final Constructor[] constructors = clazz.getConstructors(); + int j = 0; + boolean consPresent = false; + for (; j < constructors.length; j++) { + Class[] types = constructors[j].getParameterTypes(); + if (types.length == 1 + && types[0].isAssignableFrom(Reader.class)) { + consPresent = true; + break; + } + } + if (!consPresent) { + throw new BuildException(className + " does not define" + + " a public constructor" + + " that takes in a Reader" + + " as its single argument."); + } + final Reader[] rdr = {ancestor}; + Reader instream = + (Reader) constructors[j].newInstance((Object[]) rdr); + setProjectOnObject(instream); + if (Parameterizable.class.isAssignableFrom(clazz)) { + final Parameter[] params = filter.getParams(); + ((Parameterizable) instream).setParameters(params); + } + return instream; + } + } catch (final ClassNotFoundException cnfe) { + throw new BuildException(cnfe); + } catch (final InstantiationException ie) { + throw new BuildException(ie); + } catch (final IllegalAccessException iae) { + throw new BuildException(iae); + } catch (final InvocationTargetException ite) { + throw new BuildException(ite); + } + } + // Ant 1.7.1 and earlier ignore without a + // classname attribute, not sure this is a good idea - + // backwards compatibility makes it hard to change, though. + return ancestor; + } }