/** * Finds type by exact FQN name. * * @param fqn * Fully qualified class name. * @return Returns type or <code>null</code> if it can not be found. */ public E lookup(String fqn) { int index = findRetrieveIndexFor(fqn); return getAt(index); }
/** * Index one type. * * @param type * Type to index. */ void index(E type) { addOrUpdate(type); }
@Override public ImmutableType call() throws Exception { return hashIndexer.lookup(hash); } });
@Test public void sameFqnTwice() { ClassType stringType1 = new ClassType(String.class.getName()); ClassType stringType2 = new ClassType(String.class.getName()); indexer.index(stringType1); indexer.index(stringType2); ClassType lookup = indexer.lookup(String.class.getName()); assertThat(indexer, hasSize(1)); assertThat(lookup == stringType2, is(true)); }
/** * Finds all {@link NonPrimitiveType}s that start with given string. * * @param fqnWildCard * String that class should start with. * @return All types starting with the given string. */ public Collection<E> findStartsWith(String fqnWildCard) { long minMaxIndex = findStartsWithMinMaxIndexes(fqnWildCard); int min = getLowerInt(minMaxIndex); int max = getUpperInt(minMaxIndex); if (min < 0) { return Collections.emptyList(); } int size = size(); List<E> results = new ArrayList<>((max - min) + 1); for (int i = min; (i <= max) && (max < size); i++) { results.add(getAt(i)); } return results; }
@Test public void findAll() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(objectType); Collection<ClassType> types = indexer.findAll(); assertThat(types, hasSize(3)); assertThat(types, hasItem(stringType)); assertThat(types, hasItem(objectType)); assertThat(types, hasItem(thisType)); } }
@Test public void notFound() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(new ClassType("a")); indexer.index(new ClassType("java.nolang")); indexer.index(objectType); // middle Collection<ClassType> results = indexer.findStartsWith("aa"); assertThat(results, is(empty())); results = indexer.findStartsWith("java.lang.something"); assertThat(results, is(empty())); results = indexer.findStartsWith("ww"); assertThat(results, is(empty())); } }
/** * Finds index for a FQN to retrieve. * * @param fqn * String representing the FQN. * @return Index where element should be retrieved from or negative value if one can not be * located. */ private int findRetrieveIndexFor(String fqn) { int size = size(); int min = 0; int max = size - 1; while (max >= min) { int mid = midpoint(min, max); int compare = getAt(mid).getFQN().compareTo(fqn); // if no difference then we have it if (0 == compare) { return mid; } // otherwise adapt min and max min = (compare < 0) ? mid + 1 : min; max = (compare > 0) ? mid - 1 : max; } return -1; }
@Override public Collection<Type> call() throws Exception { return fqnIndexer.findAll(); } });
@Override public Collection<Type> call() throws Exception { return fqnIndexer.findByPattern(pattern); } });
@Override public ImmutableType call() throws Exception { return fqnIndexer.lookup(fqn); } });
@Test public void notFound() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(objectType); ClassType lookup = indexer.lookup("nonExistingType"); assertThat(indexer, hasSize(3)); assertThat(lookup, is(nullValue())); }
@Override public Collection<ImmutableType> call() throws Exception { Collection<ImmutableType> result = new ArrayList<>(); for (ImmutableType type : fqnIndexer.findAll()) { if (type.isClass() && type.isInitialized()) { if (type.castToClass().hasInstrumentationPoints()) { result.add(type); } } } return result; } });
@Test public void equalsPatternNonExistingElement() { EqualsMatchPattern equalsMatchPattern = new EqualsMatchPattern("whatever"); Collection<ClassType> results = indexer.findByPattern(equalsMatchPattern); assertThat(results, is(empty())); } }
@Test public void find() throws Exception { String fqn = "fqn"; when(fqnIndexer.lookup(fqn)).thenReturn(type); ImmutableType type = lookup.findByFQN(fqn); assertThat((Type) type, is(type)); verify(fqnIndexer, times(1)).lookup(fqn); verify(classCache, times(1)).executeWithReadLock(Matchers.<Callable<?>> anyObject()); verifyNoMoreInteractions(fqnIndexer, classCache); verifyZeroInteractions(hashIndexer); } }
@Test public void replace() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); ClassType secondObjectType = new ClassType(Object.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(objectType); indexer.index(secondObjectType); ClassType lookup = indexer.lookup(Object.class.getName()); assertThat(indexer, hasSize(3)); assertThat(System.identityHashCode(lookup), is(System.identityHashCode(secondObjectType))); } }
@Test @SuppressWarnings("unchecked") public void noClassesLoaded() throws Exception { Collection<? extends ImmutableType> result = lookup.findInstrumentedTypes(); verify(classCache).executeWithReadLock(any(Callable.class)); verify(fqnIndexer).findAll(); verifyNoMoreInteractions(fqnIndexer, classCache); verifyZeroInteractions(hashIndexer); assertThat(result, hasSize(0)); }
@Test public void lookup() { ClassType stringType = new ClassType(String.class.getName()); ClassType objectType = new ClassType(Object.class.getName()); ClassType thisType = new ClassType(FqnIndexer.class.getName()); indexer.index(stringType); indexer.index(thisType); indexer.index(new ClassType("a")); indexer.index(new ClassType("java.nolang")); indexer.index(objectType); ClassType stringLookup = indexer.lookup(stringType.getFQN()); ClassType objectLookup = indexer.lookup(objectType.getFQN()); ClassType thisTypeLookup = indexer.lookup(thisType.getFQN()); assertThat(indexer, hasSize(5)); assertThat(stringLookup, is(stringType)); assertThat(objectLookup, is(objectType)); assertThat(thisTypeLookup, is(thisType)); }