From feff0c4f670183e12a6a56cfd3e9909380537bab Mon Sep 17 00:00:00 2001 From: aclement Date: Fri, 5 Sep 2008 19:20:30 +0000 Subject: [PATCH] 246125: pull across useful utils from bcel package to 'normal' weaver package --- weaver/src/org/aspectj/weaver/Advice.java | 3 +- .../src/org/aspectj/weaver/AnnotationAJ.java | 9 ++++ .../aspectj/weaver/StandardAnnotation.java | 14 ++++++ weaver/src/org/aspectj/weaver/Utils.java | 43 +++++++++++++++++++ .../aspectj/weaver/bcel/BcelAnnotation.java | 17 ++++++++ .../src/org/aspectj/weaver/bcel/Utility.java | 42 ++---------------- 6 files changed, 87 insertions(+), 41 deletions(-) create mode 100644 weaver/src/org/aspectj/weaver/Utils.java diff --git a/weaver/src/org/aspectj/weaver/Advice.java b/weaver/src/org/aspectj/weaver/Advice.java index b63fff6cb..60a90a9c6 100644 --- a/weaver/src/org/aspectj/weaver/Advice.java +++ b/weaver/src/org/aspectj/weaver/Advice.java @@ -18,7 +18,6 @@ import java.util.List; import org.aspectj.asm.AsmManager; import org.aspectj.bridge.IMessage; import org.aspectj.bridge.ISourceLocation; -import org.aspectj.weaver.bcel.Utility; import org.aspectj.weaver.patterns.AndPointcut; import org.aspectj.weaver.patterns.PerClause; import org.aspectj.weaver.patterns.Pointcut; @@ -218,7 +217,7 @@ public abstract class Advice extends ShadowMunger { if (shadowReturnType.isParameterizedType() && (shadowReturnType.getRawType() == afterReturningType.getRawType())) { uncheckedMatchWith = shadowReturnType.getSimpleName(); } - if (!Utility.isSuppressing(getSignature().getAnnotations(), "uncheckedArgument")) { + if (!Utils.isSuppressing(getSignature().getAnnotations(), "uncheckedArgument")) { world.getLint().uncheckedArgument.signal(new String[] { afterReturningType.getSimpleName(), uncheckedMatchWith, afterReturningType.getSimpleBaseName(), shadow.toResolvedString(world) }, getSourceLocation(), new ISourceLocation[] { shadow.getSourceLocation() }); diff --git a/weaver/src/org/aspectj/weaver/AnnotationAJ.java b/weaver/src/org/aspectj/weaver/AnnotationAJ.java index 5b51dbd25..c7605c1ee 100644 --- a/weaver/src/org/aspectj/weaver/AnnotationAJ.java +++ b/weaver/src/org/aspectj/weaver/AnnotationAJ.java @@ -90,4 +90,13 @@ public interface AnnotationAJ { */ public boolean isRuntimeVisible(); + /** + * Determine the string representation of the value of a field. For example in @SuppressAjWarnings({"adviceDidNotMatch"}) + * the return value for getStringFormOfValue("value") would be "[adviceDidNotMatch]". + * + * @param name the name of the annotation field being looked up + * @return string representation of the value of that field, may be null if no such field set + */ + public String getStringFormOfValue(String name); + } diff --git a/weaver/src/org/aspectj/weaver/StandardAnnotation.java b/weaver/src/org/aspectj/weaver/StandardAnnotation.java index 64962ecf4..3dd7bfb82 100644 --- a/weaver/src/org/aspectj/weaver/StandardAnnotation.java +++ b/weaver/src/org/aspectj/weaver/StandardAnnotation.java @@ -136,4 +136,18 @@ public class StandardAnnotation extends AbstractAnnotationAJ { nvPairs.add(pair); } + /** + * {@inheritDoc} + */ + public String getStringFormOfValue(String name) { + if (hasNameValuePairs()) { + for (Iterator iterator = nvPairs.iterator(); iterator.hasNext();) { + AnnotationNameValuePair nvPair = (AnnotationNameValuePair) iterator.next(); + if (nvPair.getName().equals(name)) { + return nvPair.getValue().stringify(); + } + } + } + return null; + } } diff --git a/weaver/src/org/aspectj/weaver/Utils.java b/weaver/src/org/aspectj/weaver/Utils.java new file mode 100644 index 000000000..8053af0c0 --- /dev/null +++ b/weaver/src/org/aspectj/weaver/Utils.java @@ -0,0 +1,43 @@ +/* ******************************************************************* + * Copyright (c) 2008 Contributors. + * All rights reserved. + * This program and the accompanying materials are made available + * under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution and is available at + * http://eclipse.org/legal/epl-v10.html + * + * ******************************************************************/ +package org.aspectj.weaver; + +/** + * + * @author Andy Clement + */ +public class Utils { + + /** + * Check if the annotations contain a SuppressAjWarnings annotation and if that annotation specifies that the given lint message + * (identified by its key) should be ignored. + * + */ + public static boolean isSuppressing(AnnotationAJ[] anns, String lintkey) { + if (anns == null) { + return false; + } + // Go through the annotation types on the advice + for (int i = 0; i < anns.length; i++) { + if (UnresolvedType.SUPPRESS_AJ_WARNINGS.getSignature().equals(anns[i].getTypeSignature())) { + // Two possibilities: + // 1. there are no values specified (i.e. @SuppressAjWarnings) + // 2. there are values specified (i.e. @SuppressAjWarnings("A") or @SuppressAjWarnings({"A","B"}) + String value = anns[i].getStringFormOfValue("value"); + // Slightly lazy, just doing a string indexof + if (value == null || value.indexOf(lintkey) != -1) { + return true; + } + } + } + return false; + } + +} diff --git a/weaver/src/org/aspectj/weaver/bcel/BcelAnnotation.java b/weaver/src/org/aspectj/weaver/bcel/BcelAnnotation.java index 9d7e83bca..9dfaa22c6 100644 --- a/weaver/src/org/aspectj/weaver/bcel/BcelAnnotation.java +++ b/weaver/src/org/aspectj/weaver/bcel/BcelAnnotation.java @@ -104,4 +104,21 @@ public class BcelAnnotation extends AbstractAnnotationAJ { return bcelAnnotation; } + /** + * {@inheritDoc} + */ + public String getStringFormOfValue(String name) { + List annotationValues = this.bcelAnnotation.getValues(); + if (annotationValues == null || annotationValues.size() == 0) { + return null; + } else { + for (Iterator iterator = annotationValues.iterator(); iterator.hasNext();) { + ElementNameValuePairGen nvPair = (ElementNameValuePairGen) iterator.next(); + if (nvPair.getNameString().equals(name)) { + return nvPair.getValue().stringifyValue(); + } + } + return null; + } + } } diff --git a/weaver/src/org/aspectj/weaver/bcel/Utility.java b/weaver/src/org/aspectj/weaver/bcel/Utility.java index 52d46b6c0..97830a950 100644 --- a/weaver/src/org/aspectj/weaver/bcel/Utility.java +++ b/weaver/src/org/aspectj/weaver/bcel/Utility.java @@ -61,6 +61,7 @@ import org.aspectj.weaver.Lint; import org.aspectj.weaver.Member; import org.aspectj.weaver.ResolvedType; import org.aspectj.weaver.UnresolvedType; +import org.aspectj.weaver.Utils; import org.aspectj.weaver.World; import org.aspectj.weaver.AjAttribute.WeaverVersionInfo; @@ -681,53 +682,16 @@ public class Utility { * Checks for suppression specified on the member or on the declaring type of that member */ public static boolean isSuppressing(Member member, String lintkey) { - boolean isSuppressing = Utility.isSuppressing(member.getAnnotations(), lintkey); + boolean isSuppressing = Utils.isSuppressing(member.getAnnotations(), lintkey); if (isSuppressing) return true; UnresolvedType type = member.getDeclaringType(); if (type instanceof ResolvedType) { - return Utility.isSuppressing(((ResolvedType) type).getAnnotations(), lintkey); + return Utils.isSuppressing(((ResolvedType) type).getAnnotations(), lintkey); } return false; } - /** - * Check if the annotations contain a SuppressAjWarnings annotation and if that annotation specifies that the given lint message - * (identified by its key) should be ignored. - * - */ - public static boolean isSuppressing(AnnotationAJ[] anns, String lintkey) { - if (anns == null) - return false; - boolean suppressed = false; - // Go through the annotation types on the advice - for (int i = 0; !suppressed && i < anns.length; i++) { - // Check for the SuppressAjWarnings annotation - if (UnresolvedType.SUPPRESS_AJ_WARNINGS.getSignature().equals(anns[i].getTypeSignature())) { - // Two possibilities: - // 1. there are no values specified (i.e. @SuppressAjWarnings) - // 2. there are values specified (i.e. @SuppressAjWarnings("A") - // or @SuppressAjWarnings({"A","B"}) - List vals = ((BcelAnnotation) anns[i]).getBcelAnnotation().getValues(); - if (vals == null || vals.isEmpty()) { // (1) - suppressed = true; - } else { // (2) - // We know the value is an array value - ArrayElementValueGen array = (ArrayElementValueGen) ((ElementNameValuePairGen) vals.get(0)).getValue(); - ElementValueGen[] values = array.getElementValuesArray(); - for (int j = 0; j < values.length; j++) { - // We know values in the array are strings - SimpleElementValueGen value = (SimpleElementValueGen) values[j]; - if (value.getValueString().equals(lintkey)) { - suppressed = true; - } - } - } - } - } - return suppressed; - } - public static List/* Lint.Kind */getSuppressedWarnings(AnnotationAJ[] anns, Lint lint) { if (anns == null) return Collections.EMPTY_LIST; -- 2.39.5