This is an automated email from the ASF dual-hosted git repository. mbenson pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-weaver.git
commit bf128d3e93feb5765905ba2e69cd354721a9e7e3 Author: Matt Benson <mben...@apache.org> AuthorDate: Fri May 18 12:00:38 2018 -0500 cleanup warnings; add utility method --- .../commons/weaver/normalizer/Normalizer.java | 19 ++++------- .../commons/weaver/privilizer/ActionGenerator.java | 4 +-- .../weaver/privilizer/BlueprintingVisitor.java | 2 +- .../commons/weaver/privilizer/Privilizer.java | 35 +++----------------- .../weaver/privilizer/PrivilizerCleaner.java | 2 +- .../weaver/privilizer/PrivilizingVisitor.java | 2 +- .../commons/weaver/model/WeaveEnvironment.java | 38 ++++++++++++++++++++++ 7 files changed, 54 insertions(+), 48 deletions(-) diff --git a/modules/normalizer/src/main/java/org/apache/commons/weaver/normalizer/Normalizer.java b/modules/normalizer/src/main/java/org/apache/commons/weaver/normalizer/Normalizer.java index b40b225..1894dba 100644 --- a/modules/normalizer/src/main/java/org/apache/commons/weaver/normalizer/Normalizer.java +++ b/modules/normalizer/src/main/java/org/apache/commons/weaver/normalizer/Normalizer.java @@ -220,7 +220,7 @@ public class Normalizer { } } - private static final class CustomClassWriter extends ClassWriter { + private final class CustomClassWriter extends ClassWriter { CustomClassWriter(final int flags) { super(flags); } @@ -231,7 +231,7 @@ public class Normalizer { @Override protected String getCommonSuperClass(final String type1, final String type2) { - return "java/lang/Object"; + return env.getCommonSuperClass(type1, type2); } } @@ -264,7 +264,7 @@ public class Normalizer { try (OutputStream outputStream = classfile.getOutputStream()) { outputStream.write(bytecode); } catch (final IOException e) { - throw new RuntimeException(e); + throw new IllegalStateException(e); } } } @@ -333,7 +333,7 @@ public class Normalizer { } catch (final RuntimeException e) { throw e; } catch (final Exception e) { - throw new RuntimeException(e); + throw new IllegalStateException(e); } } return result; @@ -358,10 +358,8 @@ public class Normalizer { * @param key {@link String} {@link Pair} indicating supertype and constructor signature * @param toMerge matching classes * @throws IOException on I/O error - * @throws ClassNotFoundException if class not found */ - private void rewrite(final Pair<String, String> key, final Set<ClassWrapper> toMerge) throws IOException, - ClassNotFoundException { + private void rewrite(final Pair<String, String> key, final Set<ClassWrapper> toMerge) throws IOException { final String target = copy(key, toMerge.iterator().next()); env.info("Merging %s identical %s implementations with constructor %s to type %s", toMerge.size(), key.getLeft(), key.getRight(), target); @@ -478,16 +476,14 @@ public class Normalizer { * @param classWrapper * @return the generated classname. * @throws IOException - * @throws ClassNotFoundException */ - private String copy(final Pair<String, String> key, final ClassWrapper classWrapper) throws IOException, - ClassNotFoundException { + private String copy(final Pair<String, String> key, final ClassWrapper classWrapper) throws IOException { env.debug("Copying %s to %s", key, targetPackage); final MessageDigest md5; try { md5 = MessageDigest.getInstance("MD5"); } catch (final NoSuchAlgorithmException e) { - throw new RuntimeException(e); + throw new IllegalStateException(e); } md5.update(key.getLeft().getBytes(StandardCharsets.UTF_8)); md5.update(key.getRight().getBytes(StandardCharsets.UTF_8)); @@ -500,7 +496,6 @@ public class Normalizer { try (InputStream bytecode = env.getClassfile(classWrapper.wrapped).getInputStream()) { final ClassReader reader = new ClassReader(bytecode); - final ClassVisitor writeClass = new WriteClass(); // we're doing most of this by hand; we only read the original class to hijack signature, ctor exceptions, diff --git a/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/ActionGenerator.java b/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/ActionGenerator.java index 9f880a8..34eb130 100644 --- a/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/ActionGenerator.java +++ b/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/ActionGenerator.java @@ -88,7 +88,7 @@ class ActionGenerator extends Privilizer.WriteClass implements Builder<Type> { final Type[] args = implIsStatic ? methd.getArgumentTypes() : ArrayUtils.insert(0, methd.getArgumentTypes(), owner.target); this.helper = new Method(privilizer().generateName("access$" + index), methd.getReturnType(), args); - this.result = privilizer().wrap(methd.getReturnType()); + this.result = Privilizer.wrap(methd.getReturnType()); this.fields = fields(args); this.actionInterface = Type.getType(exc ? PrivilegedExceptionAction.class : PrivilegedAction.class); } @@ -157,7 +157,7 @@ class ActionGenerator extends Privilizer.WriteClass implements Builder<Type> { final SignatureVisitor actionImplemented = type.visitInterface(); actionImplemented.visitClassType(actionInterface.getInternalName()); final SignatureVisitor visitTypeArgument = actionImplemented.visitTypeArgument('='); - new SignatureReader(privilizer().wrap(methd.getReturnType()).getDescriptor()).accept(visitTypeArgument); + new SignatureReader(Privilizer.wrap(methd.getReturnType()).getDescriptor()).accept(visitTypeArgument); actionImplemented.visitEnd(); final String signature = type.toString(); diff --git a/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/BlueprintingVisitor.java b/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/BlueprintingVisitor.java index ac3b1ce..ef88a7f 100644 --- a/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/BlueprintingVisitor.java +++ b/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/BlueprintingVisitor.java @@ -128,7 +128,7 @@ class BlueprintingVisitor extends Privilizer.PrivilizerClassVisitor { try (InputStream bytecode = privilizer().env.getClassfile(className).getInputStream()) { new ClassReader(bytecode).accept(result, ClassReader.SKIP_DEBUG | ClassReader.EXPAND_FRAMES); } catch (final Exception e) { - throw new RuntimeException(e); + throw new IllegalStateException(e); } return result; } diff --git a/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java b/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java index 1fd50d8..25d54b0 100644 --- a/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java +++ b/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/Privilizer.java @@ -84,34 +84,7 @@ public class Privilizer { @Override protected String getCommonSuperClass(final String type1, final String type2) { -// https://gitlab.ow2.org/asm/asm/merge_requests/166 - ClassLoader classLoader = env.classLoader; - Class<?> class1; - try { - class1 = Class.forName(type1.replace('/', '.'), false, classLoader); - } catch (Exception e) { - throw new TypeNotPresentException(type1, e); - } - Class<?> class2; - try { - class2 = Class.forName(type2.replace('/', '.'), false, classLoader); - } catch (Exception e) { - throw new TypeNotPresentException(type2, e); - } - if (class1.isAssignableFrom(class2)) { - return type1; - } - if (class2.isAssignableFrom(class1)) { - return type2; - } - if (class1.isInterface() || class2.isInterface()) { - return "java/lang/Object"; - } else { - do { - class1 = class1.getSuperclass(); - } while (!class1.isAssignableFrom(class2)); - return class1.getName().replace('.', '/'); - } + return env.getCommonSuperClass(type1, type2); } } @@ -141,7 +114,7 @@ public class Privilizer { try (OutputStream outputStream = classfile.getOutputStream()) { outputStream.write(bytecode); } catch (final IOException e) { - throw new RuntimeException(e); + throw new IllegalStateException(e); } } } @@ -233,7 +206,7 @@ public class Privilizer { classReader.accept(cvr, ClassReader.EXPAND_FRAMES); } catch (final Exception e) { - throw new RuntimeException(e); + throw new IllegalStateException(e); } } @@ -248,7 +221,7 @@ public class Privilizer { classReader.accept(cv, ClassReader.EXPAND_FRAMES); } catch (final Exception e) { - throw new RuntimeException(e); + throw new IllegalStateException(e); } } diff --git a/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerCleaner.java b/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerCleaner.java index bb09954..27939ec 100644 --- a/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerCleaner.java +++ b/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizerCleaner.java @@ -73,7 +73,7 @@ public class PrivilizerCleaner implements Cleaner { } }, ClassReader.SKIP_CODE + ClassReader.SKIP_DEBUG + ClassReader.SKIP_FRAMES); } catch (final Exception e) { - throw new RuntimeException(e); + throw new IllegalStateException(e); } } boolean result = false; diff --git a/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java b/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java index 09c8cce..bda171e 100644 --- a/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java +++ b/modules/privilizer/weaver/src/main/java/org/apache/commons/weaver/privilizer/PrivilizingVisitor.java @@ -99,7 +99,7 @@ class PrivilizingVisitor extends Privilizer.PrivilizerClassVisitor { if (Type.getType(Privileged.class).getDescriptor().equals(desc)) { final AccessLevel localAccessLevel = AccessLevel.of(access); if (accessLevel.compareTo(localAccessLevel) > 0) { - throw new RuntimeException(new IllegalAccessException("Method " + className + "#" + methd + throw new IllegalStateException(new IllegalAccessException("Method " + className + "#" + methd + " must have maximum access level '" + accessLevel + "' but is defined wider ('" + localAccessLevel + "')")); } diff --git a/processor/src/main/java/org/apache/commons/weaver/model/WeaveEnvironment.java b/processor/src/main/java/org/apache/commons/weaver/model/WeaveEnvironment.java index 565eb4b..af7bd2b 100644 --- a/processor/src/main/java/org/apache/commons/weaver/model/WeaveEnvironment.java +++ b/processor/src/main/java/org/apache/commons/weaver/model/WeaveEnvironment.java @@ -215,6 +215,44 @@ public abstract class WeaveEnvironment { } /** + * Calculate a common superclass for the specified classnames, in terms of the {@link WeaveEnvironment}'s + * {@link #classLoader}. This method exists as a convenience feature for weaver modules that use ASM, and is + * implemented with the same logic as the method of the same name in that library's {@code ClassVisitor} class. + * + * @param type1 + * @param type2 + * @return {@link String} internal name of superclass common to {@code type1} and {@code type2} + */ + public final String getCommonSuperClass(final String type1, final String type2) { + // https://gitlab.ow2.org/asm/asm/merge_requests/166 + Class<?> class1; + try { + class1 = Class.forName(type1.replace('/', '.'), false, classLoader); + } catch (Exception e) { + throw new TypeNotPresentException(type1, e); + } + Class<?> class2; + try { + class2 = Class.forName(type2.replace('/', '.'), false, classLoader); + } catch (Exception e) { + throw new TypeNotPresentException(type2, e); + } + if (class1.isAssignableFrom(class2)) { + return type1; + } + if (class2.isAssignableFrom(class1)) { + return type2; + } + if (class1.isInterface() || class2.isInterface()) { + return "java/lang/Object"; + } + do { + class1 = class1.getSuperclass(); + } while (!class1.isAssignableFrom(class2)); + return class1.getName().replace('.', '/'); + } + + /** * Delete the specified resource. * @param name to delete * @return whether successful -- To stop receiving notification emails like this one, please contact mben...@apache.org.