/projects/cayenne-3.0.1/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/enhancer/AccessorVisitor.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 148 lines · 93 code · 28 blank · 27 comment · 16 complexity · 8767c1d1c4f7d1dc750c4ebad3b448a2 MD5 · raw file

  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.cayenne.enhancer;
  20. import java.util.regex.Matcher;
  21. import java.util.regex.Pattern;
  22. import org.objectweb.asm.ClassAdapter;
  23. import org.objectweb.asm.ClassVisitor;
  24. import org.objectweb.asm.MethodVisitor;
  25. import org.objectweb.asm.Type;
  26. /**
  27. * An enhancer that adds interceptor code to the getters and setters.
  28. *
  29. * @since 3.0
  30. */
  31. public abstract class AccessorVisitor extends ClassAdapter {
  32. // duplicated from JpaClassDescriptor.
  33. private static final Pattern GETTER_PATTERN = Pattern
  34. .compile("^(is|get)([A-Z])(.*)$");
  35. private static final Pattern SETTER_PATTERN = Pattern.compile("^set([A-Z])(.*)$");
  36. private EnhancementHelper helper;
  37. public static String propertyNameForGetter(String getterName) {
  38. Matcher getMatch = GETTER_PATTERN.matcher(getterName);
  39. if (getMatch.matches()) {
  40. return getMatch.group(2).toLowerCase() + getMatch.group(3);
  41. }
  42. return null;
  43. }
  44. public static String propertyNameForSetter(String setterName) {
  45. Matcher setMatch = SETTER_PATTERN.matcher(setterName);
  46. if (setMatch.matches()) {
  47. return setMatch.group(1).toLowerCase() + setMatch.group(2);
  48. }
  49. return null;
  50. }
  51. public AccessorVisitor(ClassVisitor cw) {
  52. super(cw);
  53. this.helper = new EnhancementHelper(this);
  54. }
  55. protected abstract boolean isEnhancedProperty(String property);
  56. protected abstract boolean isLazyFaulted(String property);
  57. @Override
  58. public void visit(
  59. int version,
  60. int access,
  61. String name,
  62. String signature,
  63. String superName,
  64. String[] interfaces) {
  65. helper.reset(name);
  66. super.visit(version, access, name, signature, superName, interfaces);
  67. }
  68. protected MethodVisitor visitGetter(
  69. MethodVisitor mv,
  70. String property,
  71. Type propertyType) {
  72. if (isEnhancedProperty(property)) {
  73. if (isLazyFaulted(property)) {
  74. return new GetterVisitor(mv, helper, property, true);
  75. }
  76. else {
  77. return new GetterVisitor(mv, helper, property, false);
  78. }
  79. }
  80. return mv;
  81. }
  82. protected MethodVisitor visitSetter(
  83. MethodVisitor mv,
  84. String property,
  85. Type propertyType) {
  86. if (isEnhancedProperty(property)) {
  87. return new SetterVisitor(mv, helper, property, propertyType);
  88. }
  89. return mv;
  90. }
  91. @Override
  92. public MethodVisitor visitMethod(
  93. int access,
  94. String name,
  95. String desc,
  96. String signature,
  97. String[] exceptions) {
  98. MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
  99. // TODO: andrus, 10/8/2006 - what other signature checks do we need to do?
  100. Type returnType = Type.getReturnType(desc);
  101. Type[] args = Type.getArgumentTypes(desc);
  102. // possible setter
  103. if ("V".equals(returnType.getDescriptor())) {
  104. if (args.length == 1) {
  105. String setProperty = AccessorVisitor.propertyNameForSetter(name);
  106. if (setProperty != null) {
  107. return visitSetter(mv, setProperty, args[0]);
  108. }
  109. }
  110. }
  111. // possible getter
  112. else if (args.length == 0) {
  113. String getProperty = AccessorVisitor.propertyNameForGetter(name);
  114. if (getProperty != null) {
  115. return visitGetter(mv, getProperty, returnType);
  116. }
  117. }
  118. return mv;
  119. }
  120. }