Skip to content

Commit c794768

Browse files
authored
Merge pull request #429 from wttech/fix-code-smells
fixed some code smells
2 parents 6afd12a + cfbdf0a commit c794768

File tree

15 files changed

+29
-129
lines changed

15 files changed

+29
-129
lines changed

app/aem/actions.checks/src/main/java/com/cognifide/apm/checks/actions/exists/CheckAuthorizableExists.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public ActionResult execute(final Context context) {
5656
public ActionResult process(final Context context, boolean execute) {
5757
ActionResult actionResult = context.createActionResult();
5858
try {
59-
Authorizable authorizable = null;
59+
Authorizable authorizable;
6060
if (shouldBeGroup) {
6161
authorizable = context.getAuthorizableManager().getGroupIfExists(id);
6262
} else {

app/aem/actions.checks/src/main/java/com/cognifide/apm/checks/utils/ActionUtils.java

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,14 @@
2020
package com.cognifide.apm.checks.utils;
2121

2222
import com.cognifide.apm.api.actions.ActionResult;
23-
import com.cognifide.apm.api.exceptions.ActionExecutionException;
24-
import java.util.Iterator;
2523
import java.util.List;
26-
import javax.jcr.RepositoryException;
27-
import org.apache.jackrabbit.api.security.user.Authorizable;
28-
import org.apache.jackrabbit.api.security.user.Group;
2924

3025
public final class ActionUtils {
3126

3227
public static final String ASSERTION_FAILED_MSG = "Assertion failed";
3328

3429
private ActionUtils() {
35-
}
36-
37-
/**
38-
* Adding group to another group may result in cyclic relation. Let current group be the group where we
39-
* want to add current authorizable to. If current authorizable contains group such that current group
40-
* belongs to, then we prevent such operation.
41-
*
42-
* @param currentGroup The group where we want to add current authorizable
43-
* @param groupToBeAdded Authorizable we want to add
44-
* @throws ActionExecutionException Throw exception, if adding operation results in cyclic relation
45-
*/
46-
public static void checkCyclicRelations(Group currentGroup, Group groupToBeAdded)
47-
throws ActionExecutionException {
48-
try {
49-
if (groupToBeAdded.getID().equals(currentGroup.getID())) {
50-
throw new ActionExecutionException(MessagingUtils.addingGroupToItself(currentGroup.getID()));
51-
}
52-
Iterator<Group> parents = currentGroup.memberOf();
53-
while (parents.hasNext()) {
54-
Group currentParent = parents.next();
55-
// Is added group among my parents?
56-
if (currentParent.getID().equals(groupToBeAdded.getID())) {
57-
throw new ActionExecutionException(MessagingUtils.cyclicRelationsForbidden(
58-
currentGroup.getID(), groupToBeAdded.getID()));
59-
}
60-
// ... and are its children among my parents?
61-
for (Iterator<Authorizable> children = groupToBeAdded.getMembers(); children.hasNext(); ) {
62-
Authorizable currentChild = children.next();
63-
if (currentParent.getID().equals(currentChild.getID())) {
64-
throw new ActionExecutionException(MessagingUtils.cyclicRelationsForbidden(
65-
currentChild.getID(), groupToBeAdded.getID()));
66-
}
67-
}
68-
}
69-
} catch (RepositoryException e) {
70-
throw new ActionExecutionException(MessagingUtils.createMessage(e));
71-
}
30+
// intentionally empty
7231
}
7332

7433
public static void logErrors(List<String> errors, ActionResult actionResult) {

app/aem/actions.checks/src/main/java/com/cognifide/apm/checks/utils/MessagingUtils.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,7 @@ public static String createMessage(Exception e) {
3131
return StringUtils.isBlank(e.getMessage()) ? "Internal error: " + e.getClass() : e.getMessage();
3232
}
3333

34-
public static String addingGroupToItself(String groupId) {
35-
return "You can not add group " + groupId + " to itself";
36-
}
37-
3834
public static String authorizableNotExists(String authorizableId) {
3935
return "Authorizable with id: " + authorizableId + " does not exists";
4036
}
41-
42-
public static String cyclicRelationsForbidden(String currentGroup, String groupToBeAdded) {
43-
return "Cannot add group " + groupToBeAdded + " to group " + currentGroup
44-
+ " due to resulting cyclic relation";
45-
}
4637
}

app/aem/actions.main/src/main/java/com/cognifide/apm/main/permissions/PermissionActionHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private void addEntry(boolean allow, final List<Privilege> privileges,
8181

8282
Map<String, Value> singleValueRestrictions = restrictions.getSingleValueRestrictions(valueFactory);
8383
Map<String, Value[]> multiValueRestrictions = restrictions.getMultiValueRestrictions(valueFactory);
84-
jackrabbitAcl.addEntry(principal, privileges.toArray(new Privilege[privileges.size()]), allow,
84+
jackrabbitAcl.addEntry(principal, privileges.toArray(new Privilege[]{}), allow,
8585
singleValueRestrictions, multiValueRestrictions);
8686
}
8787

app/aem/actions.main/src/main/java/com/cognifide/apm/main/utils/ActionUtils.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919
*/
2020
package com.cognifide.apm.main.utils;
2121

22-
import com.cognifide.apm.api.actions.ActionResult;
2322
import com.cognifide.apm.api.exceptions.ActionExecutionException;
2423
import java.util.Iterator;
25-
import java.util.List;
2624
import javax.jcr.RepositoryException;
2725
import org.apache.jackrabbit.api.security.user.Authorizable;
2826
import org.apache.jackrabbit.api.security.user.Group;
@@ -32,6 +30,7 @@ public final class ActionUtils {
3230
public static final String EXECUTION_INTERRUPTED_MSG = "Execution interrupted";
3331

3432
private ActionUtils() {
33+
// intentionally empty
3534
}
3635

3736
/**
@@ -70,10 +69,4 @@ public static void checkCyclicRelations(Group currentGroup, Group groupToBeAdded
7069
throw new ActionExecutionException(MessagingUtils.createMessage(e));
7170
}
7271
}
73-
74-
public static void logErrors(List<String> errors, ActionResult actionResult) {
75-
for (String error : errors) {
76-
actionResult.logError(error);
77-
}
78-
}
7972
}

app/aem/api/src/main/java/com/cognifide/apm/api/actions/Message.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public Message(String text, String type) {
3232
this.type = type;
3333
}
3434

35-
// Gson library needs this
3635
private Message() {
36+
// GSON library needs
3737
}
3838

3939
public String getText() {

app/aem/api/src/main/java/com/cognifide/apm/api/exceptions/InitializationException.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

app/aem/core/src/main/java/com/cognifide/apm/core/actions/ActionFactoryImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.cognifide.apm.core.Property;
2525
import com.cognifide.apm.core.grammar.argument.Arguments;
2626
import java.util.ArrayList;
27-
import java.util.Collections;
2827
import java.util.Comparator;
2928
import java.util.List;
3029
import java.util.Optional;
@@ -82,7 +81,7 @@ public List<CommandDescription> getCommandDescriptions() {
8281
}
8382

8483
private void sortReferences(List<CommandDescription> references) {
85-
Collections.sort(references, Comparator.comparing(CommandDescription::getGroup, (o1, o2) -> {
84+
references.sort(Comparator.comparing(CommandDescription::getGroup, (o1, o2) -> {
8685
if (CORE_GROUP.equals(o1) && CORE_GROUP.equals(o2)) {
8786
return 0;
8887
}

app/aem/core/src/main/java/com/cognifide/apm/core/actions/ActionMapperRegistryImpl.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
import com.cognifide.apm.main.services.ApmActionsMainService;
2929
import com.google.common.collect.ImmutableMap;
3030
import com.google.common.collect.Maps;
31+
import java.util.ArrayList;
3132
import java.util.Collection;
3233
import java.util.Collections;
3334
import java.util.List;
3435
import java.util.Map;
3536
import java.util.Optional;
3637
import java.util.concurrent.atomic.AtomicReference;
37-
import java.util.stream.Collectors;
3838
import org.osgi.service.component.ComponentContext;
3939
import org.osgi.service.component.annotations.Activate;
4040
import org.osgi.service.component.annotations.Component;
@@ -92,15 +92,13 @@ public Optional<MapperDescriptor> getMapper(String name) {
9292

9393
@Override
9494
public Collection<MapperDescriptor> getMappers() {
95-
return mappers.get().values()
96-
.stream()
97-
.collect(Collectors.toList());
95+
return new ArrayList<>(mappers.get().values());
9896
}
9997

10098
private static Map<String, MapperDescriptor> createActionMappers(List<Class<?>> classes) {
10199
MapperDescriptorFactory mapperDescriptorFactory = new MapperDescriptorFactory();
102100
Map<String, MapperDescriptor> mappers = Maps.newHashMapWithExpectedSize(classes.size());
103-
for (Class clazz : classes) {
101+
for (Class<?> clazz : classes) {
104102
try {
105103
MapperDescriptor mapperDescriptor = mapperDescriptorFactory.create(clazz);
106104
mappers.put(mapperDescriptor.getName(), mapperDescriptor);

app/aem/core/src/main/java/com/cognifide/apm/core/actions/MapperDescriptorFactory.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ private Optional<MappingDescriptor> create(Mapper mapper, Method method) {
115115
}
116116

117117
private <T extends Annotation> T getAnnotation(Annotation[] annotations, Class<T> type) {
118-
for (int i = 0; i < annotations.length; i++) {
119-
if (type.isInstance(annotations[i])) {
120-
return (T) annotations[i];
118+
for (Annotation annotation : annotations) {
119+
if (type.isInstance(annotation)) {
120+
return (T) annotation;
121121
}
122122
}
123123
return null;
@@ -129,16 +129,15 @@ private <T extends Annotation> boolean containsAnnotation(Annotation[] annotatio
129129

130130
private Class<? extends ApmType> getApmType(Type type) {
131131
if (type instanceof Class) {
132-
Class aClass = (Class) type;
133-
if (String.class.equals(aClass)) {
132+
Class<?> clazz = (Class<?>) type;
133+
if (String.class.equals(clazz)) {
134134
return ApmString.class;
135-
}
136-
if (Integer.class.equals(aClass)) {
135+
} else if (Integer.class.equals(clazz)) {
137136
return ApmInteger.class;
138137
}
139138
} else if (type instanceof ParameterizedType) {
140139
ParameterizedType parameterizedType = (ParameterizedType) type;
141-
Class rawType = (Class) parameterizedType.getRawType();
140+
Class<?> rawType = (Class<?>) parameterizedType.getRawType();
142141
if (List.class.equals(rawType)) {
143142
return ApmList.class;
144143
} else if (Map.class.equals(rawType)) {

0 commit comments

Comments
 (0)