001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.hivemind.annotations.internal;
016
017 import java.lang.reflect.Method;
018
019 import org.apache.hivemind.Location;
020 import org.apache.hivemind.Resource;
021
022 /**
023 * Implementation of the {@link org.apache.hivemind.Location} interface.
024 * Uses a method name of a class as position.
025 *
026 * @author Achim Huegen
027 */
028 public final class AnnotatedModuleLocation implements Location
029 {
030 private Resource _resource;
031 private Class _moduleClass;
032 private Method _method;
033
034 public AnnotatedModuleLocation(Resource resource, Class moduleClass, Method method)
035 {
036 _resource = resource;
037 _moduleClass = moduleClass;
038 _method = method;
039 }
040
041 public AnnotatedModuleLocation(Resource resource, Class moduleClass)
042 {
043 this(resource, moduleClass, null);
044 }
045
046 public Resource getResource()
047 {
048 return _resource;
049 }
050
051 public Method getMethod()
052 {
053 return _method;
054 }
055
056 public Class getModuleClass()
057 {
058 return _moduleClass;
059 }
060
061 public int hashCode()
062 {
063 return ((237 + _resource.hashCode()) + 13 * _moduleClass.hashCode())
064 + (_method != null ? _method.hashCode() : 0);
065 }
066
067 public boolean equals(Object other)
068 {
069 if (!(other instanceof AnnotatedModuleLocation))
070 return false;
071
072 AnnotatedModuleLocation l = (AnnotatedModuleLocation) other;
073
074 if (_moduleClass.equals(l.getModuleClass()))
075 return false;
076
077 if (_method.equals(l.getMethod()))
078 return false;
079
080 return _resource.equals(l.getResource());
081 }
082
083 /**
084 * Returns the position in format "class x, method y"
085 *
086 * @see org.apache.hivemind.Location#getPosition()
087 */
088 public String getPosition()
089 {
090 String result = "Class " + _moduleClass.getName();
091 if (_method != null) {
092 result += ", method " + _method.getName();
093 }
094 return result;
095 }
096
097 public String toString()
098 {
099 return getPosition();
100 }
101
102 }