private MetaMethod[] getStaticMethods() { if (staticMethodCache != null) { return staticMethodCache; } return staticMethodCache = Arrays.stream(getMethods()).filter(m -> m.isStatic()).toArray(s -> new MetaMethod[s]); }
private MetaMethod[] getStaticMethods() { if (staticMethodCache != null) { return staticMethodCache; } return staticMethodCache = Arrays.stream(getMethods()).filter(m -> m.isStatic()).toArray(s -> new MetaMethod[s]); }
public static boolean shouldProxyMethod(final MetaMethod method) { final String methodName = method.getName(); return !method.isFinal() && !method.isStatic() && !method.isPrivate() && !methodName.equals("hashCode") && !methodName.equals("equals") && !methodName.equals("toString") && !methodName.equals("clone") && !methodName.equals("finalize"); }
public static boolean shouldProxyMethod(final MetaMethod method) { final String methodName = method.getName(); return !method.isFinal() && !method.isStatic() && !method.isPrivate() && !methodName.equals("hashCode") && !methodName.equals("equals") && !methodName.equals("toString") && !methodName.equals("clone") && !methodName.equals("finalize"); }
private Collection<MetaMethod> getAllDisposesMethods(final MetaClass type, final boolean staticOnly) { final Collection<MetaMethod> disposers = new ArrayList<>(); for (final MetaMethod method : type.getMethods()) { if (staticOnly && !method.isStatic()) { continue; } final List<MetaParameter> disposerParams = method.getParametersAnnotatedWith(Disposes.class); if (disposerParams.size() > 1) { throw new RuntimeException("Found method " + method + " in " + method.getDeclaringClassName() + " with multiple @Disposes parameters."); } else if (disposerParams.size() == 1) { disposers.add(method); } } return disposers; }
public static String methodAccess(final MetaMethod method) { final StringBuilder buf = new StringBuilder(50); if (!method.getReturnType().isVoid()) { buf.append("return "); } if (!method.isStatic()) { buf.append("instance."); } buf.append('@').append(method.getDeclaringClass().getFullyQualifiedName().replace('$', '.')) .append("::").append(method instanceof MetaConstructor ? "new" : method.getName()).append('('); for (final MetaParameter parm : method.getParameters()) { buf.append(parm.getType().getInternalName()); } buf.append(")("); final int length = method.getParameters().length; for (int i = 0; i < length; i++) { buf.append("a").append(i); if (i + 1 < length) buf.append(","); } buf.append(")"); return buf.toString(); } }
public static String methodAccess(final MetaMethod method) { final StringBuilder buf = new StringBuilder(50); if (!method.getReturnType().isVoid()) { buf.append("return "); } if (!method.isStatic()) { buf.append("instance."); } buf.append('@').append(method.getDeclaringClass().getFullyQualifiedName().replace('$', '.')) .append("::").append(method instanceof MetaConstructor ? "new" : method.getName()).append('('); for (final MetaParameter parm : method.getParameters()) { buf.append(parm.getType().getInternalName()); } buf.append(")("); final int length = method.getParameters().length; for (int i = 0; i < length; i++) { buf.append("a").append(i); if (i + 1 < length) buf.append(","); } buf.append(")"); return buf.toString(); } }
/** * @param producerTypeInjectable If this parameter is null, only static methods will be processed. */ private void processProducerMethods(final Injectable producerTypeInjectable, final MetaClass producerType, final DependencyGraphBuilder builder, final Collection<MetaMethod> disposesMethods, final boolean enabled, final List<String> problems) { final boolean staticOnly = (producerTypeInjectable == null); final Collection<Class<? extends Annotation>> producerAnnos = injectionContext.getAnnotationsForElementType(WiringElementType.ProducerElement); for (final Class<? extends Annotation> anno : producerAnnos) { final List<MetaMethod> methods = producerType.getDeclaredMethodsAnnotatedWith(anno); for (final MetaMethod method : methods) { if (!staticOnly || method.isStatic()) { processProducerMethod(producerTypeInjectable, producerType, builder, disposesMethods, method, enabled, problems); } } } }
@Override public MetaMethod getBestMatchingStaticMethod(final String name, final Class... parameters) { MetaMethod meth = getMethod(name, parameters); if (meth == null || !meth.isStatic()) { meth = null; } final MetaClass[] mcParms = new MetaClass[parameters.length]; for (int i = 0; i < parameters.length; i++) { mcParms[i] = MetaClassFactory.get(parameters[i]); } if (meth == null) { meth = getBestMatchingMethod(new GetMethodsCallback() { @Override public MetaMethod[] getMethods() { return getStaticMethods(); } }, name, mcParms); } return meth; }
@Override public MetaMethod getBestMatchingMethod(final String name, final Class... parameters) { MetaMethod meth = getMethod(name, parameters); if (meth == null || meth.isStatic()) { meth = null; } final MetaClass[] mcParms = new MetaClass[parameters.length]; for (int i = 0; i < parameters.length; i++) { mcParms[i] = MetaClassFactory.get(parameters[i]); } if (meth == null) { meth = getBestMatchingMethod(new GetMethodsCallback() { @Override public MetaMethod[] getMethods() { return AbstractMetaClass.this.getMethods(); } }, name, mcParms); } return meth; }
@Override public MetaMethod getBestMatchingStaticMethod(final String name, final Class... parameters) { MetaMethod meth = getMethod(name, parameters); if (meth == null || !meth.isStatic()) { meth = null; } final MetaClass[] mcParms = new MetaClass[parameters.length]; for (int i = 0; i < parameters.length; i++) { mcParms[i] = MetaClassFactory.get(parameters[i]); } if (meth == null) { meth = getBestMatchingMethod(new GetMethodsCallback() { @Override public MetaMethod[] getMethods() { return getStaticMethods(); } }, name, mcParms); } return meth; }
@Override public MetaMethod getBestMatchingMethod(final String name, final Class... parameters) { MetaMethod meth = getMethod(name, parameters); if (meth == null || meth.isStatic()) { meth = null; } final MetaClass[] mcParms = new MetaClass[parameters.length]; for (int i = 0; i < parameters.length; i++) { mcParms[i] = MetaClassFactory.get(parameters[i]); } if (meth == null) { meth = getBestMatchingMethod(new GetMethodsCallback() { @Override public MetaMethod[] getMethods() { return AbstractMetaClass.this.getMethods(); } }, name, mcParms); } return meth; }
@Override public ContextualStatementBuilder call(final Statement instance, final HasAnnotations annotated, final BuildMetaClass factory, final Statement... statement) { final MetaMethod method = (MetaMethod) annotated; if (method.isPublic()) { if (method.isStatic()) { return invokeStatic(method.getDeclaringClass(), method.getName(), (Object[]) statement); } else { return nestedCall(instance).invoke(method, (Object[]) statement); } } else { final Object[] params = getParams(method.isStatic(), instance, statement); return invokeStatic(notNull(factory), getPrivateMethodName(method), params); } }
for (final Class<? extends Annotation> producerAnnoType : producerAnnos) { final List<MetaMethod> producerMethods = type.getMethodsAnnotatedWith(producerAnnoType); if (!producerMethods.isEmpty() && producerMethods.stream().anyMatch(method -> !method.isStatic())) { return false;
private List<Statement> methodCreateInstanceStatements(final MetaMethod producingMember, final Injectable producerInjectable, final Injectable producedInjectable, final Collection<Dependency> paramDeps, final ClassStructureBuilder<?> bodyBlockBuilder) { final List<Statement> stmts = new ArrayList<>(); controller.ensureMemberExposed(producingMember); if (!producingMember.isStatic()) { final Statement producerInstanceValue = loadVariable("contextManager").invoke("getInstance", producerInjectable.getFactoryName()); stmts.add(declareVariable(PRODUCER_INSTANCE, producerInjectable.getInjectedType(), producerInstanceValue)); stmts.add(loadVariable(PRODUCER_INSTANCE).assignValue(castTo(producerInjectable.getInjectedType(), invokeStatic(Factory.class, "maybeUnwrapProxy", loadVariable(PRODUCER_INSTANCE))))); } final List<Statement> depScopeRegistrationStmts = new ArrayList<>(); final Statement[] producerParams = generateProducerParams(producingMember, paramDeps, stmts, depScopeRegistrationStmts); final Statement invocation = controller.exposedMethodStmt(loadVariable(PRODUCER_INSTANCE), producingMember, producerParams); stmts.add(declareFinalVariable("instance", producedInjectable.getInjectedType(), invocation)); if (!producingMember.isStatic()) { stmts.add(setProducerInstanceReference()); if (producerInjectable.getWiringElementTypes().contains(WiringElementType.DependentBean)) { stmts.add(loadVariable("this").invoke("registerDependentScopedReference", loadVariable("instance"), loadVariable(PRODUCER_INSTANCE))); } } stmts.addAll(depScopeRegistrationStmts); stmts.add(loadVariable("instance").returnValue()); return stmts; }
private boolean shouldProxyMethod(final MetaMethod method, final Multimap<String, MetaMethod> proxiedMethodsByName) { return (method.getDeclaringClass() != null && method.getDeclaringClass().isInterface()) || !method.isStatic() && (method.isPublic() || method.isProtected()) && !method.isFinal() && methodIsNotFromObjectUnlessHashCode(method) && typesInSignatureAreVisible(method) && isNotAlreadyProxied(method, proxiedMethodsByName); }
private void processProducerMethod(final Injectable producerTypeInjectable, final MetaClass producerType, final DependencyGraphBuilder builder, final Collection<MetaMethod> disposesMethods, final MetaMethod method, final boolean enabled, final List<String> problems) { final Class<? extends Annotation> directScope = getScope(method); final WiringElementType[] wiringTypes = getWiringTypeForProducer(producerType, method, directScope); final InjectableType injectableType = (enabled ? InjectableType.Producer : InjectableType.Disabled); final Injectable producedInjectable = builder.addInjectable(getMethodReturnType(method), qualFactory.forSource(method), getPathPredicate(method, problems), directScope, injectableType, wiringTypes); if (method.isStatic()) { builder.addProducerMemberDependency(producedInjectable, producerType, method); } else { builder.addProducerMemberDependency(producedInjectable, producerType, producerTypeInjectable.getQualifier(), method); } if (enabled) { processProducerAndDisposerMethodsDependencies(builder, disposesMethods, method, producedInjectable); } }
@Override public void makeMethodAccessible(final ClassStructureBuilder<?> classBuilder, final MetaMethod method, final Modifier[] modifiers) { final MetaMethod erasedMethod = method.getDeclaringClass().getErased().getDeclaredMethod(method.getName(), getErasedParamterTypes(method)); final List<Parameter> wrapperDefParms = new ArrayList<Parameter>(); if (!erasedMethod.isStatic()) { wrapperDefParms.add(Parameter.of(erasedMethod.getDeclaringClass().getErased(), "instance")); } final List<Parameter> methodDefParms = DefParameters.from(erasedMethod).getParameters(); wrapperDefParms.addAll(methodDefParms); Annotation[] annotations = NO_ANNOTATIONS; for (MetaParameter p : erasedMethod.getParameters()) { if (p.getType().getCanonicalName().equals("long")) { annotations = new Annotation[] { UNSAFE_NATIVE_LONG_ANNOTATION }; } } if (erasedMethod.getReturnType().getCanonicalName().equals("long")) { annotations = new Annotation[] { UNSAFE_NATIVE_LONG_ANNOTATION }; } classBuilder.publicMethod(erasedMethod.getReturnType(), PrivateAccessUtil.getPrivateMethodName(method)) .annotatedWith(annotations) .parameters(DefParameters.fromParameters(wrapperDefParms)) .modifiers(appendJsni(modifiers)) .body() ._(StringStatement.of(JSNIUtil.methodAccess(erasedMethod))) .finish(); }
@Override public void makeMethodAccessible(final ClassStructureBuilder<?> classBuilder, final MetaMethod method, final Modifier[] modifiers) { final MetaMethod erasedMethod = method.getDeclaringClass().getErased().getDeclaredMethod(method.getName(), getErasedParamterTypes(method)); final List<Parameter> wrapperDefParms = new ArrayList<Parameter>(); if (!erasedMethod.isStatic()) { wrapperDefParms.add(Parameter.of(erasedMethod.getDeclaringClass().getErased(), "instance")); } final List<Parameter> methodDefParms = DefParameters.from(erasedMethod).getParameters(); wrapperDefParms.addAll(methodDefParms); Annotation[] annotations = NO_ANNOTATIONS; for (MetaParameter p : erasedMethod.getParameters()) { if (p.getType().getCanonicalName().equals("long")) { annotations = new Annotation[] { UNSAFE_NATIVE_LONG_ANNOTATION }; } } if (erasedMethod.getReturnType().getCanonicalName().equals("long")) { annotations = new Annotation[] { UNSAFE_NATIVE_LONG_ANNOTATION }; } classBuilder.publicMethod(erasedMethod.getReturnType(), PrivateAccessUtil.getPrivateMethodName(method)) .annotatedWith(annotations) .parameters(DefParameters.fromParameters(wrapperDefParms)) .modifiers(appendJsni(modifiers)) .body() ._(StringStatement.of(JSNIUtil.methodAccess(erasedMethod))) .finish(); }