• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / migrations / #507

12 Nov 2023 12:12AM UTC coverage: 80.317% (-0.06%) from 80.381%
#507

push

github

web-flow
Merge pull request #325 from hazendaz/master

Sonar / exception cleanup

1 of 2 new or added lines in 1 file covered. (50.0%)

1771 of 2205 relevant lines covered (80.32%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.0
/src/main/java/org/apache/ibatis/migration/JavaMigrationLoader.java
1
/*
2
 *    Copyright 2010-2023 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.apache.ibatis.migration;
17

18
import java.io.Reader;
19
import java.io.StringReader;
20
import java.lang.reflect.Modifier;
21
import java.util.ArrayList;
22
import java.util.List;
23
import java.util.Set;
24

25
import org.apache.ibatis.migration.io.ResolverUtil;
26

27
public class JavaMigrationLoader implements MigrationLoader {
28

29
  private String[] packageNames;
30

31
  private ClassLoader classLoader;
32

33
  public JavaMigrationLoader(String... packageNames) {
34
    this(null, packageNames);
1✔
35
  }
1✔
36

37
  public JavaMigrationLoader(ClassLoader classLoader, String... packageNames) {
1✔
38
    this.classLoader = classLoader;
1✔
39
    this.packageNames = packageNames;
1✔
40
  }
1✔
41

42
  @Override
43
  public List<Change> getMigrations() {
44
    List<Change> migrations = new ArrayList<>();
1✔
45
    ResolverUtil<MigrationScript> resolver = getResolver();
1✔
46
    resolver.findImplementations(MigrationScript.class, packageNames);
1✔
47
    Set<Class<? extends MigrationScript>> classes = resolver.getClasses();
1✔
48
    for (Class<? extends MigrationScript> clazz : classes) {
1✔
49
      try {
50
        if (!Modifier.isAbstract(clazz.getModifiers())) {
1✔
51
          MigrationScript script = clazz.getDeclaredConstructor().newInstance();
1✔
52
          Change change = parseChangeFromMigrationScript(script);
1✔
53
          migrations.add(change);
1✔
54
        }
55
      } catch (Exception e) {
×
56
        throw new MigrationException("Could not instanciate MigrationScript: " + clazz.getName(), e);
×
57
      }
1✔
58
    }
1✔
59
    return migrations;
1✔
60
  }
61

62
  private Change parseChangeFromMigrationScript(MigrationScript script) {
63
    Change change = new Change();
1✔
64
    change.setId(script.getId());
1✔
65
    change.setDescription(script.getDescription());
1✔
66
    change.setFilename(script.getClass().getName());
1✔
67
    return change;
1✔
68
  }
69

70
  @Override
71
  public Reader getScriptReader(Change change, boolean undo) {
72
    ResolverUtil<MigrationScript> resolver = getResolver();
1✔
73
    final String className = change.getFilename();
1✔
74
    for (String pkg : packageNames) {
1✔
75
      resolver.find(
1✔
76
          type -> type != null && MigrationScript.class.isAssignableFrom(type) && type.getName().equals(className),
1✔
77
          pkg);
78
    }
79
    Set<Class<? extends MigrationScript>> classes = resolver.getClasses();
1✔
80
    // There should be only one script.
81
    for (Class<? extends MigrationScript> clazz : classes) {
1✔
82
      try {
83
        MigrationScript script = clazz.getDeclaredConstructor().newInstance();
1✔
84
        return new StringReader(undo ? script.getDownScript() : script.getUpScript());
1✔
85
      } catch (Exception e) {
×
86
        throw new MigrationException("Could not instanciate MigrationScript: " + clazz.getName(), e);
×
87
      }
88
    }
NEW
89
    return null;
×
90
  }
91

92
  @Override
93
  public Reader getBootstrapReader() {
94
    return getSoleScriptReader(BootstrapScript.class);
1✔
95
  }
96

97
  @Override
98
  public Reader getOnAbortReader() {
99
    return getSoleScriptReader(OnAbortScript.class);
×
100
  }
101

102
  public <T extends SimpleScript> Reader getSoleScriptReader(Class<T> scriptClass) {
103
    ResolverUtil<T> resolver = getResolver();
1✔
104
    resolver.findImplementations(scriptClass, packageNames);
1✔
105
    Set<Class<? extends T>> classes = resolver.getClasses();
1✔
106
    if (classes == null || classes.isEmpty()) {
1✔
107
      return null;
×
108
    }
109
    if (classes.size() > 1) {
1✔
110
      throw new MigrationException("There can be only one implementation of " + scriptClass.getName());
×
111
    }
112
    Class<? extends T> clazz = classes.iterator().next();
1✔
113
    try {
114
      T script = clazz.getDeclaredConstructor().newInstance();
1✔
115
      return new StringReader(script.getScript());
1✔
116
    } catch (Exception e) {
×
117
      throw new MigrationException("Could not instanciate script class: " + clazz.getName(), e);
×
118
    }
119
  }
120

121
  private <T> ResolverUtil<T> getResolver() {
122
    ResolverUtil<T> resolver = new ResolverUtil<>();
1✔
123
    if (classLoader != null) {
1✔
124
      resolver.setClassLoader(classLoader);
×
125
    }
126
    return resolver;
1✔
127
  }
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc