Sitelet https://github.com/KrailOrg/q3c-util/commit/1ca84110d596349b2957bb20cf8653bd9121e95e
Skip to content

Commit 1ca8411

Browse files
author
David Sowerby
committed
Initial export from Krail
1 parent 2c56133 commit 1ca8411

31 files changed

Lines changed: 2867 additions & 0 deletions

‎.gitignore‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
3+
.classpath
4+
.idea
5+
.project
6+
build/*
7+
out
8+
classes
9+
.gradle/
10+
11+
12+
13+
out.txt
14+
15+
*.iml
16+
*.ipr
17+
*.iws
18+

‎README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# q3c-util
2+
3+
A fairly random collection of utilities, used primarily in [Krail](https://github.com/davidsowerby/krail), but has found uses in other places.
4+
5+
It contains the following packages:
6+
7+
8+
## clazz
9+
10+
A mix of classes which identify the unenhanced class from a byte enhanced object
11+
12+
## dag
13+
14+
- A dynamic Directed Acyclic Graph (dynamic in the sense that it checks for cycles as items are added)
15+
16+
## forest
17+
18+
- A semantic wrapper over a [Jung graph](http://jung.sourceforge.net/), providing Tree like syntax for manipulation
19+
20+
## guava
21+
22+
- A Guava Cache configuration object
23+
24+
## reflect
25+
26+
- Ignore - just one old class which has been deprecated

‎build.gradle‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
buildscript {
2+
ext.kotlin_version = '1.1.3-2'
3+
ext.kaytee_plugin_version = "0.16.0.0"
4+
5+
repositories {
6+
mavenLocal()
7+
jcenter()
8+
mavenCentral()
9+
}
10+
11+
dependencies {
12+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13+
classpath "uk.q3c.kaytee:kaytee-plugin:$kaytee_plugin_version"
14+
}
15+
}
16+
17+
apply plugin: 'kotlin'
18+
apply plugin: 'uk.q3c.kaytee'
19+
20+
group='uk.q3c.util'
21+
22+
kaytee {
23+
baseVersion = "0.8.0.0"
24+
}
25+
26+
bintray {
27+
user = 'dsowerby'
28+
}
29+
30+
compileKotlin {
31+
kotlinOptions.jvmTarget = "1.8"
32+
}
33+
34+
ext.assertjVersion = '3.3.0'
35+
ext.commonsLangVersion = '3.3.2'
36+
ext.guavaVersion = '23.0'
37+
ext.slf4jVersion = '1.7.6'
38+
39+
repositories {
40+
jcenter()
41+
mavenCentral()
42+
}
43+
44+
dependencies {
45+
//logging
46+
compile 'org.slf4j:slf4j-api:' + slf4jVersion
47+
compile 'ch.qos.logback:logback-classic:1.1.2'
48+
compile 'ch.qos.logback:logback-core:1.1.2'
49+
compile 'org.apache.commons:commons-lang3:' + commonsLangVersion
50+
//the easiest way to all the Jung parts
51+
compile 'net.sf.jung:jung-samples:2.0.1'
52+
compile 'com.google.code.findbugs:annotations:3.0.0'
53+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
54+
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
55+
compile 'com.google.guava:guava:' + guavaVersion
56+
compile 'org.apache.commons:commons-collections4:4.1'
57+
58+
testCompile 'org.assertj:assertj-core:' + assertjVersion
59+
testCompile 'org.assertj:assertj-guava:1.3.1'
60+
compile 'org.mockito:mockito-all:1.10.19'
61+
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
62+
testCompile 'cglib:cglib-nodep:3.2.0'
63+
testCompile 'org.objenesis:objenesis:2.2'
64+
}
65+
sourceCompatibility = '1.8'
66+
apply plugin: 'kotlin'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
*
3+
* * Copyright (c) 2016. David Sowerby
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6+
* * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* *
8+
* * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
* * specific language governing permissions and limitations under the License.
11+
*
12+
*/
13+
14+
package uk.q3c.util.clazz;
15+
16+
/**
17+
* Created by David Sowerby on 03 Jan 2016
18+
*/
19+
public interface ClassNameUtils {
20+
21+
/**
22+
* Returns a "pure" class name, ignoring any appended enhancement
23+
*
24+
* @param clazz the class to return the simple name of
25+
* @return a "pure" class name, ignoring any appended enhancement
26+
*/
27+
28+
String simpleClassNameEnhanceRemoved(Class<?> clazz);
29+
30+
/**
31+
* Returns the underlying class (effectively removing enhancement by Guice), often needed to identify annotations
32+
*
33+
* @param clazz the original, possibly enhanced class
34+
*/
35+
36+
Class<?> classWithEnhanceRemoved(Class<?> clazz);
37+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
*
3+
* * Copyright (c) 2016. David Sowerby
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6+
* * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* *
8+
* * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
* * specific language governing permissions and limitations under the License.
11+
*
12+
*/
13+
package uk.q3c.util.clazz;
14+
15+
import javax.annotation.Nonnull;
16+
17+
import static com.google.common.base.Preconditions.*;
18+
19+
public class DefaultClassNameUtils implements ClassNameUtils {
20+
21+
@Override
22+
@Nonnull
23+
public String simpleClassNameEnhanceRemoved(@Nonnull Class<?> clazz) {
24+
checkNotNull(clazz);
25+
String simpleName = clazz.getSimpleName();
26+
if (simpleName.contains("$$Enhancer")) {
27+
simpleName = simpleName.substring(0, simpleName.indexOf("$$Enhancer"));
28+
29+
// could be an inner class
30+
if (simpleName.contains("$")) {
31+
int index = simpleName.indexOf('$');
32+
simpleName = simpleName.substring(index + 1, simpleName.length());
33+
}
34+
35+
}
36+
37+
return simpleName;
38+
}
39+
40+
41+
@Override
42+
@Nonnull
43+
public Class<?> classWithEnhanceRemoved(@Nonnull Class<?> clazz) {
44+
checkNotNull(clazz);
45+
while (clazz.getName()
46+
.contains("$$EnhancerByGuice")) {
47+
clazz = clazz.getSuperclass();
48+
}
49+
return clazz;
50+
}
51+
52+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
*
3+
* * Copyright (c) 2016. David Sowerby
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6+
* * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* *
8+
* * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
* * specific language governing permissions and limitations under the License.
11+
*
12+
*/
13+
14+
package uk.q3c.util.clazz;
15+
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
19+
/**
20+
* This implementation of {@link UnenhancedClassIdentifier} identifies classes enhanced by Guice AOP and returns the origin, un-enhanced class.
21+
* <p>
22+
* Created by David Sowerby on 10/05/15.
23+
*/
24+
public class DefaultUnenhancedClassIdentifier implements UnenhancedClassIdentifier {
25+
private static Logger log = LoggerFactory.getLogger(DefaultUnenhancedClassIdentifier.class);
26+
27+
@Override
28+
public Class<?> getOriginalClassFor(Object target) {
29+
Class clazz = target.getClass();
30+
if (clazz.getName()
31+
.contains("$$EnhancerByGuice$$")) {
32+
log.debug("Guice AOP enhanced class detected, using superclass to read annotations");
33+
return clazz.getSuperclass();
34+
} else {
35+
if (clazz.getName()
36+
.contains("$$")) {
37+
log.warn("The I18NProcessor has been invoked on a proxy object or object which has been enhanced by something other than Guice AOP. This " +
38+
"means" +
39+
" it will not be able to detect any annotated fields.\n" + "" +
40+
"Possible solutions are either to separate the annotated fields from the AOP intercepted methods, perhaps by injecting a worker " +
41+
"object,\n" +
42+
" or to directly code the equivalent of the AOP interception.\n" +
43+
"Better still, you may be able to provide a different implementation of this class, which knows how to identify the original, " +
44+
"un-enhanced, class");
45+
}
46+
}
47+
return clazz;
48+
}
49+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
*
3+
* * Copyright (c) 2016. David Sowerby
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6+
* * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* *
8+
* * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
* * specific language governing permissions and limitations under the License.
11+
*
12+
*/
13+
14+
package uk.q3c.util.clazz;
15+
16+
/**
17+
* Returns the un-enhanced original class from a byte enhanced instance. Often needed when the instance is subject to reflection to identify field / class annotations.
18+
* <p>
19+
* If {@code target} is not enhanced, returns target.getClass()
20+
* <p>
21+
* <p>
22+
* Created by David Sowerby on 10/05/15.
23+
*/
24+
public interface UnenhancedClassIdentifier {
25+
Class<?> getOriginalClassFor(Object target);
26+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
*
3+
* * Copyright (c) 2016. David Sowerby
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6+
* * the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* *
8+
* * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
* * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
* * specific language governing permissions and limitations under the License.
11+
*
12+
*/
13+
14+
package uk.q3c.util.collection;
15+
16+
import com.google.common.collect.ImmutableList;
17+
import org.apache.commons.collections4.ListUtils;
18+
19+
import java.io.Serializable;
20+
import java.lang.annotation.Annotation;
21+
import java.util.List;
22+
import java.util.Set;
23+
24+
25+
/**
26+
* The syntax of getting the Generics right to create a list of Class<? extends Annotation> is clumsy and prone to errors - this class just makes it clearer and
27+
* simpler
28+
* <p>
29+
* Created by David Sowerby on 07/08/15.
30+
*/
31+
public class AnnotationList implements Serializable {
32+
33+
protected final ImmutableList<Class<? extends Annotation>> list;
34+
35+
/**
36+
* Construct with List containing required values - may be empty
37+
*
38+
* @param list containing required values - may be empty
39+
*/
40+
public AnnotationList(List<Class<? extends Annotation>> list) {
41+
super();
42+
this.list = ImmutableList.copyOf(list);
43+
}
44+
45+
/**
46+
* Construct with elements containing required values - may be empty
47+
*
48+
* @param elements containing required values - may be empty
49+
*/
50+
@SafeVarargs
51+
public AnnotationList(Class<? extends Annotation>... elements) {
52+
super();
53+
this.list = (elements == null) ? ImmutableList.of() : ImmutableList.copyOf(elements);
54+
}
55+
56+
public AnnotationList(Set<? extends Class<? extends Annotation>> set) {
57+
this.list = ImmutableList.copyOf(set);
58+
}
59+
60+
61+
public ImmutableList<Class<? extends Annotation>> getList() {
62+
return list;
63+
}
64+
65+
public int size() {
66+
return list.size();
67+
}
68+
69+
70+
public boolean isEmpty() {
71+
return list.isEmpty();
72+
}
73+
74+
@Override
75+
public boolean equals(Object o) {
76+
if (this == o) return true;
77+
if (o == null || getClass() != o.getClass()) return false;
78+
79+
AnnotationList that = (AnnotationList) o;
80+
81+
return ListUtils.isEqualList(this.list, that.list);
82+
}
83+
84+
@Override
85+
public int hashCode() {
86+
return list.hashCode();
87+
}
88+
}

0 commit comments

Comments
 (0)