Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
7525eb31
Commit
7525eb31
authored
Aug 09, 2013
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify OnClassCondition
Merge OnMissingClassCondition into OnClassCondition and simplify code.
parent
cf655945
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
78 additions
and
108 deletions
+78
-108
ConditionalOnMissingClass.java
...ot/autoconfigure/condition/ConditionalOnMissingClass.java
+1
-1
OnClassCondition.java
...mework/boot/autoconfigure/condition/OnClassCondition.java
+76
-24
OnMissingClassCondition.java
...boot/autoconfigure/condition/OnMissingClassCondition.java
+0
-82
OnMissingClassConditionTests.java
...autoconfigure/condition/OnMissingClassConditionTests.java
+1
-1
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingClass.java
View file @
7525eb31
...
...
@@ -32,7 +32,7 @@ import org.springframework.context.annotation.Conditional;
@Target
({
ElementType
.
TYPE
,
ElementType
.
METHOD
})
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
@Conditional
(
On
Missing
ClassCondition
.
class
)
@Conditional
(
OnClassCondition
.
class
)
public
@interface
ConditionalOnMissingClass
{
/**
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnClassCondition.java
View file @
7525eb31
...
...
@@ -16,7 +16,8 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
condition
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.LinkedList
;
import
java.util.List
;
import
org.apache.commons.logging.Log
;
...
...
@@ -24,15 +25,16 @@ import org.apache.commons.logging.LogFactory;
import
org.springframework.context.annotation.Condition
;
import
org.springframework.context.annotation.ConditionContext
;
import
org.springframework.core.type.AnnotatedTypeMetadata
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.StringUtils
;
/**
* {@link Condition} that checks for the specific classes.
* {@link Condition} that checks for the
presence or absence of
specific classes.
*
* @author Phillip Webb
* @see ConditionalOnClass
* @see ConditionalOnMissingClass
*/
class
OnClassCondition
implements
Condition
{
...
...
@@ -43,40 +45,90 @@ class OnClassCondition implements Condition {
String
checking
=
ConditionLogUtils
.
getPrefix
(
logger
,
metadata
);
MultiValueMap
<
String
,
Object
>
attributes
=
metadata
.
getAllAnnotationAttributes
(
ConditionalOnClass
.
class
.
getName
(),
true
);
if
(
attributes
!=
null
)
{
List
<
String
>
classNames
=
new
ArrayList
<
String
>();
collectClassNames
(
classNames
,
attributes
.
get
(
"value"
));
collectClassNames
(
classNames
,
attributes
.
get
(
"name"
));
Assert
.
isTrue
(
classNames
.
size
()
>
0
,
"@ConditionalOnClass annotations must specify at least one class value"
);
for
(
String
className
:
classNames
)
{
MultiValueMap
<
String
,
Object
>
onClasses
=
getAttributes
(
metadata
,
ConditionalOnClass
.
class
);
if
(
onClasses
!=
null
)
{
List
<
String
>
missing
=
getMatchingClasses
(
onClasses
,
MatchType
.
MISSING
,
context
);
if
(!
missing
.
isEmpty
())
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
checking
+
"Looking for class: "
+
className
);
logger
.
debug
(
checking
+
"Required @ConditionalOnClass classes not found: "
+
StringUtils
.
collectionToCommaDelimitedString
(
missing
)
+
" (search terminated with matches=false)"
);
}
if
(!
ClassUtils
.
isPresent
(
className
,
context
.
getClassLoader
()))
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
checking
+
"Class not found: "
+
className
+
" (search terminated with matches=false)"
);
}
return
false
;
return
false
;
}
}
MultiValueMap
<
String
,
Object
>
onMissingClasses
=
getAttributes
(
metadata
,
ConditionalOnMissingClass
.
class
);
if
(
onMissingClasses
!=
null
)
{
List
<
String
>
present
=
getMatchingClasses
(
onMissingClasses
,
MatchType
.
PRESENT
,
context
);
if
(!
present
.
isEmpty
())
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
checking
+
"Required @ConditionalOnMissing classes found: "
+
StringUtils
.
collectionToCommaDelimitedString
(
present
)
+
" (search terminated with matches=false)"
);
}
return
false
;
}
}
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
checking
+
"Match result is: true"
);
}
return
true
;
}
private
void
collectClassNames
(
List
<
String
>
classNames
,
List
<
Object
>
values
)
{
for
(
Object
value
:
values
)
{
for
(
Object
valueItem
:
(
Object
[])
value
)
{
classNames
.
add
(
valueItem
instanceof
Class
<?>
?
((
Class
<?>)
valueItem
)
.
getName
()
:
valueItem
.
toString
());
private
MultiValueMap
<
String
,
Object
>
getAttributes
(
AnnotatedTypeMetadata
metadata
,
Class
<?>
annotationType
)
{
return
metadata
.
getAllAnnotationAttributes
(
annotationType
.
getName
(),
true
);
}
private
List
<
String
>
getMatchingClasses
(
MultiValueMap
<
String
,
Object
>
attributes
,
MatchType
matchType
,
ConditionContext
context
)
{
List
<
String
>
matches
=
new
LinkedList
<
String
>();
addAll
(
matches
,
attributes
.
get
(
"value"
));
addAll
(
matches
,
attributes
.
get
(
"name"
));
Iterator
<
String
>
iterator
=
matches
.
iterator
();
while
(
iterator
.
hasNext
())
{
if
(!
matchType
.
matches
(
iterator
.
next
(),
context
))
{
iterator
.
remove
();
}
}
return
matches
;
}
private
void
addAll
(
List
<
String
>
list
,
List
<
Object
>
itemsToAdd
)
{
if
(
itemsToAdd
!=
null
)
{
for
(
Object
item
:
itemsToAdd
)
{
for
(
String
arrayItem
:
(
String
[])
item
)
{
list
.
add
(
arrayItem
.
toString
());
}
}
}
}
private
static
enum
MatchType
{
PRESENT
{
@Override
public
boolean
matches
(
String
className
,
ConditionContext
context
)
{
return
ClassUtils
.
isPresent
(
className
,
context
.
getClassLoader
());
}
},
MISSING
{
@Override
public
boolean
matches
(
String
className
,
ConditionContext
context
)
{
return
!
ClassUtils
.
isPresent
(
className
,
context
.
getClassLoader
());
}
};
public
abstract
boolean
matches
(
String
className
,
ConditionContext
context
);
};
}
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnMissingClassCondition.java
deleted
100644 → 0
View file @
cf655945
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed 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
org
.
springframework
.
boot
.
autoconfigure
.
condition
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.context.annotation.Condition
;
import
org.springframework.context.annotation.ConditionContext
;
import
org.springframework.core.type.AnnotatedTypeMetadata
;
import
org.springframework.util.Assert
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.MultiValueMap
;
/**
* {@link Condition} that checks for the specific classes.
*
* @author Dave Syer
* @see ConditionalOnMissingClass
*/
class
OnMissingClassCondition
implements
Condition
{
private
static
Log
logger
=
LogFactory
.
getLog
(
OnMissingClassCondition
.
class
);
@Override
public
boolean
matches
(
ConditionContext
context
,
AnnotatedTypeMetadata
metadata
)
{
String
checking
=
ConditionLogUtils
.
getPrefix
(
logger
,
metadata
);
MultiValueMap
<
String
,
Object
>
attributes
=
metadata
.
getAllAnnotationAttributes
(
ConditionalOnMissingClass
.
class
.
getName
(),
true
);
if
(
attributes
!=
null
)
{
List
<
String
>
classNames
=
new
ArrayList
<
String
>();
collectClassNames
(
classNames
,
attributes
.
get
(
"value"
));
Assert
.
isTrue
(
classNames
.
size
()
>
0
,
"@ConditionalOnMissingClass annotations must specify at least one class value"
);
for
(
String
className
:
classNames
)
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
checking
+
"Looking for class: "
+
className
);
}
if
(
ClassUtils
.
isPresent
(
className
,
context
.
getClassLoader
()))
{
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
checking
+
"Found class: "
+
className
+
" (search terminated with matches=false)"
);
}
return
false
;
}
}
}
if
(
logger
.
isDebugEnabled
())
{
logger
.
debug
(
checking
+
"Match result is: true"
);
}
return
true
;
}
private
void
collectClassNames
(
List
<
String
>
classNames
,
List
<
Object
>
values
)
{
for
(
Object
value
:
values
)
{
for
(
Object
valueItem
:
(
Object
[])
value
)
{
classNames
.
add
(
valueItem
instanceof
Class
<?>
?
((
Class
<?>)
valueItem
)
.
getName
()
:
valueItem
.
toString
());
}
}
}
// FIXME merge with OnClassCondition
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/OnMissingClassConditionTests.java
View file @
7525eb31
...
...
@@ -26,7 +26,7 @@ import static org.junit.Assert.assertFalse;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
* Tests for {@link
OnMissingClassCondition
}.
* Tests for {@link
ConditionalOnMissingClass
}.
*
* @author Dave Syer
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment