Skip to content

Commit

Permalink
API for extracting all bundle keys from all c10n enabled interfaces (…
Browse files Browse the repository at this point in the history
…issue #12)

* Use reflections library to scan for interfaces annotated with @C10NMessages,
 and apply bundle key extraction, returning the complete set of all keys.
* Added C10NTools class, as the main entry point to tooling API
* Refactoring
* Added test dependency from tools -> core
  • Loading branch information
rodionmoiseev committed Nov 24, 2012
1 parent 112f37e commit cd56809
Show file tree
Hide file tree
Showing 18 changed files with 441 additions and 117 deletions.
28 changes: 0 additions & 28 deletions core/src/main/java/c10n/DefaultC10NConfigBase.java

This file was deleted.

5 changes: 5 additions & 0 deletions core/src/main/java/c10n/share/utils/BundleKeyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package c10n.share.utils;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -39,6 +40,10 @@ public static Set<C10NBundleKey> allBundleKeys(Class<?>... c10nInterfaces) {
}

public static Set<C10NBundleKey> allBundleKeys(String globalPrefix, Class<?>... c10nInterfaces) {
return allBundleKeys(globalPrefix, Arrays.asList(c10nInterfaces));
}

public static Set<C10NBundleKey> allBundleKeys(String globalPrefix, Iterable<Class<?>> c10nInterfaces) {
Set<C10NBundleKey> res = new HashSet<C10NBundleKey>();
for (Class<?> c10nInterface : c10nInterfaces) {
for (Method method : c10nInterface.getDeclaredMethods()) {
Expand Down
10 changes: 6 additions & 4 deletions tools/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ apply plugin: 'application'

mainClassName = 'c10n.tools.codegen.Main'

dependencies{
compile project(':core')
compile group: "org.reflections", name: "reflections", version: "0.9.8"
compile group: "com.google.code.javaparser", name: "javaparser", version: "1.0.8"
dependencies {
compile project(':core')
compile group: "org.reflections", name: "reflections", version: "0.9.8"
compile group: "com.google.code.javaparser", name: "javaparser", version: "1.0.8"

testCompile project(':core').sourceSets.test.output
}
47 changes: 47 additions & 0 deletions tools/src/main/java/c10n/tools/C10NTools.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package c10n.tools;

import c10n.tools.search.C10NBundleKeySearch;
import c10n.tools.search.SearchModule;

/**
* @author rodion
*/
public final class C10NTools {
/**
* <p>Creates new bundle key search implementation instance.</p>
*
* @return bundle key search module implementation (not null)
*/
public static C10NBundleKeySearch bundleKeySearch() {
return bundleKeySearch("");
}

/**
* <p>Creates new bundle key search implementation instance.</p>
*
* @param keyPrefix global bundle key prefix to use (not null)
* @return bundle key search module implementation (not null)
*/
public static C10NBundleKeySearch bundleKeySearch(String keyPrefix) {
return SearchModule.reflectionsBundleKeySearch(keyPrefix);
}
}
41 changes: 41 additions & 0 deletions tools/src/main/java/c10n/tools/search/C10NBundleKeySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package c10n.tools.search;

import c10n.share.utils.C10NBundleKey;

/**
* <p>Extracts all c10n bundle keys for a given set of
* c10n interface classes.</p>
*
* @author rodion
*/
public interface C10NBundleKeySearch {
/**
* <p>Retrieve all c10n bundle keys for inspection, from the current classpath.
* Only the packages starting with the given package prefix(es) will be included
* in search results.</p>
* <p>Packages will be searched recursively into sub-packages, for each prefix.</p>
*
* @param packagePrefixes prefixes of packages to search (not null)
* @return a collection of all matching bundle keys (not null)
*/
Iterable<C10NBundleKey> findAllKeys(String... packagePrefixes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@
import java.util.Set;

public interface C10NInterfaceSearch {
Set<Class<?>> find(String packagePrefix,
Class<? extends Annotation> annotationClass);
Set<Class<?>> find(Class<? extends Annotation> annotationClass, String... packagePrefixes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package c10n.tools.search;

import c10n.C10NMessages;
import c10n.share.utils.BundleKeyUtils;
import c10n.share.utils.C10NBundleKey;
import com.google.common.collect.Lists;

import java.util.List;
import java.util.Set;

/**
* @author rodion
*/
class DefaultC10NBundleKeySearch implements C10NBundleKeySearch {
private final C10NInterfaceSearch c10NInterfaceSearch;
private final String keyPrefix;

DefaultC10NBundleKeySearch(C10NInterfaceSearch c10NInterfaceSearch, String keyPrefix) {
this.c10NInterfaceSearch = c10NInterfaceSearch;
this.keyPrefix = keyPrefix;
}

@Override
public Iterable<C10NBundleKey> findAllKeys(String... packagePrefixes) {
List<C10NBundleKey> res = Lists.newArrayList();
Set<Class<?>> c10nInterfaces = c10NInterfaceSearch.find(C10NMessages.class, packagePrefixes);
return BundleKeyUtils.allBundleKeys(keyPrefix, c10nInterfaces);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,48 @@

package c10n.tools.search;

import java.lang.annotation.Annotation;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import java.lang.annotation.Annotation;
import java.net.URL;
import java.util.Arrays;
import java.util.Set;

class DefaultC10NInterfaceSearch implements C10NInterfaceSearch {
private final Set<URL> cp;

DefaultC10NInterfaceSearch() {
this(ClasspathHelper.forJavaClassPath());
}

DefaultC10NInterfaceSearch(Set<URL> cp) {
this.cp = cp;
}

@Override
public Set<Class<?>> find(String packagePrefix, Class<? extends Annotation> annotationClass) {
final Predicate<String> filter = new FilterBuilder.Include(
FilterBuilder.prefix(packagePrefix));
Reflections reflections = new Reflections(new ConfigurationBuilder()
.setUrls(cp)
.filterInputsBy(filter)
.setScanners(
new TypeAnnotationsScanner().filterResultsBy(filter),
new SubTypesScanner().filterResultsBy(filter)));
Set<String> types = reflections.getStore().getTypesAnnotatedWith(annotationClass.getName());
URL[] urls = cp.toArray(new URL[cp.size()]);
return ImmutableSet.copyOf(Reflections.forNames(types, new URLClassLoader(urls)));
}
@Override
public Set<Class<?>> find(Class<? extends Annotation> annotationClass, String... packagePrefixes) {
return new Reflections(new ConfigurationBuilder()
.filterInputsBy(getPackageInputFilter())
.setUrls(getPackageURLs(packagePrefixes)))
.getTypesAnnotatedWith(annotationClass);
}


private Set<URL> getPackageURLs(String... packagePrefixes) {
Iterable<URL> packages = Iterables.concat(Iterables.transform(
Arrays.asList(packagePrefixes), new Function<String, Set<URL>>() {
@Override
public Set<URL> apply(String prefix) {
return ClasspathHelper.forPackage(prefix);
}
}));

return Sets.newHashSet(packages);
}

private FilterBuilder getPackageInputFilter(String... packagePrefixes) {
final FilterBuilder inputFilter = new FilterBuilder();

for (String prefix : packagePrefixes) {
inputFilter.include(FilterBuilder.prefix(prefix));
}

return inputFilter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@

package c10n.tools.search;

import java.net.URL;
import java.util.Set;
public final class SearchModule {
public static C10NInterfaceSearch reflectionsSearch() {
return new DefaultC10NInterfaceSearch();
}

public final class C10NInterfacesSearches {
public static C10NInterfaceSearch reflectionsSearch(Set<URL> urls){
return new DefaultC10NInterfaceSearch(urls);
}

public static C10NInterfaceSearch reflectionsSearch(){
return new DefaultC10NInterfaceSearch();
}
/**
* <p>Bundle key search implementation based on the
* <a href="http://code.google.com/p/reflections">Reflections library</a>.</p>
*
* @param keyPrefix global bundle key prefix to use
* @return bundle key search module implementation (not null)
*/
public static C10NBundleKeySearch reflectionsBundleKeySearch(String keyPrefix) {
return new DefaultC10NBundleKeySearch(reflectionsSearch(), keyPrefix);
}
}
34 changes: 34 additions & 0 deletions tools/src/test/java/c10n/tools/AllTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package c10n.tools;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

/**
* @author rodion
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
c10n.AllTests.class,
c10n.tools.search.AllTests.class
})
public class AllTests {
}
34 changes: 34 additions & 0 deletions tools/src/test/java/c10n/tools/search/AllTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package c10n.tools.search;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

/**
* @author rodion
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({
C10NInterfaceSearchTest.class,
DefaultC10NBundleKeySearchTest.class
})
public class AllTests {
}
Loading

0 comments on commit cd56809

Please sign in to comment.