| 1 |
|
package net.sf.sapjcosupport; |
| 2 |
|
|
| 3 |
|
import org.apache.commons.lang.NotImplementedException; |
| 4 |
|
import org.apache.oro.text.regex.MalformedPatternException; |
| 5 |
|
import org.apache.oro.text.regex.Pattern; |
| 6 |
|
import org.apache.oro.text.regex.Perl5Compiler; |
| 7 |
|
import org.apache.oro.text.regex.Perl5Matcher; |
| 8 |
|
import org.apache.log4j.Logger; |
| 9 |
|
import org.springframework.core.NestedRuntimeException; |
| 10 |
|
|
| 11 |
|
import java.lang.reflect.Field; |
| 12 |
|
import java.util.ArrayList; |
| 13 |
|
import java.util.Iterator; |
| 14 |
|
import java.util.NoSuchElementException; |
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
public class ReflectionFieldIterator implements Iterator { |
| 27 |
|
private Object instance; |
| 28 |
|
private int counter; |
| 29 |
|
private Field[] fields; |
| 30 |
|
private Logger logger; |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
public ReflectionFieldIterator(Object instance) { |
| 39 |
12 |
this(instance, null); |
| 40 |
12 |
} |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
24 |
public ReflectionFieldIterator(Object instance, String regularExpression) { |
| 56 |
24 |
logger = Logger.getLogger(getClass()); |
| 57 |
24 |
this.instance = instance; |
| 58 |
24 |
if (instance != null) { |
| 59 |
12 |
fields = instance.getClass().getFields(); |
| 60 |
12 |
if (regularExpression != null) { |
| 61 |
6 |
filterFieldNames(regularExpression); |
| 62 |
|
} |
| 63 |
|
} else { |
| 64 |
12 |
if (logger.isDebugEnabled()) { |
| 65 |
0 |
logger.debug("The instance that was passed to the iterator is null."); |
| 66 |
|
} |
| 67 |
12 |
fields = null; |
| 68 |
|
} |
| 69 |
24 |
} |
| 70 |
|
|
| 71 |
|
private void filterFieldNames(String regularExpression) { |
| 72 |
6 |
if (fields == null) { |
| 73 |
0 |
return; |
| 74 |
|
} |
| 75 |
|
Pattern pattern; |
| 76 |
|
try { |
| 77 |
6 |
pattern = new Perl5Compiler().compile(regularExpression); |
| 78 |
0 |
} catch (MalformedPatternException e) { |
| 79 |
0 |
if (logger.isInfoEnabled()) { |
| 80 |
0 |
logger.info("Regular expression did not compile to a pattern; all fields will be considered.", e); |
| 81 |
|
} |
| 82 |
0 |
return; |
| 83 |
6 |
} |
| 84 |
6 |
Perl5Matcher matcher = new Perl5Matcher(); |
| 85 |
6 |
ArrayList filteredFieldList = new ArrayList(fields.length); |
| 86 |
6 |
for (int i = 0; i < fields.length; i++) { |
| 87 |
0 |
if (matcher.matches(fields[i].getName(), pattern)) { |
| 88 |
0 |
filteredFieldList.add(fields[i]); |
| 89 |
|
} |
| 90 |
|
} |
| 91 |
6 |
fields = (Field[]) filteredFieldList.toArray(new Field[filteredFieldList.size()]); |
| 92 |
6 |
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
public boolean hasNext() { |
| 100 |
8 |
if (fields == null) { |
| 101 |
2 |
return false; |
| 102 |
|
} |
| 103 |
6 |
return counter < fields.length; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
public Object next() { |
| 113 |
2 |
if (!hasNext()) { |
| 114 |
2 |
throw new NoSuchElementException("No more elements available in iterator"); |
| 115 |
|
} |
| 116 |
|
try { |
| 117 |
0 |
fields[++counter].setAccessible(true); |
| 118 |
0 |
return fields[counter].get(instance); |
| 119 |
0 |
} catch (IllegalAccessException e) { |
| 120 |
0 |
throw new IllegalAccessRuntimeException(e); |
| 121 |
|
} |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
|
| 125 |
|
|
| 126 |
|
|
| 127 |
|
public void remove() { |
| 128 |
0 |
throw new NotImplementedException("This iterator implementation does not support item removal"); |
| 129 |
|
} |
| 130 |
|
|
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
public class IllegalAccessRuntimeException extends RuntimeWrapperException { |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
0 |
public IllegalAccessRuntimeException(final IllegalAccessException exception) { |
| 144 |
0 |
super("Could not access field through reflection", exception); |
| 145 |
0 |
} |
| 146 |
|
} |
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| 151 |
|
public class RuntimeWrapperException extends NestedRuntimeException { |
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
0 |
public RuntimeWrapperException(Throwable throwable) { |
| 159 |
0 |
super(throwable.getMessage(), throwable); |
| 160 |
0 |
} |
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
0 |
public RuntimeWrapperException(String message, Throwable throwable) { |
| 169 |
0 |
super(message, throwable); |
| 170 |
0 |
} |
| 171 |
|
} |
| 172 |
|
} |