11adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov/** 211a89b445f3bde56bf07e6a0d04f0b0256dcb215Andrey Somov * Copyright (c) 2008, http://www.snakeyaml.org 31adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * 41adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * Licensed under the Apache License, Version 2.0 (the "License"); 51adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * you may not use this file except in compliance with the License. 61adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * You may obtain a copy of the License at 71adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * 81adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * http://www.apache.org/licenses/LICENSE-2.0 91adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * 101adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * Unless required by applicable law or agreed to in writing, software 111adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * distributed under the License is distributed on an "AS IS" BASIS, 121adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * See the License for the specific language governing permissions and 141adf3a94fce500e0bda933e4b495a004d5789bd7Andrey Somov * limitations under the License. 159629be70863521bead138c295351f3dec926ab1Andrey Somov */ 169629be70863521bead138c295351f3dec926ab1Andrey Somovpackage org.yaml.snakeyaml.introspector; 179629be70863521bead138c295351f3dec926ab1Andrey Somov 189629be70863521bead138c295351f3dec926ab1Andrey Somovimport java.beans.PropertyDescriptor; 199629be70863521bead138c295351f3dec926ab1Andrey Somov 209629be70863521bead138c295351f3dec926ab1Andrey Somovimport org.yaml.snakeyaml.error.YAMLException; 219629be70863521bead138c295351f3dec926ab1Andrey Somov 22cdbfe6b4eb506ee04dd4cbd54da7244a9d9876a2Andrey Somov/** 23cdbfe6b4eb506ee04dd4cbd54da7244a9d9876a2Andrey Somov * <p> 24cdbfe6b4eb506ee04dd4cbd54da7244a9d9876a2Andrey Somov * A <code>MethodProperty</code> is a <code>Property</code> which is accessed 25cdbfe6b4eb506ee04dd4cbd54da7244a9d9876a2Andrey Somov * through accessor methods (setX, getX). It is possible to have a 26cdbfe6b4eb506ee04dd4cbd54da7244a9d9876a2Andrey Somov * <code>MethodProperty</code> which has only setter, only getter, or both. It 27cdbfe6b4eb506ee04dd4cbd54da7244a9d9876a2Andrey Somov * is not possible to have a <code>MethodProperty</code> which has neither 28cdbfe6b4eb506ee04dd4cbd54da7244a9d9876a2Andrey Somov * setter nor getter. 29cdbfe6b4eb506ee04dd4cbd54da7244a9d9876a2Andrey Somov * </p> 30cdbfe6b4eb506ee04dd4cbd54da7244a9d9876a2Andrey Somov */ 31f03f2ae6057da48f3de7ad3fc50ee9c92633884cmaslovalexpublic class MethodProperty extends GenericProperty { 32964a641db77777caf35e57328e5c0d7d2a49e1baAndrey Somov 339629be70863521bead138c295351f3dec926ab1Andrey Somov private final PropertyDescriptor property; 3487bde16e30ef24e63364ac981b1a598d5f971737maslovalex private final boolean readable; 3587bde16e30ef24e63364ac981b1a598d5f971737maslovalex private final boolean writable; 369629be70863521bead138c295351f3dec926ab1Andrey Somov 379629be70863521bead138c295351f3dec926ab1Andrey Somov public MethodProperty(PropertyDescriptor property) { 38964a641db77777caf35e57328e5c0d7d2a49e1baAndrey Somov super(property.getName(), property.getPropertyType(), 39964a641db77777caf35e57328e5c0d7d2a49e1baAndrey Somov property.getReadMethod() == null ? null : property.getReadMethod() 40964a641db77777caf35e57328e5c0d7d2a49e1baAndrey Somov .getGenericReturnType()); 419629be70863521bead138c295351f3dec926ab1Andrey Somov this.property = property; 4287bde16e30ef24e63364ac981b1a598d5f971737maslovalex this.readable = property.getReadMethod() != null; 4387bde16e30ef24e63364ac981b1a598d5f971737maslovalex this.writable = property.getWriteMethod() != null; 449629be70863521bead138c295351f3dec926ab1Andrey Somov } 45964a641db77777caf35e57328e5c0d7d2a49e1baAndrey Somov 469629be70863521bead138c295351f3dec926ab1Andrey Somov @Override 479629be70863521bead138c295351f3dec926ab1Andrey Somov public void set(Object object, Object value) throws Exception { 489629be70863521bead138c295351f3dec926ab1Andrey Somov property.getWriteMethod().invoke(object, value); 499629be70863521bead138c295351f3dec926ab1Andrey Somov } 509629be70863521bead138c295351f3dec926ab1Andrey Somov 518caea31160c72bce4181ed38b16f850e06d29a14Andrey Somov @Override 529629be70863521bead138c295351f3dec926ab1Andrey Somov public Object get(Object object) { 539629be70863521bead138c295351f3dec926ab1Andrey Somov try { 544a6195802b89d4a186f3d01ec959124d0050c9baAndrey Somov property.getReadMethod().setAccessible(true);// issue 50 559629be70863521bead138c295351f3dec926ab1Andrey Somov return property.getReadMethod().invoke(object); 569629be70863521bead138c295351f3dec926ab1Andrey Somov } catch (Exception e) { 578caea31160c72bce4181ed38b16f850e06d29a14Andrey Somov throw new YAMLException("Unable to find getter for property '" + property.getName() 588caea31160c72bce4181ed38b16f850e06d29a14Andrey Somov + "' on object " + object + ":" + e); 599629be70863521bead138c295351f3dec926ab1Andrey Somov } 609629be70863521bead138c295351f3dec926ab1Andrey Somov } 6187bde16e30ef24e63364ac981b1a598d5f971737maslovalex 6287bde16e30ef24e63364ac981b1a598d5f971737maslovalex @Override 6387bde16e30ef24e63364ac981b1a598d5f971737maslovalex public boolean isWritable() { 6487bde16e30ef24e63364ac981b1a598d5f971737maslovalex return writable; 6587bde16e30ef24e63364ac981b1a598d5f971737maslovalex } 66964a641db77777caf35e57328e5c0d7d2a49e1baAndrey Somov 6787bde16e30ef24e63364ac981b1a598d5f971737maslovalex @Override 6887bde16e30ef24e63364ac981b1a598d5f971737maslovalex public boolean isReadable() { 6987bde16e30ef24e63364ac981b1a598d5f971737maslovalex return readable; 7087bde16e30ef24e63364ac981b1a598d5f971737maslovalex } 719629be70863521bead138c295351f3dec926ab1Andrey Somov}