![JAR search and dependency download from the Maven repository](/logo.png)
patterntesting.runtime.util.JoinPointHelper Maven / Gradle / Ivy
Go to download
PatternTesting Runtime (patterntesting-rt) is the runtime component for
the PatternTesting framework. It provides the annotations and base classes
for the PatternTesting testing framework (e.g. patterntesting-check,
patterntesting-concurrent or patterntesting-exception) but can be also
used standalone for classpath monitoring or profiling.
It uses AOP and AspectJ to perform this feat.
The newest version!
/*
* $Id: JoinPointHelper.java,v 1.14 2009/12/19 22:34:09 oboehm Exp $
*
* Copyright (c) 2008 by Oliver Boehm
*
* 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 orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 23.10.2008 by oliver ([email protected])
*/
package patterntesting.runtime.util;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import org.aspectj.lang.*;
import org.aspectj.lang.reflect.*;
import patterntesting.annotation.check.runtime.*;
import patterntesting.runtime.annotation.DontLogMe;
/**
* The Class JoinPointHelper.
*
* @author oliver
* @since 23.10.2008
* @version $Revision: 1.14 $
*/
public class JoinPointHelper {
/**
* Gets the as string.
*
* @param joinpoint the joinpoint
*
* @return the as string
*/
public static String getAsString(JoinPoint joinpoint) {
Signature sig = joinpoint.getSignature();
Object[] args = joinpoint.getArgs();
if (sig instanceof FieldSignature) {
if (args.length == 0) {
return "get " + sig.toShortString();
} else {
return "set " + sig.toShortString() + " = "
+ getArgAsString(args[0]);
}
} else if (sig instanceof CodeSignature) {
Annotation[][] paramAnnotations = getParameterAnnotations(joinpoint);
return SignatureHelper.getAsString(sig.getDeclaringTypeName(), sig)
+ getArgsAsString(args, paramAnnotations);
} else {
return SignatureHelper.getAsString(sig.getDeclaringTypeName(), sig)
+ getArgsAsString(args);
}
}
/**
* Gets the as short string.
*
* @param joinpoint the joinpoint
*
* @return the as short string
*/
public static String getAsShortString(JoinPoint joinpoint) {
Signature sig = joinpoint.getSignature();
if (sig instanceof FieldSignature) {
return getAsString(joinpoint);
}
Object[] args = joinpoint.getArgs();
return SignatureHelper.getAsString(sig.getDeclaringType()
.getSimpleName(), sig)
+ getArgsAsString(args);
}
/**
* Gets the parameter annotations.
*
* @param joinpoint should be a Method- or Constructor-Signature
*
* @return the annotations of a method or constructor, otherwise null
*/
@MayReturnNull
public static Annotation[][] getParameterAnnotations(JoinPoint joinpoint) {
Signature sig = joinpoint.getSignature();
if (sig instanceof MethodSignature) {
MethodSignature methodSig = (MethodSignature) sig;
return methodSig.getMethod().getParameterAnnotations();
} else if (sig instanceof ConstructorSignature) {
ConstructorSignature ctorSig = (ConstructorSignature) sig;
return getParameterAnnotations(ctorSig);
} else {
return null;
}
}
/**
* Normally you get an annotation array with the size of the constructor
* parameters. But not for inner classes. Here the first parameter of the
* constructor is the embedded class. In this case we must correct the
* annotation array
*
* @param ctorSig
* @return
*/
@MayReturnNull
private static Annotation[][] getParameterAnnotations(ConstructorSignature ctorSig) {
Constructor> ctor = ctorSig.getConstructor();
Annotation[][] annotations = ctor.getParameterAnnotations();
if (isInnerClass(ctorSig)) {
Annotation[][] corrected = new Annotation[annotations.length+1][0];
corrected[0] = null;
System.arraycopy(annotations, 0, corrected, 1, annotations.length);
return corrected;
}
return annotations;
}
/**
* The difference to Signature.toString() is that in case of a methode
* or constructor the return type is not part of result.
*
* @param sig the sig
*
* @return signature as string
*
* @deprecated Use {@link SignatureHelper#getAsString(Signature)} instead
*/
@Deprecated
public static String getAsString(Signature sig) {
return SignatureHelper.getAsString(sig);
}
/**
* Gets the args as string.
*
* @param args the args
*
* @return the args as string
*/
@NullArgsAllowed
public static String getArgsAsString(final Object[] args) {
if ((args == null) || (args.length == 0)) {
return "()";
}
String s = "(" + getArgAsString(args[0]);
for (int i = 1; i < args.length; i++) {
s += ", " + getArgAsString(args[i]);
}
return s + ")";
}
@NullArgsAllowed
private static String getArgsAsString(final Object[] args,
final Annotation[][] annotations) {
if ((args == null) || (args.length == 0)) {
return "()";
}
String s = "(" + getArgAsString(args[0], annotations[0]);
for (int i = 1; i < args.length; i++) {
s += ", " + getArgAsString(args[i], annotations[i]);
}
return s + ")";
}
/**
* Gets the arg as string.
*
* @param obj the obj
*
* @return the arg as string
*/
@NullArgsAllowed
public static String getArgAsString(final Object obj) {
if (obj == null) {
return "(null)";
}
if (obj instanceof String) {
return "\"" + obj + "\"";
}
return obj.toString();
}
@NullArgsAllowed
private static String getArgAsString(final Object obj,
final Annotation[] annotations) {
if (hasDontLogMeAnnotation(annotations)) {
return "*";
} else {
return getArgAsString(obj);
}
}
private static boolean hasDontLogMeAnnotation(final Annotation[] annotations) {
if (annotations == null) {
return false;
}
for (Annotation annotation : annotations) {
if (annotation instanceof DontLogMe) {
return true;
}
}
return false;
}
/**
* The Java compiler generates something like OuterClass$InnerClass
* as name for the inner class. So if a name contains a '$' it is
* probably an inner class.
*
* @param jp the jp
*
* @return true if joinpoint belongs to an inner class
*/
public static boolean isInnerClass(JoinPoint jp) {
return isInnerClass(jp.getSignature());
}
/**
* The Java compiler generates something like OuterClass$InnerClass
* as name for the inner class. So if a name contains a '$' it is
* probably an inner class.
*
* @param sig the sig
*
* @return true if signature belongs to an inner class
*/
public static boolean isInnerClass(Signature sig) {
String typeName = sig.getDeclaringTypeName();
return typeName.contains("$");
}
}
/**
* $Log: JoinPointHelper.java,v $
* Revision 1.14 2009/12/19 22:34:09 oboehm
* trailing spaces removed
*
* Revision 1.13 2009/12/14 17:14:30 oboehm
* trailing tabs removed
*
* Revision 1.12 2009/09/25 14:49:43 oboehm
* javadocs completed with the help of JAutodoc
*
* Revision 1.11 2009/09/18 13:54:52 oboehm
* javadoc warnings fixed
*
* Revision 1.10 2009/09/02 19:23:42 oboehm
* getParameterAnnotations() works now also for inner class constructors
*
* Revision 1.9 2009/06/10 19:56:57 oboehm
* DontLogMe annotation added to hide parameters logged by @ProfileMe
*
* $Source: /cvsroot/patterntesting/PatternTesting08/patterntesting-rt/src/main/java/patterntesting/runtime/util/JoinPointHelper.java,v $
*/
© 2015 - 2025 Weber Informatics LLC | Privacy Policy