Search in sources :

Example 51 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project androidannotations by androidannotations.

the class ServiceActionHandler method process.

@Override
public void process(Element element, EIntentServiceHolder holder) throws Exception {
    ExecutableElement executableElement = (ExecutableElement) element;
    String methodName = element.getSimpleName().toString();
    ServiceAction annotation = element.getAnnotation(ServiceAction.class);
    String extraKey = annotation.value();
    if (extraKey.isEmpty()) {
        extraKey = methodName;
    }
    JFieldVar actionKeyField = createStaticActionField(holder, extraKey, methodName);
    addActionInOnHandleIntent(holder, executableElement, methodName, actionKeyField);
    addActionToIntentBuilder(holder, executableElement, methodName, actionKeyField);
}
Also used : JFieldVar(com.helger.jcodemodel.JFieldVar) ExecutableElement(javax.lang.model.element.ExecutableElement) ServiceAction(org.androidannotations.annotations.ServiceAction)

Example 52 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project androidannotations by androidannotations.

the class SharedPrefHandler method getValidMethods.

private List<ExecutableElement> getValidMethods(Element element) {
    List<? extends Element> members = element.getEnclosedElements();
    List<ExecutableElement> methods = ElementFilter.methodsIn(members);
    List<ExecutableElement> validMethods = new ArrayList<>();
    for (ExecutableElement method : methods) {
        validMethods.add(method);
    }
    return validMethods;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList)

Example 53 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project androidannotations by androidannotations.

the class WakeLockHandler method process.

@Override
public void process(Element element, EComponentHolder holder) {
    ExecutableElement executableElement = (ExecutableElement) element;
    WakeLock annotation = executableElement.getAnnotation(WakeLock.class);
    String tag = extractTag(executableElement);
    Level level = annotation.level();
    Flag[] flags = annotation.flags();
    JMethod method = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock previousMethodBody = codeModelHelper.removeBody(method);
    JBlock methodBody = method.body();
    IJExpression levelAndFlags = getClasses().POWER_MANAGER.staticRef(level.name());
    if (flags.length > 0) {
        for (Flag flag : flags) {
            levelAndFlags = levelAndFlags.bor(getClasses().POWER_MANAGER.staticRef(flag.name()));
        }
    }
    JInvocation newWakeLock = holder.getPowerManagerRef().invoke("newWakeLock").arg(levelAndFlags).arg(JExpr.lit(tag));
    JVar wakeLock = methodBody.decl(getClasses().WAKE_LOCK, "wakeLock", JExpr._null());
    JTryBlock tryBlock = methodBody._try();
    tryBlock.body().assign(wakeLock, newWakeLock);
    tryBlock.body().add(wakeLock.invoke("acquire"));
    tryBlock.body().add(previousMethodBody);
    JBlock finallyBlock = tryBlock._finally();
    JConditional ifStatement = finallyBlock._if(wakeLock.ne(JExpr._null()));
    ifStatement._then().add(wakeLock.invoke("release"));
}
Also used : WakeLock(org.androidannotations.annotations.WakeLock) ExecutableElement(javax.lang.model.element.ExecutableElement) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) Flag(org.androidannotations.annotations.WakeLock.Flag) JBlock(com.helger.jcodemodel.JBlock) Level(org.androidannotations.annotations.WakeLock.Level) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JVar(com.helger.jcodemodel.JVar)

Example 54 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project androidannotations by androidannotations.

the class CoreValidatorHelper method isSharedPreference.

public void isSharedPreference(Element element, ElementValidation valid) {
    TypeMirror type = element.asType();
    if (element instanceof ExecutableElement) {
        element = ((ExecutableElement) element).getParameters().get(0);
        type = element.asType();
    }
    /*
		 * The type is not available yet because it has just been generated
		 */
    if (type instanceof ErrorType || type.getKind() == TypeKind.ERROR) {
        String elementTypeName = type.toString();
        boolean sharedPrefValidatedInRound = false;
        if (elementTypeName.endsWith(classSuffix())) {
            String prefTypeName = elementTypeName.substring(0, elementTypeName.length() - classSuffix().length());
            prefTypeName = prefTypeName.replace(classSuffix() + ".", ".");
            Set<? extends Element> sharedPrefElements = validatedModel().getRootAnnotatedElements(SharedPref.class.getName());
            for (Element sharedPrefElement : sharedPrefElements) {
                TypeElement sharedPrefTypeElement = (TypeElement) sharedPrefElement;
                String sharedPrefQualifiedName = sharedPrefTypeElement.getQualifiedName().toString();
                if (sharedPrefQualifiedName.endsWith(prefTypeName)) {
                    sharedPrefValidatedInRound = true;
                    break;
                }
            }
        }
        if (!sharedPrefValidatedInRound) {
            valid.invalidate();
        }
    } else {
        extendsType(element, SharedPreferencesHelper.class.getName(), valid);
    }
}
Also used : ErrorType(javax.lang.model.type.ErrorType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) DefaultString(org.androidannotations.annotations.sharedpreferences.DefaultString) SharedPref(org.androidannotations.annotations.sharedpreferences.SharedPref) SharedPreferencesHelper(org.androidannotations.api.sharedpreferences.SharedPreferencesHelper)

