PageRenderTime 110ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/subprojects/core/src/main/groovy/org/gradle/api/internal/project/ant/BasicAntBuilder.java

https://github.com/andrewhj-mn/gradle
Java | 108 lines | 76 code | 14 blank | 18 comment | 0 complexity | 400b45cb6e45b316e2dc4f5ec5f1f7c3 MD5 | raw file
  1. /*
  2. * Copyright 2009 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.gradle.api.internal.project.ant;
  17. import groovy.util.AntBuilder;
  18. import org.apache.tools.ant.Project;
  19. import org.apache.tools.ant.Target;
  20. import org.gradle.api.Transformer;
  21. import org.gradle.api.internal.file.ant.AntFileResource;
  22. import org.gradle.api.internal.file.ant.BaseDirSelector;
  23. import java.io.Closeable;
  24. import java.lang.reflect.Field;
  25. import java.util.List;
  26. import java.util.Map;
  27. public class BasicAntBuilder extends org.gradle.api.AntBuilder implements Closeable {
  28. private final Field nodeField;
  29. private final List children;
  30. public BasicAntBuilder() {
  31. // These are used to discard references to tasks so they can be garbage collected
  32. Field collectorField;
  33. try {
  34. nodeField = AntBuilder.class.getDeclaredField("lastCompletedNode");
  35. nodeField.setAccessible(true);
  36. collectorField = AntBuilder.class.getDeclaredField("collectorTarget");
  37. collectorField.setAccessible(true);
  38. Target target = (Target) collectorField.get(this);
  39. Field childrenField = Target.class.getDeclaredField("children");
  40. childrenField.setAccessible(true);
  41. children = (List) childrenField.get(target);
  42. } catch (Exception e) {
  43. throw new RuntimeException(e);
  44. }
  45. getAntProject().addDataTypeDefinition("gradleFileResource", AntFileResource.class);
  46. getAntProject().addDataTypeDefinition("gradleBaseDirSelector", BaseDirSelector.class);
  47. }
  48. @Override
  49. public Map<String, Object> getProperties() {
  50. throw new UnsupportedOperationException();
  51. }
  52. @Override
  53. public Map<String, Object> getReferences() {
  54. throw new UnsupportedOperationException();
  55. }
  56. public void importBuild(Object antBuildFile) {
  57. throw new UnsupportedOperationException();
  58. }
  59. public void importBuild(Object antBuildFile, Transformer<? extends String, ? super String> taskNamer) {
  60. throw new UnsupportedOperationException();
  61. }
  62. @Override
  63. protected void nodeCompleted(Object parent, Object node) {
  64. ClassLoader original = Thread.currentThread().getContextClassLoader();
  65. Thread.currentThread().setContextClassLoader(Project.class.getClassLoader());
  66. try {
  67. super.nodeCompleted(parent, node);
  68. } finally {
  69. Thread.currentThread().setContextClassLoader(original);
  70. }
  71. }
  72. protected Object postNodeCompletion(Object parent, Object node) {
  73. try {
  74. return nodeField.get(this);
  75. } catch (IllegalAccessException e) {
  76. throw new RuntimeException(e);
  77. }
  78. }
  79. protected Object doInvokeMethod(String methodName, Object name, Object args) {
  80. Object value = super.doInvokeMethod(methodName, name, args);
  81. // Discard the node so it can be garbage collected. Some Ant tasks cache a potentially large amount of state
  82. // in fields.
  83. try {
  84. nodeField.set(this, null);
  85. children.clear();
  86. } catch (IllegalAccessException e) {
  87. throw new RuntimeException(e);
  88. }
  89. return value;
  90. }
  91. public void close() {
  92. getProject().fireBuildFinished(null);
  93. }
  94. }