Coverage report

| %line | %branch
--- | --- | ---

org.apache.commons.jelly.tags.core.InvokeTag

86%

100%

1

     /*

2

      * Copyright 2002,2004 The Apache Software Foundation.

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.apache.commons.jelly.tags.core;

17

18

     import java.lang.reflect.InvocationTargetException;

19

     import java.util.ArrayList;

20

     import java.util.List;

21

22

     import org.apache.commons.beanutils.MethodUtils;

23

     import org.apache.commons.jelly.JellyTagException;

24

     import org.apache.commons.jelly.MissingAttributeException;

25

     import org.apache.commons.jelly.TagSupport;

26

     import org.apache.commons.jelly.XMLOutput;

27

28

     /**

29

       * A tag which calls a method in an object instantied by core:new

30

       *

31

       *

32

       * @author Rodney Waldhoff

33

       * @version $Revision: 155420 $

34

       */

35

     public class InvokeTag extends TagSupport implements ArgTagParent {

36

37

         /** the variable exported */

38

         private String var;

39

40

         /** the variable where the method's exception is exported */

41

         private String exceptionVar;

42

43

         /** the method to invoke */

44

         private String methodName;

45

46

         /** the object to invoke the method on */

47

         private Object onInstance;

48

49

221

         private List paramTypes = new ArrayList();

50

221

         private List paramValues = new ArrayList();

51

52

221

         public InvokeTag() {

53

221

         }

54

55

         /** Sets the name of the variable exported by this tag */

56

         public void setVar(String var) {

57

26

             this.var = <span class="keyword">var;

58

26

         }

59

60

         /** Sets the name of a variable that exports the exception thrown by

61

          * the method's invocation (if any)

62

          */

63

         public void setExceptionVar(String var) {

64

13

             this.exceptionVar = var;

65

13

         }

66

67

         public void setMethod(String method) {

68

221

             this.methodName = method;

69

221

         }

70

71

         public void setOn(Object instance) {

72

221

             this.onInstance = instance;

73

221

         }

74

75

         public void addArgument(Class type, Object value) {

76

208

             paramTypes.add(type);

77

208

             paramValues.add(value);

78

208

         }

79

80

         // Tag interface

81

         //-------------------------------------------------------------------------

82

         public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {

83

221

             if ( null == methodName) {

84

0

                 throw new MissingAttributeException( "method" );

85

             }

86

221

             if ( null == onInstance ) {

87

0

                 throw new MissingAttributeException( "on" );

88

             }

89

90

221

             invokeBody(output);

91

92

221

             Object[] values = paramValues.toArray();

93

221

             Class[] types = (Class[])(paramTypes.toArray(new Class[paramTypes.size()]));

94

95

221

             Object result = null;

96

             try {

97

221

                 result = MethodUtils.invokeMethod(onInstance,methodName,values,types);

98

             }

99

0

             catch (NoSuchMethodException e) {

100

0

                 throw new JellyTagException(e);

101

             }

102

0

             catch (IllegalAccessException e) {

103

0

                 throw new JellyTagException(e);

104

             }

105

26

             catch (InvocationTargetException e) {

106

26

                 if(null != exceptionVar) {

107

13

                     context.setVariable(exceptionVar,e.getTargetException());

108

13

                 } else {

109

13

                     throw new JellyTagException("method " + methodName +

110

                         " threw exception: "+ e.getTargetException().getMessage(),

111

                         e.getTargetException() );

112

                 }

113

             }

114

             finally {

115

221

                 paramTypes.clear();

116

221

                 paramValues.clear();

117

221

             }

118

119

221

             ArgTag parentArg = (ArgTag)(findAncestorWithClass(ArgTag.class));

120

208

             if(null != parentArg) {

121

52

                 parentArg.setValue(result);

122

             }

123

208

             if(null != var) {

124

26

                 context.setVariable(var, result);

125

             }

126

208

         }

127

     }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.