View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.model.interpolation;
20  
21  /*
22   * Copyright 2007 The Codehaus Foundation.
23   *
24   * Licensed under the Apache License, Version 2.0 (the "License");
25   * you may not use this file except in compliance with the License.
26   * You may obtain a copy of the License at
27   *
28   *      http://www.apache.org/licenses/LICENSE-2.0
29   *
30   * Unless required by applicable law or agreed to in writing, software
31   * distributed under the License is distributed on an "AS IS" BASIS,
32   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33   * See the License for the specific language governing permissions and
34   * limitations under the License.
35   */
36  
37  import org.apache.maven.model.interpolation.reflection.ReflectionValueExtractor;
38  import org.codehaus.plexus.interpolation.AbstractValueSource;
39  
40  /**
41   * Wraps an object, providing reflective access to the object graph of which the
42   * supplied object is the root. Expressions like 'child.name' will translate into
43   * 'rootObject.getChild().getName()' for non-boolean properties, and
44   * 'rootObject.getChild().isName()' for boolean properties.
45   *
46   * @deprecated use {@link org.apache.maven.api.services.ModelBuilder} instead
47   */
48  @Deprecated(since = "4.0.0")
49  public class ObjectBasedValueSource extends AbstractValueSource {
50  
51      private final Object root;
52  
53      /**
54       * Construct a new value source, using the supplied object as the root from
55       * which to start, and using expressions split at the dot ('.') to navigate
56       * the object graph beneath this root.
57       * @param root the root of the graph.
58       */
59      public ObjectBasedValueSource(Object root) {
60          super(true);
61          this.root = root;
62      }
63  
64      /**
65       * <p>Split the expression into parts, tokenized on the dot ('.') character. Then,
66       * starting at the root object contained in this value source, apply each part
67       * to the object graph below this root, using either 'getXXX()' or 'isXXX()'
68       * accessor types to resolve the value for each successive expression part.
69       * Finally, return the result of the last expression part's resolution.</p>
70       *
71       * <p><b>NOTE:</b> The object-graph nagivation actually takes place via the
72       * {@link ReflectionValueExtractor} class.</p>
73       */
74      public Object getValue(String expression) {
75          if (expression == null || expression.trim().isEmpty()) {
76              return null;
77          }
78  
79          try {
80              return ReflectionValueExtractor.evaluate(expression, root, false);
81          } catch (Exception e) {
82              addFeedback("Failed to extract \'" + expression + "\' from: " + root, e);
83          }
84  
85          return null;
86      }
87  }