Example 55 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project androidannotations by androidannotations.

the class TraceHandler method process.

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
    ExecutableElement executableElement = (ExecutableElement) element;
    String tag = extractTag(executableElement);
    int level = executableElement.getAnnotation(Trace.class).level();
    JMethod method = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock previousMethodBody = codeModelHelper.removeBody(method);
    JBlock methodBody = method.body();
    JInvocation isLoggableInvocation = getClasses().LOG.staticInvoke("isLoggable");
    isLoggableInvocation.arg(tag).arg(logLevelFromInt(level, getClasses().LOG));
    JConditional ifStatement = methodBody._if(isLoggableInvocation);
    JInvocation currentTimeInvoke = getClasses().SYSTEM.staticInvoke("currentTimeMillis");
    JBlock thenBody = ifStatement._then();
    // Log In
    String logMethodName = logMethodNameFromLevel(level);
    JInvocation logEnterInvoke = getClasses().LOG.staticInvoke(logMethodName);
    logEnterInvoke.arg(tag);
    logEnterInvoke.arg(getEnterMessage(method, executableElement));
    thenBody.add(logEnterInvoke);
    JVar startDeclaration = thenBody.decl(getCodeModel().LONG, "traceStart" + generationSuffix(), currentTimeInvoke);
    JTryBlock tryBlock;
    JVar result = null;
    if (method.type().fullName().equals("void")) {
        tryBlock = thenBody._try();
        tryBlock.body().add(previousMethodBody);
    } else {
        JInvocation superCall = codeModelHelper.getSuperCall(holder, method);
        result = thenBody.decl(getJClass(Object.class), "traceResult" + generationSuffix(), JExpr._null());
        tryBlock = thenBody._try();
        tryBlock.body().assign(result, superCall);
        tryBlock.body()._return(JExpr.cast(boxify(method.type()), result));
    }
    JBlock finallyBlock = tryBlock._finally();
    JVar durationDeclaration = finallyBlock.decl(getCodeModel().LONG, "traceDuration" + generationSuffix(), currentTimeInvoke.minus(startDeclaration));
    JInvocation logExitInvoke = getClasses().LOG.staticInvoke(logMethodName);
    logExitInvoke.arg(tag);
    logExitInvoke.arg(getExitMessage(executableElement, method, result, durationDeclaration));
    finallyBlock.add(logExitInvoke);
    JBlock elseBlock = ifStatement._else();
    elseBlock.add(previousMethodBody);
}
Also used : Trace(org.androidannotations.annotations.Trace) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) JInvocation(com.helger.jcodemodel.JInvocation) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JVar(com.helger.jcodemodel.JVar)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)345 TypeElement (javax.lang.model.element.TypeElement)158 TypeMirror (javax.lang.model.type.TypeMirror)97 VariableElement (javax.lang.model.element.VariableElement)85 Element (javax.lang.model.element.Element)72 Test (org.junit.Test)41 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)32 DeclaredType (javax.lang.model.type.DeclaredType)31 ArrayList (java.util.ArrayList)26 JBlock (com.helger.jcodemodel.JBlock)25 JInvocation (com.helger.jcodemodel.JInvocation)20 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)18 JVar (com.helger.jcodemodel.JVar)18 Map (java.util.Map)18 HashSet (java.util.HashSet)17 IJExpression (com.helger.jcodemodel.IJExpression)16 ElementValidation (org.androidannotations.ElementValidation)15 Block (com.google.devtools.j2objc.ast.Block)13 JMethod (com.helger.jcodemodel.JMethod)13 Elements (javax.lang.model.util.Elements)13