String name = m.getName(); if (m.getDeclaringClass() == clazz) { if (Modifier.isNative(mod)) return; if (Modifier.isFinal(mod)) { mod &= ~Modifier.FINAL; m2.setModifiers(mod); if (Modifier.isFinal(mod)) return; m2 = CtNewMethod.delegator(findOriginal(m, dontSearch), clazz); m2.setModifiers(mod); clazz.addMethod(m2); + "_" + name); if (Modifier.isStatic(mod)) body = trapStaticMethod; else clazz); wmethod.setModifiers(mod); clazz.addMethod(wmethod);
private CtClass produceProxyClass(CtClass orgclass, Class orgRtClass) throws CannotCompileException, NotFoundException int modify = orgclass.getModifiers(); if (Modifier.isAbstract(modify) || Modifier.isNative(modify) || !Modifier.isPublic(modify)) throw new CannotCompileException(orgclass.getName() + " must be public, non-native, and non-abstract."); CtClass proxy = classPool.makeClass(orgclass.getName(), orgclass.getSuperclass()); = new CtField(classPool.get("javassist.tools.rmi.ObjectImporter"), fieldImporter, proxy); f.setModifiers(Modifier.PRIVATE); proxy.addField(f, CtField.Initializer.byParameter(0)); f = new CtField(CtClass.intType, fieldObjectId, proxy); f.setModifiers(Modifier.PRIVATE); proxy.addField(f, CtField.Initializer.byParameter(1)); throw new CannotCompileException(e);
public TransformCall(Transformer next, String oldMethodName, CtMethod substMethod) { super(next); methodname = oldMethodName; methodDescriptor = substMethod.getMethodInfo2().getDescriptor(); classname = newClassname = substMethod.getDeclaringClass().getName(); newMethodname = substMethod.getName(); constPool = null; newMethodIsPrivate = Modifier.isPrivate(substMethod.getModifiers()); }
private void processMethods(CtClass clazz, boolean dontSearch) throws CannotCompileException, NotFoundException { CtMethod[] ms = clazz.getMethods(); for (int i = 0; i < ms.length; ++i) { CtMethod m = ms[i]; int mod = m.getModifiers(); if (Modifier.isPublic(mod) && !Modifier.isAbstract(mod)) processMethods0(mod, clazz, m, i, dontSearch); } }
private boolean isInheritable(int mod, CtClass superclazz) { if (Modifier.isPrivate(mod)) return false; if (Modifier.isPackage(mod)) { String pname = getPackageName(); String pname2 = superclazz.getPackageName(); if (pname == null) return pname2 == null; else return pname.equals(pname2); } return true; } }
/** * Returns true if this member is accessible from the given class. */ public boolean visibleFrom(CtClass clazz) { int mod = getModifiers(); if (Modifier.isPublic(mod)) return true; else if (Modifier.isPrivate(mod)) return clazz == declaringClass; else { // package or protected String declName = declaringClass.getPackageName(); String fromName = clazz.getPackageName(); boolean visible; if (declName == null) visible = fromName == null; else visible = declName.equals(fromName); if (!visible && Modifier.isProtected(mod)) return clazz.subclassOf(declaringClass); return visible; } }
public <T> Class<T> createStructClz( Class<T> clazz ) throws Exception { if ( Modifier.isFinal(clazz.getModifiers()) || Modifier.isAbstract(clazz.getModifiers()) ) { throw new RuntimeException("Cannot add final classes to structs "+clazz.getName()); return present; ClassPool pool = defaultPool; CtClass newClz = pool.makeClass(proxyName); CtClass orig = null; if ( rawByteClassDefs.get(clazz.getName()) != null ) { orig = pool.makeClass( new ByteArrayInputStream(rawByteClassDefs.get(clazz.getName()))); } else { orig = pool.getOrNull(clazz.getName()); if ( orig == null ) { pool.insertClassPath(new ClassClassPath(clazz)); newClz.setSuperclass(orig); CtMethod[] methods = orig.getMethods(); for (int i = 0; i < methods.length; i++) { CtMethod method = methods[i]; final Class curClz = Class.forName( method.getDeclaringClass().getName() ); boolean allowed = ((method.getModifiers() & AccessFlag.ABSTRACT) == 0 ) && (method.getModifiers() & AccessFlag.NATIVE) == 0 && (method.getModifiers() & AccessFlag.FINAL) == 0 && ( !method.getDeclaringClass().getName().equals(FSTStruct.class.getName())
public Class<?> toClass(ClassLoader loader, ProtectionDomain pd) { if (mCtc != null) { mCtc.detach(); CtClass ctcs = mSuperClass == null ? null : mPool.get(mSuperClass); if (mClassName == null) { mClassName = (mSuperClass == null || javassist.Modifier.isPublic(ctcs.getModifiers()) ? ClassGenerator.class.getName() : mSuperClass + "$sc") + id; mCtc = mPool.makeClass(mClassName); if (mSuperClass != null) { mCtc.setSuperclass(ctcs); mCtc.addInterface(mPool.get(DC.class.getName())); // add dynamic class tag. if (mInterfaces != null) { for (String cl : mInterfaces) { mCtc.addField(CtField.make(code, mCtc)); throw new RuntimeException(e.getMessage(), e); } catch (CannotCompileException e) { throw new RuntimeException(e.getMessage(), e);
public Class<?> toClass() { if (mCtc != null) mCtc.detach(); long id = CLASS_NAME_COUNTER.getAndIncrement(); try { CtClass ctcs = mSuperClass == null ? null : mPool.get(mSuperClass); if (mClassName == null) mClassName = (mSuperClass == null || javassist.Modifier.isPublic(ctcs.getModifiers()) ? TccClassGenerator.class.getName() : mSuperClass + "$sc") + id; mCtc = mPool.makeClass(mClassName); if (mSuperClass != null) mCtc.setSuperclass(ctcs); mCtc.addInterface(mPool.get(DC.class.getName())); // add dynamic class tag. if (mInterfaces != null) for (String cl : mInterfaces) mCtc.addInterface(mPool.get(cl)); if (mFields != null) for (String code : mFields) mCtc.addField(CtField.make(code, mCtc)); if (mMethods != null) { for (String code : mMethods) { ctMethod.getMethodInfo().addAttribute(attr); throw new RuntimeException(e.getMessage(), e); } catch (CannotCompileException e) { throw new RuntimeException(e.getMessage(), e);
} catch (ClassNotFoundException e) { ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader()); CtClass ctClass = pool.makeClass(parameterClassName); ClassFile classFile = ctClass.getClassFile(); classFile.setVersionToJava5(); ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName))); if (annotation.annotationType().isAnnotationPresent(Constraint.class)) { javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation( classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName())); Method[] members = annotation.annotationType().getMethods(); for (Method member : members) { if (Modifier.isPublic(member.getModifiers()) && member.getParameterTypes().length == 0 && member.getDeclaringClass() == annotation.annotationType()) { CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName)); ctField.getFieldInfo().addAttribute(attribute); ctClass.addField(ctField); parameterClass = ctClass.toClass(clazz.getClassLoader(), null);
private void doInstrument(CtClass target) throws NotFoundException, CannotCompileException { CtMethod[] modelMethods = modelClass.getDeclaredMethods(); CtMethod[] targetMethods = target.getDeclaredMethods(); CtMethod modelGetClass = modelClass.getDeclaredMethod("modelClass"); CtMethod newGetClass = CtNewMethod.copy(modelGetClass, target, null); newGetClass.setBody("{ return " + target.getName() + ".class; }"); int modifiers = method.getModifiers(); if (Modifier.isStatic(modifiers)) { if (targetHasMethod(targetMethods, method)) { Logger.debug("Detected method: " + method.getName() + ", skipping delegate."); } else { CtMethod newMethod; if (Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers)) { newMethod = CtNewMethod.copy(method, target, classMap); newMethod.instrument(conv); for (Object attr : method.getMethodInfo().getAttributes()) { if (attr instanceof SignatureAttribute) { newMethod.getMethodInfo().addAttribute((SignatureAttribute) attr);
ClassPool cp = new ClassPool(); cp.insertClassPath(new ClassClassPath(method.getDeclaringClass())); CtClass cc = cp.get(method.getDeclaringClass().getName()); useIndexMapping = cc.getClassFile().getMajorVersion() >= 52; cm = cc.getDeclaredMethod(method.getName(), params); MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); .getAttribute(LocalVariableAttribute.tag); int offset = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
ClassPool pool = ClassPool.getDefault(); pool.insertClassPath(new ClassClassPath(clazz)); CtClass cc = pool.get(clazz.getName()); paramTypeNames[i] = paramTypes[i].getName(); CtMethod cm = cc.getDeclaredMethod(method.getName(), pool.get(paramTypeNames)); MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute .getAttribute(LocalVariableAttribute.tag); String[] paramNames = new String[cm.getParameterTypes().length]; int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
private static boolean hasMainMethod(InputStream is) throws IOException { try { ClassPool pool = ClassPool.getDefault(); CtClass ctClass = pool.makeClass(is); CtClass stringClass = pool.get("java.lang.String[]"); CtMethod mainMethod = ctClass.getDeclaredMethod("main", new CtClass[] { stringClass }); return mainMethod.getReturnType() == CtClass.voidType && Modifier.isStatic(mainMethod.getModifiers()) && Modifier.isPublic(mainMethod.getModifiers()); } catch (NotFoundException e) { return false; } }
private void ensureType(CtClass cc) throws NotFoundException, CannotCompileException { CtMethod ccms[] = cc.getDeclaredMethods(); if( !javassist.Modifier.isAbstract(cc.getModifiers()) && !hasExisting("frozenType", "()I", ccms) ) { // Build a simple field & method returning the type token cc.addField(new CtField(CtClass.intType, "_frozen$type", cc)); cc.addMethod(CtNewMethod.make("public int frozenType() {" + " return _frozen$type == 0 ? (_frozen$type=water.TypeMap.onIce(\""+cc.getName()+"\")) : _frozen$type;" + "}",cc)); } }
private static CtMethod delegator0(CtMethod delegate, CtClass declaring) throws CannotCompileException, NotFoundException MethodInfo deleInfo = delegate.getMethodInfo2(); String methodName = deleInfo.getName(); String desc = deleInfo.getDescriptor(); ConstPool cp = declaring.getClassFile2().getConstPool(); MethodInfo minfo = new MethodInfo(cp, methodName, desc); minfo.setAccessFlags(deleInfo.getAccessFlags()); boolean isStatic = Modifier.isStatic(delegate.getModifiers()); CtClass deleClass = delegate.getDeclaringClass(); CtClass[] params = delegate.getParameterTypes(); int s;
private CtField[] collectPersistentFields(CtClass managedCtClass) { List<CtField> persistentFieldList = new ArrayList<CtField>(); for ( CtField ctField : managedCtClass.getDeclaredFields() ) { // skip static fields and skip fields added by enhancement and outer reference in inner classes if ( ctField.getName().startsWith( "$$_hibernate_" ) || "this$0".equals( ctField.getName() ) ) { continue; } if ( !Modifier.isStatic( ctField.getModifiers() ) && enhancementContext.isPersistentField( ctField ) ) { persistentFieldList.add( ctField ); } } // HHH-10646 Add fields inherited from @MappedSuperclass // HHH-10981 There is no need to do it for @MappedSuperclass if ( !enhancementContext.isMappedSuperclassClass( managedCtClass ) ) { persistentFieldList.addAll( collectInheritPersistentFields( managedCtClass ) ); } CtField[] orderedFields = enhancementContext.order( persistentFieldList.toArray( new CtField[0] ) ); log.debugf( "Persistent fields for entity %s: %s", managedCtClass.getName(), Arrays.toString( orderedFields ) ); return orderedFields; }
cc.checkModify(); CodeAttribute ca = methodInfo.getCodeAttribute(); if (ca == null) throw new CannotCompileException("no method body"); int nvars = jv.recordParams(getParameterTypes(), Modifier.isStatic(getModifiers())); jv.recordParamNames(ca, nvars); jv.recordLocalVariables(ca, 0); jv.recordType(getReturnType0()); jv.compileStmnt(src); Bytecode b = jv.getBytecode(); iterator.insert(b.getExceptionTable(), pos); if (rebuild) methodInfo.rebuildStackMapIf6(cc.getClassPool(), cc.getClassFile2()); throw new CannotCompileException(e); throw new CannotCompileException(e);
private void ensureNewInstance(CtClass cc) throws NotFoundException, CannotCompileException { CtMethod ccms[] = cc.getDeclaredMethods(); if( !javassist.Modifier.isAbstract(cc.getModifiers()) && !hasExisting("newInstance", "()Lwater/Freezable;", ccms) ) { cc.addMethod(CtNewMethod.make( "public water.Freezable newInstance() {\n" + " return new " +cc.getName()+"();\n" + "}", cc)); } }
int mod = m.getModifiers(); if (m.getDeclaringClass() != Object.class && !Modifier.isFinal(mod)) if (Modifier.isPublic(mod)) { CtMethod body; if (Modifier.isStatic(mod)) body = forwardStaticMethod; else ConstParameter.integer(i), proxy); wmethod.setModifiers(mod); proxy.addMethod(wmethod); else if (!Modifier.isProtected(mod) && !Modifier.isPrivate(mod)) throw new CannotCompileException( "the methods must be public, protected, or private.");