diff --git a/src/main/org/apache/tools/ant/IntrospectionHelper.java b/src/main/org/apache/tools/ant/IntrospectionHelper.java index 7b5c44d85..590828d68 100644 --- a/src/main/org/apache/tools/ant/IntrospectionHelper.java +++ b/src/main/org/apache/tools/ant/IntrospectionHelper.java @@ -310,12 +310,7 @@ public final class IntrospectionHelper implements BuildListener { * @return a helper for the specified class */ public static synchronized IntrospectionHelper getHelper(Class c) { - IntrospectionHelper ih = (IntrospectionHelper) helpers.get(c); - if (ih == null) { - ih = new IntrospectionHelper(c); - helpers.put(c, ih); - } - return ih; + return getHelper(null, c); } /** @@ -332,9 +327,19 @@ public final class IntrospectionHelper implements BuildListener { * @return a helper for the specified class */ public static IntrospectionHelper getHelper(Project p, Class c) { - IntrospectionHelper ih = getHelper(c); - // Cleanup at end of project - p.addBuildListener(ih); + IntrospectionHelper ih = (IntrospectionHelper) helpers.get(c); + if (ih == null) { + ih = new IntrospectionHelper(c); + if (p != null) { + // #30162: do *not* cache this if there is no project, as we + // cannot guarantee that the cache will be cleared. + helpers.put(c, ih); + } + } + if (p != null) { + // Cleanup at end of project + p.addBuildListener(ih); + } return ih; } diff --git a/src/main/org/apache/tools/ant/ProjectHelper.java b/src/main/org/apache/tools/ant/ProjectHelper.java index 15ae624eb..5e0d515ce 100644 --- a/src/main/org/apache/tools/ant/ProjectHelper.java +++ b/src/main/org/apache/tools/ant/ProjectHelper.java @@ -304,9 +304,7 @@ public class ProjectHelper { } IntrospectionHelper ih = - IntrospectionHelper.getHelper(target.getClass()); - - project.addBuildListener(ih); + IntrospectionHelper.getHelper(project, target.getClass()); for (int i = 0; i < attrs.getLength(); i++) { // reflect these into the target @@ -368,7 +366,7 @@ public class ProjectHelper { target = ((TypeAdapter) target).getProxy(); } - IntrospectionHelper.getHelper(target.getClass()).addText(project, + IntrospectionHelper.getHelper(project, target.getClass()).addText(project, target, text); } @@ -388,7 +386,7 @@ public class ProjectHelper { public static void storeChild(Project project, Object parent, Object child, String tag) { IntrospectionHelper ih - = IntrospectionHelper.getHelper(parent.getClass()); + = IntrospectionHelper.getHelper(project, parent.getClass()); ih.storeElement(project, parent, child, tag); } diff --git a/src/main/org/apache/tools/ant/UnknownElement.java b/src/main/org/apache/tools/ant/UnknownElement.java index 9a0165376..65687cdcb 100644 --- a/src/main/org/apache/tools/ant/UnknownElement.java +++ b/src/main/org/apache/tools/ant/UnknownElement.java @@ -320,7 +320,7 @@ public class UnknownElement extends Task { String parentUri = getNamespace(); Class parentClass = parent.getClass(); - IntrospectionHelper ih = IntrospectionHelper.getHelper(parentClass); + IntrospectionHelper ih = IntrospectionHelper.getHelper(getProject(), parentClass); if (children != null) { diff --git a/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java b/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java index 19cc2fac4..02505de65 100644 --- a/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java +++ b/src/main/org/apache/tools/ant/helper/ProjectHelperImpl.java @@ -863,7 +863,7 @@ public class ProjectHelperImpl extends ProjectHelper { public void init(String propType, AttributeList attrs) throws SAXParseException { Class parentClass = parent.getClass(); IntrospectionHelper ih = - IntrospectionHelper.getHelper(parentClass); + IntrospectionHelper.getHelper(helperImpl.project, parentClass); try { String elementName = propType.toLowerCase(Locale.US); diff --git a/src/main/org/apache/tools/ant/taskdefs/AntStructure.java b/src/main/org/apache/tools/ant/taskdefs/AntStructure.java index 14aefe933..41c07a4c7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/AntStructure.java +++ b/src/main/org/apache/tools/ant/taskdefs/AntStructure.java @@ -200,7 +200,7 @@ public class AntStructure extends Task { IntrospectionHelper ih = null; try { - ih = IntrospectionHelper.getHelper(element); + ih = IntrospectionHelper.getHelper(getProject(), element); } catch (Throwable t) { /* * XXX - failed to load the class properly.